Skip to content

Commit 1845adb

Browse files
committed
Share repeated location strings in Bytegen
1 parent 7bbf612 commit 1845adb

1 file changed

Lines changed: 41 additions & 9 deletions

File tree

bytecomp/bytegen.ml

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,36 @@ let rec size_of_lambda env = function
226226
| Lsequence (_lam, lam') -> size_of_lambda env lam'
227227
| _ -> RHS_nonrec
228228

229+
(**** Sharing repeated location strings ****)
230+
231+
module StringTbl = Hashtbl.Make (struct
232+
type t = string
233+
let hash = Hashtbl.hash
234+
let equal = String.equal
235+
end)
236+
237+
type string_cache = string StringTbl.t
238+
let active_cache : string_cache option ref = ref None
239+
240+
let with_string_cache f =
241+
assert (!active_cache = None);
242+
active_cache := Some (StringTbl.create 20);
243+
Fun.protect ~finally:(fun () -> active_cache := None) f
244+
245+
let cache_string s =
246+
let tbl = match !active_cache with Some c -> c | None -> assert false in
247+
match StringTbl.find tbl s with
248+
| s' -> s'
249+
| exception Not_found ->
250+
StringTbl.add tbl s s;
251+
s
252+
253+
let string_of_scoped_location l =
254+
cache_string (string_of_scoped_location l)
255+
256+
let string_of_scopes l =
257+
cache_string (string_of_scopes l)
258+
229259
(**** Merging consecutive events ****)
230260

231261
let copy_event ev kind info repr =
@@ -1086,20 +1116,22 @@ let compile_implementation modulename expr =
10861116
label_counter := 0;
10871117
sz_static_raises := [] ;
10881118
compunit_name := modulename;
1089-
let init_code = comp_block empty_env expr 0 [] in
1090-
if Stack.length functions_to_compile > 0 then begin
1091-
let lbl_init = new_label() in
1092-
Kbranch lbl_init :: comp_remainder (Klabel lbl_init :: init_code)
1093-
end else
1094-
init_code
1119+
with_string_cache (fun () ->
1120+
let init_code = comp_block empty_env expr 0 [] in
1121+
if Stack.length functions_to_compile > 0 then begin
1122+
let lbl_init = new_label() in
1123+
Kbranch lbl_init :: comp_remainder (Klabel lbl_init :: init_code)
1124+
end else
1125+
init_code)
10951126

10961127
let compile_phrase expr =
10971128
Stack.clear functions_to_compile;
10981129
label_counter := 0;
10991130
sz_static_raises := [] ;
1100-
let init_code = comp_block empty_env expr 1 [Kreturn 1] in
1101-
let fun_code = comp_remainder [] in
1102-
(init_code, fun_code)
1131+
with_string_cache (fun () ->
1132+
let init_code = comp_block empty_env expr 1 [Kreturn 1] in
1133+
let fun_code = comp_remainder [] in
1134+
(init_code, fun_code))
11031135

11041136
let reset () =
11051137
label_counter := 0;

0 commit comments

Comments
 (0)