Skip to content

Commit d26067f

Browse files
vouillonhhugo
authored andcommitted
Use imported string constants for JavaScript strings
1 parent 2b9f838 commit d26067f

File tree

9 files changed

+22
-84
lines changed

9 files changed

+22
-84
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Runtime/wasm: support jsoo_env and keep track of backtrace status (#1881)
2020
* Runtime/wasm: support unmarshaling compressed data (#1898)
2121
* Runtime/wasm: make resuming a continuation more efficient in Wasm (#1892)
22+
* Runtime/wasm: use imported string constants for JavaScript strings (#2022)
2223
* Compiler: improve performance of Javascript linking
2324
* Compiler: remove empty blocks (#1934)
2425
* Ppx: explicitly disallow polymorphic method (#1897)

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ let run
484484
then Some (Filename.temp_file unit_name ".wasm.map")
485485
else None)
486486
@@ fun opt_input_sourcemap ->
487-
let strings, fragments =
487+
let fragments =
488488
output
489489
code
490490
~wat_file:
@@ -500,7 +500,7 @@ let run
500500
~input_file
501501
~output_file:tmp_wasm_file
502502
();
503-
{ Link.unit_name; unit_info; strings; fragments }
503+
{ Link.unit_name; unit_info; fragments }
504504
in
505505
cont unit_data unit_name tmp_wasm_file opt_tmp_map_file
506506
in

compiler/lib-wasm/code_generation.ml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ type context =
5353
; mutable dummy_funs : Var.t IntMap.t
5454
; mutable cps_dummy_funs : Var.t IntMap.t
5555
; mutable init_code : W.instruction list
56-
; mutable string_count : int
57-
; mutable strings : string list
58-
; mutable string_index : int StringMap.t
5956
; mutable fragments : Javascript.expression StringMap.t
6057
; mutable globalized_variables : Var.Set.t
6158
; value_type : W.value_type
@@ -78,9 +75,6 @@ let make_context ~value_type =
7875
; dummy_funs = IntMap.empty
7976
; cps_dummy_funs = IntMap.empty
8077
; init_code = []
81-
; string_count = 0
82-
; strings = []
83-
; string_index = StringMap.empty
8478
; fragments = StringMap.empty
8579
; globalized_variables = Var.Set.empty
8680
; value_type
@@ -254,16 +248,6 @@ let register_init_code code st =
254248
st.context.init_code <- st'.instrs @ st.context.init_code;
255249
(), st
256250

257-
let register_string s st =
258-
let context = st.context in
259-
try StringMap.find s context.string_index, st
260-
with Not_found ->
261-
let n = context.string_count in
262-
context.string_count <- 1 + context.string_count;
263-
context.strings <- s :: context.strings;
264-
context.string_index <- StringMap.add s n context.string_index;
265-
n, st
266-
267251
let register_fragment name f st =
268252
let context = st.context in
269253
if not (StringMap.mem name context.fragments)

compiler/lib-wasm/code_generation.mli

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ type context =
3737
; mutable dummy_funs : Code.Var.t Stdlib.IntMap.t
3838
; mutable cps_dummy_funs : Code.Var.t Stdlib.IntMap.t
3939
; mutable init_code : Wasm_ast.instruction list
40-
; mutable string_count : int
41-
; mutable strings : string list
42-
; mutable string_index : int StringMap.t
4340
; mutable fragments : Javascript.expression StringMap.t
4441
; mutable globalized_variables : Code.Var.Set.t
4542
; value_type : Wasm_ast.value_type
@@ -178,8 +175,6 @@ val register_init_code : unit t -> unit t
178175

179176
val init_code : context -> unit t
180177

181-
val register_string : string -> int t
182-
183178
val register_fragment : string -> (unit -> Javascript.expression) -> unit t
184179

185180
val get_context : context t

compiler/lib-wasm/gc_target.ml

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -913,20 +913,12 @@ module Constant = struct
913913
let* () = register_global name { mut = false; typ = Type.value } c in
914914
return (W.GlobalGet name)
915915

916-
let str_js_utf8 s =
916+
let byte_string s =
917917
let b = Buffer.create (String.length s) in
918918
String.iter s ~f:(function
919-
| '\\' -> Buffer.add_string b "\\\\"
920-
| c -> Buffer.add_char b c);
921-
Buffer.contents b
922-
923-
let str_js_byte s =
924-
let b = Buffer.create (String.length s) in
925-
String.iter s ~f:(function
926-
| '\\' -> Buffer.add_string b "\\\\"
927919
| '\128' .. '\255' as c ->
928-
Buffer.add_string b "\\x";
929-
Buffer.add_char_hex b c
920+
Buffer.add_char b (Char.chr (0xC2 lor (Char.code c lsr 6)));
921+
Buffer.add_char b (Char.chr (0x80 lor (Char.code c land 0x3F)))
930922
| c -> Buffer.add_char b c);
931923
Buffer.contents b
932924

@@ -990,22 +982,18 @@ module Constant = struct
990982
| NativeString s ->
991983
let s =
992984
match s with
993-
| Utf (Utf8 s) -> str_js_utf8 s
994-
| Byte s -> str_js_byte s
985+
| Utf (Utf8 s) -> s
986+
| Byte s -> byte_string s
995987
in
996-
let* i = register_string s in
997988
let* x =
998-
let* name = unit_name in
999989
register_import
1000-
~import_module:
1001-
(match name with
1002-
| None -> "strings"
1003-
| Some name -> name ^ ".strings")
1004-
~name:(string_of_int i)
1005-
(Global { mut = false; typ = Ref { nullable = false; typ = Any } })
990+
~import_module:""
991+
~name:s
992+
(Global { mut = false; typ = Ref { nullable = false; typ = Extern } })
1006993
in
1007994
let* ty = Type.js_type in
1008-
return (Const_named ("str_" ^ s), W.StructNew (ty, [ GlobalGet x ]))
995+
return
996+
(Const_named ("str_" ^ s), W.StructNew (ty, [ AnyConvertExtern (GlobalGet x) ]))
1009997
| String s ->
1010998
let* ty = Type.string_type in
1011999
if String.length s >= string_length_threshold

compiler/lib-wasm/generate.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,12 +1217,7 @@ module Generate (Target : Target_sig.S) = struct
12171217
in
12181218
global_context.init_code <- [];
12191219
global_context.other_fields <- List.rev_append functions global_context.other_fields;
1220-
let js_code =
1221-
List.rev global_context.strings, StringMap.bindings global_context.fragments
1222-
in
1223-
global_context.string_count <- 0;
1224-
global_context.strings <- [];
1225-
global_context.string_index <- StringMap.empty;
1220+
let js_code = StringMap.bindings global_context.fragments in
12261221
global_context.fragments <- StringMap.empty;
12271222
toplevel_name, js_code
12281223

compiler/lib-wasm/generate.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ val f :
2727
-> live_vars:int array
2828
-> in_cps:Effects.in_cps
2929
-> deadcode_sentinal:Code.Var.t
30-
-> Wasm_ast.var * (string list * (string * Javascript.expression) list)
30+
-> Wasm_ast.var * (string * Javascript.expression) list
3131

3232
val add_start_function : context:Code_generation.context -> Wasm_ast.var -> unit
3333

compiler/lib-wasm/link.ml

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,17 @@ let trim_semi s =
314314
type unit_data =
315315
{ unit_name : string
316316
; unit_info : Unit_info.t
317-
; strings : string list
318317
; fragments : (string * Javascript.expression) list
319318
}
320319

321320
let info_to_sexp ~predefined_exceptions ~build_info ~unit_data =
322321
let add nm skip v rem = if skip then rem else Sexp.List (Atom nm :: v) :: rem in
323322
let units =
324323
List.map
325-
~f:(fun { unit_name; unit_info; strings; fragments } ->
324+
~f:(fun { unit_name; unit_info; fragments } ->
326325
Sexp.List
327326
(Unit_info.to_sexp unit_info
328327
|> add "name" false [ Atom unit_name ]
329-
|> add
330-
"strings"
331-
(List.is_empty strings)
332-
(List.map ~f:(fun s -> Sexp.Atom s) strings)
333328
|> add
334329
"fragments"
335330
(List.is_empty fragments)
@@ -366,9 +361,6 @@ let info_from_sexp info =
366361
let unit_name =
367362
u |> member "name" |> Option.value ~default:[] |> single string
368363
in
369-
let strings =
370-
u |> member "strings" |> Option.value ~default:[] |> List.map ~f:string
371-
in
372364
let fragments =
373365
u
374366
|> member "fragments"
@@ -383,7 +375,7 @@ let info_from_sexp info =
383375
, let lex = Parse_js.Lexer.of_string (to_string e) in
384376
Parse_js.parse_expr lex ))*)
385377
in
386-
{ unit_name; unit_info; strings; fragments })
378+
{ unit_name; unit_info; fragments })
387379
in
388380
build_info, predefined_exceptions, unit_data
389381

@@ -444,28 +436,13 @@ let build_runtime_arguments
444436
let generated_js =
445437
List.concat
446438
@@ List.map
447-
~f:(fun (unit_name, (strings, fragments)) ->
439+
~f:(fun (unit_name, fragments) ->
448440
let name s =
449441
match unit_name with
450442
| None -> s
451443
| Some nm -> nm ^ "." ^ s
452444
in
453-
let strings =
454-
if List.is_empty strings
455-
then []
456-
else
457-
[ ( name "strings"
458-
, Javascript.EArr
459-
(List.map
460-
~f:(fun s ->
461-
Javascript.Element (EStr (Utf8_string.of_string_exn s)))
462-
strings) )
463-
]
464-
in
465-
let fragments =
466-
if List.is_empty fragments then [] else [ name "fragments", obj fragments ]
467-
in
468-
strings @ fragments)
445+
if List.is_empty fragments then [] else [ name "fragments", obj fragments ])
469446
generated_js
470447
in
471448
let generated_js =
@@ -821,8 +798,8 @@ let link ~output_file ~linkall ~enable_source_maps ~files =
821798
let generated_js =
822799
List.concat
823800
@@ List.map files ~f:(fun (_, (_, units)) ->
824-
List.map units ~f:(fun { unit_name; strings; fragments; _ } ->
825-
Some unit_name, (strings, fragments)))
801+
List.map units ~f:(fun { unit_name; fragments; _ } ->
802+
Some unit_name, fragments))
826803
in
827804
let runtime_args =
828805
let js =

compiler/lib-wasm/link.mli

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ end
3838
type unit_data =
3939
{ unit_name : string
4040
; unit_info : Unit_info.t
41-
; strings : string list
4241
; fragments : (string * Javascript.expression) list
4342
}
4443

@@ -55,8 +54,7 @@ val build_runtime_arguments :
5554
-> separate_compilation:bool
5655
-> missing_primitives:string list
5756
-> wasm_dir:string
58-
-> generated_js:
59-
(string option * (string list * (string * Javascript.expression) list)) list
57+
-> generated_js:(string option * (string * Javascript.expression) list) list
6058
-> unit
6159
-> Javascript.expression
6260

0 commit comments

Comments
 (0)