Skip to content

Commit c5d39ce

Browse files
authored
Small cleanup (#1893)
* Compiler: better heuristic for read_lines * Misc: review open_in vs open_in_bin * Compiler: refactor reading lines from files * fixup! Compiler: refactor reading lines from files * fixup! fixup! Compiler: refactor reading lines from files
1 parent ca9207d commit c5d39ce

File tree

13 files changed

+99
-117
lines changed

13 files changed

+99
-117
lines changed

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ let run
184184
| Some file ->
185185
if not (Sys.file_exists file)
186186
then failwith (Printf.sprintf "export file %S does not exist" file);
187-
let ic = open_in file in
187+
let ic = open_in_text file in
188188
let t = Hashtbl.create 17 in
189189
(try
190190
while true do
191-
Hashtbl.add t (input_line ic) ()
191+
Hashtbl.add t (String.trim (In_channel.input_line_exn ic)) ()
192192
done;
193193
assert false
194194
with End_of_file -> ());

compiler/bin-wasm_of_ocaml/preprocess.ml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ open Wasm_of_ocaml_compiler
2222

2323
let () = Sys.catch_break true
2424

25-
let read_contents ch =
26-
let buf = Buffer.create 65536 in
27-
let b = Bytes.create 65536 in
28-
let rec read () =
29-
let n = input ch b 0 (Bytes.length b) in
30-
if n > 0
31-
then (
32-
Buffer.add_subbytes buf b 0 n;
33-
read ())
34-
in
35-
read ();
36-
Buffer.contents buf
37-
3825
type variables =
3926
{ enable : string list
4027
; disable : string list
@@ -102,7 +89,7 @@ let preprocess { input_file; output_file; variables } =
10289
match input_file with
10390
| None -> f stdin
10491
| Some file ->
105-
let ch = open_in file in
92+
let ch = open_in_text file in
10693
let res = f ch in
10794
close_in ch;
10895
res
@@ -112,7 +99,7 @@ let preprocess { input_file; output_file; variables } =
11299
| Some "-" | None -> f stdout
113100
| Some file -> Filename.gen_file file f
114101
in
115-
let contents = with_input read_contents in
102+
let contents = with_input In_channel.input_all in
116103
let res =
117104
Wat_preprocess.f
118105
~filename:(Option.value ~default:"-" input_file)

compiler/lib-wasm/binaryen.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ let generate_dependencies ~dependencies primitives =
7676
(Yojson.Basic.Util.to_list (Yojson.Basic.from_string dependencies))))
7777

7878
let filter_unused_primitives primitives usage_file =
79-
let ch = open_in usage_file in
79+
let ch = open_in_bin usage_file in
8080
let s = ref primitives in
8181
(try
8282
while true do
8383
let l = input_line ch in
8484
match String.drop_prefix ~prefix:"unused: js:" l with
85-
| Some nm -> s := StringSet.remove nm !s
85+
| Some nm -> s := StringSet.remove (String.trim nm) !s
8686
| None -> ()
8787
done
8888
with End_of_file -> ());

compiler/lib-wasm/link.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ module Wasm_binary = struct
106106
let check ~contents = String.starts_with ~prefix:header contents
107107

108108
let check_file ~file =
109-
let ch = open_in file in
109+
let ch = open_in_bin file in
110110
let res =
111111
try
112112
let s = really_input_string ch 8 in

compiler/lib/link_js.ml

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,7 @@ end = struct
5757
t.lnum <- 0
5858

5959
let open_ fname =
60-
let lines =
61-
(* If we are not on 32-bit hardware (where the max string length is too
62-
small), read the entire file and split it in lines. This is faster
63-
than reading it line by line. On 32 bits, we fall back to a
64-
line-by-line read. *)
65-
let ic = open_in_bin fname in
66-
let x =
67-
if Sys.word_size >= 64
68-
then
69-
really_input_string ic (in_channel_length ic) |> String.split_on_char ~sep:'\n'
70-
else In_channel.input_lines ic
71-
in
72-
close_in ic;
73-
x
74-
in
60+
let lines = file_lines_bin fname in
7561
{ lines; lnum = 0; fname; current = lines }
7662

7763
let next t =

compiler/lib/parse_js.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ end = struct
6767
{ l; env = Flow_lexer.Lex_env.create l; report_error }
6868

6969
let of_file file : t =
70-
let ic = open_in file in
70+
let ic = open_in_bin file in
7171
let lexbuf = Sedlexing.Utf8.from_channel ic in
7272
Sedlexing.set_filename lexbuf file;
7373
create lexbuf

compiler/lib/source_map.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,22 @@ type info =
773773
; sources : string list
774774
; names : string list
775775
}
776+
777+
let find_in_js_file file =
778+
let lines =
779+
file_lines_bin file
780+
|> List.filter_map ~f:(String.drop_prefix ~prefix:"//# sourceMappingURL=")
781+
in
782+
match lines with
783+
| [ line ] ->
784+
let content =
785+
match String.drop_prefix ~prefix:"data:application/json;base64," line with
786+
| None ->
787+
let ic = open_in_bin (String.trim line) in
788+
let c = In_channel.input_all ic in
789+
close_in ic;
790+
c
791+
| Some base64 -> Base64.decode_exn (String.trim base64)
792+
in
793+
Some (of_string content)
794+
| _ -> None

compiler/lib/source_map.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ val of_file : ?tmp_buf:Buffer.t -> string -> t
149149

150150
val invariant : t -> unit
151151

