Skip to content

Commit fdeb2a0

Browse files
committed
specialize String.make
1 parent 9d7dd21 commit fdeb2a0

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

jscomp/core/lam_analysis.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
5656
(* non-observable side effect *)
5757
| "caml_sys_get_config"
5858
| "caml_sys_get_argv" (* should be fine *)
59+
| "caml_string_repeat"
5960
| "caml_make_vect"
6061
| "caml_create_bytes"
6162
| "caml_obj_dup"

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,20 @@ let translate loc (prim_name : string)
380380
E.string_comp Ge e0 e1
381381
| _ -> assert false
382382
end
383+
| "caml_string_repeat"
384+
->
385+
begin match args with
386+
| [ n ; {expression_desc = Number (Int {i})} ] ->
387+
let str = (String.make 1 (Char.chr (Int32.to_int i))) in
388+
begin match n.expression_desc with
389+
| Number (Int {i = 1l}) -> E.str str
390+
| _ ->
391+
E.call (E.dot (E.str str) "repeat") [n]
392+
~info:Js_call_info.builtin_runtime_call
393+
end
394+
| _ ->
395+
E.runtime_call Js_runtime_modules.string "make" args
396+
end
383397
| "caml_string_greaterthan"
384398
->
385399
begin match args with
@@ -475,7 +489,7 @@ let translate loc (prim_name : string)
475489
| _ -> assert false
476490
end
477491
| "caml_create_bytes"
478-
| "caml_create_string" ->
492+
->
479493
(* Bytes.create *)
480494
(* Note that for invalid range, JS raise an Exception RangeError,
481495
here in OCaml it's [Invalid_argument], we have to preserve this semantics.

jscomp/runtime/caml_string.ml

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,8 @@ let get s i=
3636
raise (Invalid_argument "index out of bounds")
3737
else Caml_string_extern.unsafe_get s i
3838

39-
(* let caml_string_get16 s i =
40-
Caml_char.code (Caml_string_extern.unsafe_get s i) + Caml_char.code (Caml_string_extern.unsafe_get s (i+1)) lsl 8
41-
42-
let caml_string_get32 s i =
43-
Caml_char.code (Caml_string_extern.unsafe_get s i) +
44-
Caml_char.code (Caml_string_extern.unsafe_get s (i+1)) lsl 8 +
45-
Caml_char.code (Caml_string_extern.unsafe_get s (i+2)) lsl 16 +
46-
Caml_char.code (Caml_string_extern.unsafe_get s (i+3)) lsl 24 *)
47-
48-
49-
(* let get s i =
50-
if i < 0 || i >= Caml_string_extern.length s then
51-
raise (Invalid_argument "index out of bounds")
52-
else Caml_string_extern.unsafe_get s i
53-
*)
39+
let make n (ch : char) : string =
40+
(Caml_string_extern.of_char ch)
41+
|. Caml_string_extern.repeat n
42+
43+

jscomp/runtime/caml_string.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@
2424

2525

2626
val get : string -> int -> char
27+
28+
val make : int -> char -> string

jscomp/stdlib-406/string.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ module B = Bytes
3636

3737
let bts = B.unsafe_to_string
3838
let bos = B.unsafe_of_string
39-
(*-FIXME: replaced by Belt.String.repeat *)
40-
let make n c =
41-
B.make n c |> bts
39+
40+
external make : int -> char -> string = "caml_string_repeat"
41+
4242
let init n f =
4343
B.init n f |> bts
4444
let copy s =

0 commit comments

Comments
 (0)