Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Commit 528cc07

Browse files
committed
Faster readfile
Probably good if we're reading a lot
1 parent 7ff2eb3 commit 528cc07

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

src/Files.ml

+7-16
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,13 @@ let maybeStat path =
5050
let getMtime path =
5151
match maybeStat path with Some {Unix.st_mtime} -> Some st_mtime | _ -> None
5252

53-
let readFile path =
54-
match maybeStat path with
55-
| Some {Unix.st_kind = Unix.S_REG} ->
56-
let ic = open_in path in
57-
let try_read () =
58-
match input_line ic with exception End_of_file -> None | x -> Some x
59-
in
60-
let rec loop acc =
61-
match try_read () with
62-
| Some s -> loop (s :: acc)
63-
| None ->
64-
close_in ic;
65-
List.rev acc
66-
in
67-
let text = loop [] |> String.concat (String.make 1 '\n') in
68-
Some text
53+
let readFile ~filename =
54+
try
55+
let chan = open_in filename in
56+
let content = really_input_string chan (in_channel_length chan) in
57+
close_in_noerr chan;
58+
Some content
59+
with
6960
| _ -> None
7061

7162
let exists path = match maybeStat path with None -> false | Some _ -> true

0 commit comments

Comments
 (0)