Skip to content

Commit

Permalink
Optimize sourcemap processing
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierNicole committed Jun 11, 2024
1 parent 519dc5c commit fb76558
Show file tree
Hide file tree
Showing 17 changed files with 1,074 additions and 418 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# dev (2024-??) - ??

## Features/Changes
* Compiler: optimize sourcemap processing, improving linking performance #1617

# 5.8.2 (2024-05-26) - Luc

## Bug fixes
Expand Down
10 changes: 6 additions & 4 deletions compiler/bin-js_of_ocaml/cmd_arg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,10 @@ let options =
; file
; sourceroot = sourcemap_root
; sources = []
; sources_content = (if sourcemap_don't_inline_content then None else Some [])
; sources_contents =
(if sourcemap_don't_inline_content then None else Some [])
; names = []
; mappings = []
; mappings = Source_map.Mappings.empty
} )
else None
in
Expand Down Expand Up @@ -551,9 +552,10 @@ let options_runtime_only =
; file
; sourceroot = sourcemap_root
; sources = []
; sources_content = (if sourcemap_don't_inline_content then None else Some [])
; sources_contents =
(if sourcemap_don't_inline_content then None else Some [])
; names = []
; mappings = []
; mappings = Source_map.Mappings.empty
} )
else None
in
Expand Down
4 changes: 2 additions & 2 deletions compiler/bin-js_of_ocaml/link.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ let options =
; file
; sourceroot = sourcemap_root
; sources = []
; sources_content = Some []
; sources_contents = Some []
; names = []
; mappings = []
; mappings = Source_map.Mappings.empty
} )
else None
in
Expand Down
41 changes: 22 additions & 19 deletions compiler/lib/js_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1903,10 +1903,10 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
let temp_mappings = ref [] in
let files = Hashtbl.create 17 in
let names = Hashtbl.create 17 in
let contents : string option list ref option =
let contents : Source_map.Source_text.t list ref option =
match source_map with
| None | Some { Source_map.sources_content = None; _ } -> None
| Some { Source_map.sources_content = Some _; _ } -> Some (ref [])
| None | Some { Source_map.sources_contents = None; _ } -> None
| Some { Source_map.sources_contents = Some _; _ } -> Some (ref [])
in
let push_mapping, get_file_index, get_name_index, source_map_enabled =
let source_map_enabled =
Expand All @@ -1918,27 +1918,28 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
| [], _ -> ()
| x :: xs, [] ->
Hashtbl.add files x (Hashtbl.length files);
Option.iter contents ~f:(fun r -> r := None :: !r);
Option.iter contents ~f:(fun r -> r := Source_map.Source_text.empty :: !r);
loop xs []
| x :: xs, y :: ys ->
Hashtbl.add files x (Hashtbl.length files);
Option.iter contents ~f:(fun r -> r := y :: !r);
loop xs ys
in
loop sm.sources (Option.value ~default:[] sm.sources_content);
loop sm.sources (Option.value ~default:[] sm.sources_contents);
List.iter sm.Source_map.names ~f:(fun f ->
Hashtbl.add names f (Hashtbl.length names));
true
in
let find_source file =
match Builtins.find file with
| Some f -> Some (Builtins.File.content f)
| None ->
if Sys.file_exists file && not (Sys.is_directory file)
then
let content = Fs.read_file file in
Some content
else None
Source_map.Source_text.encode
(match Builtins.find file with
| Some f -> Some (Builtins.File.content f)
| None ->
if Sys.file_exists file && not (Sys.is_directory file)
then
let content = Fs.read_file file in
Some content
else None)
in
( (fun pos m -> temp_mappings := (pos, m) :: !temp_mappings)
, (fun file ->
Expand Down Expand Up @@ -1979,19 +1980,20 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
| Some sm ->
let sources = hashtbl_to_list files in
let names = hashtbl_to_list names in
let sources_content =
match contents with
| None -> None
| Some r -> Some (List.rev !r)
let sources_contents =
let open Option.Syntax in
let* r = contents in
Option.return (List.rev !r)
in
let sources =
List.map sources ~f:(fun filename ->
match Builtins.find filename with
| None -> filename
| Some _ -> Filename.concat "/builtin" filename)
in
let sm_mappings = Source_map.Mappings.decode sm.mappings in
let mappings =
List.rev_append_map !temp_mappings sm.mappings ~f:(fun (pos, m) ->
List.rev_append_map !temp_mappings sm_mappings ~f:(fun (pos, m) ->
let gen_line = pos.PP.p_line + 1 in
let gen_col = pos.PP.p_col in
match m with
Expand All @@ -2006,7 +2008,8 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
Source_map.Gen_Ori_Name
{ gen_line; gen_col; ori_source; ori_line; ori_col; ori_name })
in
Some { sm with Source_map.sources; names; sources_content; mappings }
let mappings = Source_map.Mappings.encode mappings in
Some { sm with Source_map.sources; names; sources_contents; mappings }
in
PP.check f;
(if stats ()
Expand Down
Loading

0 comments on commit fb76558

Please sign in to comment.