Skip to content

Commit

Permalink
Avoid parsing sourceContents in Js_output
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierNicole committed Jun 4, 2024
1 parent 7421ae2 commit e25050c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
28 changes: 13 additions & 15 deletions compiler/lib/js_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1903,7 +1903,7 @@ 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_contents = None; _ } -> None
| Some { Source_map.sources_contents = Some _; _ } -> Some (ref [])
Expand All @@ -1918,30 +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
let sources_contents =
Option.map ~f:(List.map ~f:Source_map.Source_text.decode) sm.sources_contents
in
loop sm.sources (Option.value ~default:[] sources_contents);
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 @@ -1985,7 +1983,7 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
let sources_contents =
let open Option.Syntax in
let* r = contents in
Option.return (List.map ~f:Source_map.Source_text.encode (List.rev !r))
Option.return (List.rev !r)
in
let sources =
List.map sources ~f:(fun filename ->
Expand Down
3 changes: 3 additions & 0 deletions compiler/lib/source_map.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2013 Hugo Heuzard
Expand Down Expand Up @@ -566,6 +567,8 @@ module Source_text = struct
external of_json_string : string -> t = "%identity"

external to_json_string : t -> string = "%identity"

let empty = Uninterpreted ""

let to_json =
function
Expand Down
2 changes: 2 additions & 0 deletions compiler/lib/source_map.mli
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ end
module Source_text : sig
type t

val empty : t

val of_json_string : string -> t
(** By default, sources contents are left uninterpreted as decoding this field can be
costly if the amount of code is large, and is seldom required. It is guaranteed that
Expand Down

0 comments on commit e25050c

Please sign in to comment.