Skip to content

Commit ae4c709

Browse files
OlivierNicolehhugo
authored andcommitted
Optimize sourcemap processing
1 parent 999bf16 commit ae4c709

17 files changed

+1085
-419
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# dev (2024-??) - ??
2+
3+
## Features/Changes
4+
* Compiler: optimize sourcemap processing, improving linking performance #1617
5+
16
# 5.8.2 (2024-05-26) - Luc
27

38
## Bug fixes

compiler/bin-js_of_ocaml/cmd_arg.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,10 @@ let options =
312312
; file
313313
; sourceroot = sourcemap_root
314314
; sources = []
315-
; sources_content = (if sourcemap_don't_inline_content then None else Some [])
315+
; sources_contents =
316+
(if sourcemap_don't_inline_content then None else Some [])
316317
; names = []
317-
; mappings = []
318+
; mappings = Source_map.Mappings.empty
318319
} )
319320
else None
320321
in
@@ -551,9 +552,10 @@ let options_runtime_only =
551552
; file
552553
; sourceroot = sourcemap_root
553554
; sources = []
554-
; sources_content = (if sourcemap_don't_inline_content then None else Some [])
555+
; sources_contents =
556+
(if sourcemap_don't_inline_content then None else Some [])
555557
; names = []
556-
; mappings = []
558+
; mappings = Source_map.Mappings.empty
557559
} )
558560
else None
559561
in

compiler/bin-js_of_ocaml/link.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ let options =
106106
; file
107107
; sourceroot = sourcemap_root
108108
; sources = []
109-
; sources_content = Some []
109+
; sources_contents = Some []
110110
; names = []
111-
; mappings = []
111+
; mappings = Source_map.Mappings.empty
112112
} )
113113
else None
114114
in

compiler/lib/js_output.ml

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,10 +1903,10 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
19031903
let temp_mappings = ref [] in
19041904
let files = Hashtbl.create 17 in
19051905
let names = Hashtbl.create 17 in
1906-
let contents : string option list ref option =
1906+
let contents : Source_map.Source_text.t list ref option =
19071907
match source_map with
1908-
| None | Some { Source_map.sources_content = None; _ } -> None
1909-
| Some { Source_map.sources_content = Some _; _ } -> Some (ref [])
1908+
| None | Some { Source_map.sources_contents = None; _ } -> None
1909+
| Some { Source_map.sources_contents = Some _; _ } -> Some (ref [])
19101910
in
19111911
let push_mapping, get_file_index, get_name_index, source_map_enabled =
19121912
let source_map_enabled =
@@ -1918,27 +1918,28 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
19181918
| [], _ -> ()
19191919
| x :: xs, [] ->
19201920
Hashtbl.add files x (Hashtbl.length files);
1921-
Option.iter contents ~f:(fun r -> r := None :: !r);
1921+
Option.iter contents ~f:(fun r -> r := Source_map.Source_text.empty :: !r);
19221922
loop xs []
19231923
| x :: xs, y :: ys ->
19241924
Hashtbl.add files x (Hashtbl.length files);
19251925
Option.iter contents ~f:(fun r -> r := y :: !r);
19261926
loop xs ys
19271927
in
1928-
loop sm.sources (Option.value ~default:[] sm.sources_content);
1928+
loop sm.sources (Option.value ~default:[] sm.sources_contents);
19291929
List.iter sm.Source_map.names ~f:(fun f ->
19301930
Hashtbl.add names f (Hashtbl.length names));
19311931
true
19321932
in
19331933
let find_source file =
1934-
match Builtins.find file with
1935-
| Some f -> Some (Builtins.File.content f)
1936-
| None ->
1937-
if Sys.file_exists file && not (Sys.is_directory file)
1938-
then
1939-
let content = Fs.read_file file in
1940-
Some content
1941-
else None
1934+
Source_map.Source_text.encode
1935+
(match Builtins.find file with
1936+
| Some f -> Some (Builtins.File.content f)
1937+
| None ->
1938+
if Sys.file_exists file && not (Sys.is_directory file)
1939+
then
1940+
let content = Fs.read_file file in
1941+
Some content
1942+
else None)
19421943
in
19431944
( (fun pos m -> temp_mappings := (pos, m) :: !temp_mappings)
19441945
, (fun file ->
@@ -1979,19 +1980,16 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
19791980
| Some sm ->
19801981
let sources = hashtbl_to_list files in
19811982
let names = hashtbl_to_list names in
1982-
let sources_content =
1983-
match contents with
1984-
| None -> None
1985-
| Some r -> Some (List.rev !r)
1986-
in
1983+
let sources_contents = Option.map ~f:(fun x -> List.rev !x) contents in
19871984
let sources =
19881985
List.map sources ~f:(fun filename ->
19891986
match Builtins.find filename with
19901987
| None -> filename
19911988
| Some _ -> Filename.concat "/builtin" filename)
19921989
in
1990+
let sm_mappings = Source_map.Mappings.decode sm.mappings in
19931991
let mappings =
1994-
List.rev_append_map !temp_mappings sm.mappings ~f:(fun (pos, m) ->
1992+
List.rev_append_map !temp_mappings sm_mappings ~f:(fun (pos, m) ->
19951993
let gen_line = pos.PP.p_line + 1 in
19961994
let gen_col = pos.PP.p_col in
19971995
match m with
@@ -2006,7 +2004,8 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
20062004
Source_map.Gen_Ori_Name
20072005
{ gen_line; gen_col; ori_source; ori_line; ori_col; ori_name })
20082006
in
2009-
Some { sm with Source_map.sources; names; sources_content; mappings }
2007+
let mappings = Source_map.Mappings.encode mappings in
2008+
Some { sm with Source_map.sources; names; sources_contents; mappings }
20102009
in
20112010
PP.check f;
20122011
(if stats ()

0 commit comments

Comments
 (0)