152+
val find_in_js_file : string -> t option
153+
152154
type info =
153155
{ mappings : Mappings.decoded
154156
; sources : string list

compiler/lib/stdlib.ml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@
1616
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717
*)
1818

19+
let open_in_text = open_in
20+
21+
let open_out_text = open_out
22+
23+
module Deprecated : sig
24+
val open_in : string -> in_channel [@@deprecated "use open_int_text/open_int_bin"]
25+
26+
val open_out : string -> out_channel [@@deprecated "use open_out_text/open_out_bin"]
27+
end = struct
28+
let open_in = open_in
29+
30+
let open_out = open_out
31+
end
32+
1933
module Poly = struct
2034
external ( < ) : 'a -> 'a -> bool = "%lessthan"
2135

@@ -1234,6 +1248,8 @@ module Fun = struct
12341248
end
12351249

12361250
module In_channel = struct
1251+
let stdlib_input_line = input_line
1252+
12371253
(* Read up to [len] bytes into [buf], starting at [ofs]. Return total bytes
12381254
read. *)
12391255
let read_upto ic buf ofs len =
@@ -1327,6 +1343,8 @@ module In_channel = struct
13271343
| exception End_of_file -> acc
13281344
in
13291345
List.rev (aux [])
1346+
1347+
let input_line_exn = stdlib_input_line
13301348
end
13311349
[@@if ocaml_version < (4, 14, 0)]
13321350

@@ -1341,9 +1359,50 @@ module In_channel = struct
13411359
| line -> line :: input_lines ic
13421360
| exception End_of_file -> []
13431361
[@@if ocaml_version < (5, 1, 0)]
1362+
1363+
let input_line_exn = stdlib_input_line
13441364
end
13451365
[@@if ocaml_version >= (4, 14, 0)]
13461366

1367+
let split_lines s =
1368+
if String.equal s ""
1369+
then []
1370+
else
1371+
let sep = '\n' in
1372+
let r = ref [] in
1373+
let j = ref (String.length s) in
1374+
(* ignore trailing new line *)
1375+
if Char.equal (String.unsafe_get s (!j - 1)) sep then decr j;
1376+
for i = !j - 1 downto 0 do
1377+
if Char.equal (String.unsafe_get s i) sep
1378+
then (
1379+
r := String.sub s ~pos:(i + 1) ~len:(!j - i - 1) :: !r;
1380+
j := i)
1381+
done;
1382+
String.sub s ~pos:0 ~len:!j :: !r
1383+
1384+
let input_lines_read_once ic len = really_input_string ic len |> split_lines
1385+
1386+
let file_lines_bin fname =
1387+
(* If possible, read the entire file and split it in lines.
1388+
This is faster than reading it line by line.
1389+
Otherwise, we fall back to a line-by-line read. *)
1390+
let ic = open_in_bin fname in
1391+
let len = in_channel_length ic in
1392+
let x =
1393+
if len < Sys.max_string_length
1394+
then input_lines_read_once ic len
1395+
else In_channel.input_lines ic
1396+
in
1397+
close_in ic;
1398+
x
1399+
1400+
let file_lines_text file =
1401+
let ic = open_in_text file in
1402+
let c = In_channel.input_lines ic in
1403+
close_in ic;
1404+
c
1405+
13471406
let generated_name = function
13481407
| "param" | "match" | "switcher" -> true
13491408
| s -> String.is_prefix ~prefix:"cst_" s

compiler/tests-compiler/util/util.ml

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module Filetype : Filetype_intf.S = struct
103103
Bytes.unsafe_to_string s
104104

105105
let write_file name contents =
106-
let channel = open_out name in
106+
let channel = open_out_bin name in
107107
Printf.fprintf channel "%s" contents;
108108
close_out channel
109109

@@ -254,38 +254,14 @@ let run_bytecode file =
254254
let swap_extention filename ~ext =
255255
Format.sprintf "%s.%s" (Filename.remove_extension filename) ext
256256

257-
let input_lines file =
258-
let rec loop acc ic =
259-
match input_line ic with
260-
| line -> loop (line :: acc) ic
261-
| exception End_of_file -> List.rev acc
262-
in
263-
let ic = open_in file in
264-
let lines = loop [] ic in
265-
close_in ic;
266-
lines
267-
268257
let print_file file =
269258
let open Js_of_ocaml_compiler.Stdlib in
270-
let all = input_lines file in
259+
let all = file_lines_text file in
271260
Printf.printf "$ cat %S\n" file;
272261
List.iteri all ~f:(fun i line -> Printf.printf "%3d: %s\n" (i + 1) line)
273262

274263
let extract_sourcemap file =
275-
let open Js_of_ocaml_compiler.Stdlib in
276-
let lines =
277-
input_lines (Filetype.path_of_js_file file)
278-
|> List.filter_map ~f:(String.drop_prefix ~prefix:"//# sourceMappingURL=")
279-
in
280-
match lines with
281-
| [ line ] ->
282-
let content =
283-
match String.drop_prefix ~prefix:"data:application/json;base64," line with
284-
| None -> String.concat ~sep:"\n" (input_lines line)
285-
| Some base64 -> Js_of_ocaml_compiler.Base64.decode_exn base64
286-
in
287-
Some (Js_of_ocaml_compiler.Source_map.of_string content)
288-
| _ -> None
264+
Js_of_ocaml_compiler.Source_map.find_in_js_file (Filetype.path_of_js_file file)
289265

290266
let compile_to_javascript
291267
?(flags = [])

0 commit comments

Comments
 (0)