Skip to content

Commit 3d6d337

Browse files
committed
move bytes function around, mini optimization of caml_bytes_create
1 parent 5b4914d commit 3d6d337

34 files changed

+174
-140
lines changed

jscomp/core/lam_dispatch_primitive.ml

+12-4
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,19 @@ let translate loc (prim_name : string)
477477
Also, it's creating a [bytes] which is a js array actually.
478478
*)
479479
begin match args with
480-
| [{expression_desc = Number (Int {i = 0l; _}); _}]
480+
| [{expression_desc = Number (Int {i; _}); _}]
481+
when i < 8l
481482
->
482-
E.array NA []
483+
(*Invariants: assuming bytes are [int array]*)
484+
E.array NA
485+
(if i = 0l then []
486+
else
487+
Ext_list.init
488+
(Int32.to_int i)
489+
(fun i -> E.zero_int_literal)
490+
)
483491
| _ ->
484-
E.runtime_call Js_runtime_modules.string
492+
E.runtime_call Js_runtime_modules.bytes
485493
"caml_create_bytes" args
486494
end
487495
| "caml_bool_compare" ->
@@ -537,7 +545,7 @@ let translate loc (prim_name : string)
537545
| "caml_fill_bytes"
538546
->
539547
E.runtime_call
540-
Js_runtime_modules.string "caml_fill_bytes" args
548+
Js_runtime_modules.bytes "caml_fill_bytes" args
541549
| "caml_is_printable"
542550
->
543551
call Js_runtime_modules.char

jscomp/runtime/.depend

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ caml_array.cmj : js.cmj js.cmi bs_stdlib_mini.cmi ../../lib/bsc.exe caml_builtin
3131
caml_basic.cmj : js.cmj js.cmi bs_stdlib_mini.cmi ../../lib/bsc.exe js_primitive.cmj caml_basic.cmi
3232
caml_builtin_exceptions.cmj : js.cmj js.cmi bs_stdlib_mini.cmi ../../lib/bsc.exe bs_obj.cmj caml_builtin_exceptions.cmi
3333
caml_bytes.cmj : js.cmj js.cmi bs_stdlib_mini.cmi ../../lib/bsc.exe caml_builtin_exceptions.cmj caml_bytes.cmi
34-
caml_char.cmj : js.cmj js.cmi bs_stdlib_mini.cmi ../../lib/bsc.exe caml_char.cmj
34+
caml_char.cmj : js.cmj js.cmi bs_stdlib_mini.cmi ../../lib/bsc.exe
3535
caml_exceptions.cmj : js.cmj js.cmi bs_stdlib_mini.cmi ../../lib/bsc.exe caml_nativeint.cmj caml_builtin_exceptions.cmj bs_obj.cmj caml_exceptions.cmi
3636
caml_float.cmj : js.cmj js.cmi bs_stdlib_mini.cmi ../../lib/bsc.exe caml_float.cmi
3737
caml_format.cmj : js.cmj js.cmi bs_stdlib_mini.cmi ../../lib/bsc.exe caml_int32.cmj caml_int64.cmj caml_utils.cmj caml_builtin_exceptions.cmj caml_format.cmi js_nativeint.cmj js_int64.cmj caml_nativeint.cmj caml_float.cmj caml_char.cmj bs_string.cmj

jscomp/runtime/caml_bytes.ml

+17-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24-
24+
external new_uninitialized : int -> bytes = "Array" [@@bs.new]
2525

2626
external unsafe_get : bytes -> int -> char = "%bytes_unsafe_get"
2727
external unsafe_set : bytes -> int -> char -> unit = "%bytes_unsafe_set"
@@ -31,3 +31,19 @@ let get s i =
3131
if i < 0 || i >= length s then
3232
raise (Invalid_argument "index out of bounds")
3333
else unsafe_get s i
34+
35+
let caml_fill_bytes (s : bytes) i l (c : char) =
36+
if l > 0 then
37+
for k = i to l + i - 1 do
38+
unsafe_set s k c
39+
done
40+
41+
let caml_create_bytes len : bytes =
42+
(* Node raise [RangeError] exception *)
43+
if len < 0 then raise (Invalid_argument "String.create")
44+
else
45+
let result = new_uninitialized len in
46+
for i = 0 to len - 1 do
47+
unsafe_set result i '\000'
48+
done ;
49+
result

jscomp/runtime/caml_bytes.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ external unsafe_set : bytes -> int -> char -> unit = "%bytes_unsafe_set"
2727
external length : bytes -> int = "%bytes_length"
2828

2929

30-
30+
val caml_create_bytes : int -> bytes
31+
val caml_fill_bytes : bytes -> int -> int -> char -> unit
3132
val get : bytes -> int -> char

jscomp/runtime/caml_string.ml

-19
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,6 @@ let caml_string_get s i=
5151
else Bs_string.unsafe_get s i
5252

5353

54-
let caml_create_bytes len : bytes =
55-
(* Node raise [RangeError] exception *)
56-
if len < 0 then raise (Invalid_argument "String.create")
57-
else
58-
let result = new_uninitialized len in
59-
for i = 0 to len - 1 do
60-
unsafe_set result i '\000'
61-
done ;
62-
result
63-
64-
65-
66-
67-
68-
let caml_fill_bytes (s : bytes) i l (c : char) =
69-
if l > 0 then
70-
for k = i to l + i - 1 do
71-
unsafe_set s k c
72-
done
7354

7455
(**
7556
TODO: [min] is not type specialized in OCaml

jscomp/runtime/caml_string.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ val bytes_to_string : bytes -> string
3737
val caml_string_of_char_array : char array -> string
3838
val caml_string_get : string -> int -> char
3939

40-
val caml_create_bytes : int -> bytes
41-
val caml_fill_bytes : bytes -> int -> int -> char -> unit
40+
41+
4242
val caml_blit_string : string -> int -> bytes -> int -> int -> unit
4343
val caml_blit_bytes : bytes -> int -> bytes -> int -> int -> unit
4444
val caml_string_get16 : string -> int -> int

jscomp/test/bytes_split_gpr_743_test.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ function eq(loc, param) {
2828
return /* () */0;
2929
}
3030

31-
var b = Caml_string.caml_create_bytes(3);
31+
var b = [
32+
0,
33+
0,
34+
0
35+
];
3236

3337
b[0] = /* "a" */97;
3438

@@ -47,7 +51,11 @@ eq("File \"bytes_split_gpr_743_test.ml\", line 17, characters 5-12", /* tuple */
4751
res
4852
]);
4953

50-
var b$1 = Caml_string.caml_create_bytes(3);
54+
var b$1 = [
55+
0,
56+
0,
57+
0
58+
];
5159

5260
b$1[0] = /* "a" */97;
5361

jscomp/test/complex_if_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var Mt = require("./mt.js");
44
var Block = require("../../lib/js/block.js");
55
var Bytes = require("../../lib/js/bytes.js");
6-
var Caml_string = require("../../lib/js/caml_string.js");
6+
var Caml_bytes = require("../../lib/js/caml_bytes.js");
77

88
function fib(n) {
99
if (n !== 1 && n !== 23) {
@@ -37,7 +37,7 @@ function escaped(s) {
3737
if (n === s.length) {
3838
return Bytes.copy(s);
3939
} else {
40-
var s$prime = Caml_string.caml_create_bytes(n);
40+
var s$prime = Caml_bytes.caml_create_bytes(n);
4141
n = 0;
4242
for(var i$1 = 0 ,i_finish$1 = s.length - 1 | 0; i$1 <= i_finish$1; ++i$1){
4343
var c = s[i$1];

jscomp/test/ext_bytes_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var Bytes = require("../../lib/js/bytes.js");
4-
var Caml_string = require("../../lib/js/caml_string.js");
4+
var Caml_bytes = require("../../lib/js/caml_bytes.js");
55

66
function escaped(s) {
77
var n = 0;
@@ -27,7 +27,7 @@ function escaped(s) {
2727
if (n === s.length) {
2828
return Bytes.copy(s);
2929
} else {
30-
var s$prime = Caml_string.caml_create_bytes(n);
30+
var s$prime = Caml_bytes.caml_create_bytes(n);
3131
n = 0;
3232
for(var i$1 = 0 ,i_finish$1 = s.length - 1 | 0; i$1 <= i_finish$1; ++i$1){
3333
var c = s[i$1];

jscomp/test/ext_string_test.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var List = require("../../lib/js/list.js");
44
var Bytes = require("../../lib/js/bytes.js");
55
var Curry = require("../../lib/js/curry.js");
66
var $$String = require("../../lib/js/string.js");
7+
var Caml_bytes = require("../../lib/js/caml_bytes.js");
78
var Caml_int32 = require("../../lib/js/caml_int32.js");
89
var Caml_string = require("../../lib/js/caml_string.js");
910
var Ext_bytes_test = require("./ext_bytes_test.js");
@@ -245,7 +246,7 @@ function is_empty(s) {
245246

246247
function repeat(n, s) {
247248
var len = s.length;
248-
var res = Caml_string.caml_create_bytes(Caml_int32.imul(n, len));
249+
var res = Caml_bytes.caml_create_bytes(Caml_int32.imul(n, len));
249250
for(var i = 0 ,i_finish = n - 1 | 0; i <= i_finish; ++i){
250251
$$String.blit(s, 0, res, Caml_int32.imul(i, len), len);
251252
}
@@ -403,7 +404,7 @@ function equal(x, y) {
403404
function unsafe_concat_with_length(len, sep, l) {
404405
if (l) {
405406
var hd = l[0];
406-
var r = Caml_string.caml_create_bytes(len);
407+
var r = Caml_bytes.caml_create_bytes(len);
407408
var hd_len = hd.length;
408409
var sep_len = sep.length;
409410
Caml_string.caml_blit_string(hd, 0, r, 0, hd_len);
@@ -647,7 +648,7 @@ function concat_array(sep, s) {
647648
for(var i = 0 ,i_finish = s_len - 1 | 0; i <= i_finish; ++i){
648649
len = len + s[i].length | 0;
649650
}
650-
var target = Caml_string.caml_create_bytes(len + Caml_int32.imul(s_len - 1 | 0, sep_len) | 0);
651+
var target = Caml_bytes.caml_create_bytes(len + Caml_int32.imul(s_len - 1 | 0, sep_len) | 0);
651652
var hd = s[0];
652653
var hd_len = hd.length;
653654
Caml_string.caml_blit_string(hd, 0, target, 0, hd_len);
@@ -674,7 +675,7 @@ function concat3(a, b, c) {
674675
var b_len = b.length;
675676
var c_len = c.length;
676677
var len = (a_len + b_len | 0) + c_len | 0;
677-
var target = Caml_string.caml_create_bytes(len);
678+
var target = Caml_bytes.caml_create_bytes(len);
678679
Caml_string.caml_blit_string(a, 0, target, 0, a_len);
679680
Caml_string.caml_blit_string(b, 0, target, a_len, b_len);
680681
Caml_string.caml_blit_string(c, 0, target, a_len + b_len | 0, c_len);
@@ -687,7 +688,7 @@ function concat4(a, b, c, d) {
687688
var c_len = c.length;
688689
var d_len = d.length;
689690
var len = ((a_len + b_len | 0) + c_len | 0) + d_len | 0;
690-
var target = Caml_string.caml_create_bytes(len);
691+
var target = Caml_bytes.caml_create_bytes(len);
691692
Caml_string.caml_blit_string(a, 0, target, 0, a_len);
692693
Caml_string.caml_blit_string(b, 0, target, a_len, b_len);
693694
Caml_string.caml_blit_string(c, 0, target, a_len + b_len | 0, c_len);
@@ -702,7 +703,7 @@ function concat5(a, b, c, d, e) {
702703
var d_len = d.length;
703704
var e_len = e.length;
704705
var len = (((a_len + b_len | 0) + c_len | 0) + d_len | 0) + e_len | 0;
705-
var target = Caml_string.caml_create_bytes(len);
706+
var target = Caml_bytes.caml_create_bytes(len);
706707
Caml_string.caml_blit_string(a, 0, target, 0, a_len);
707708
Caml_string.caml_blit_string(b, 0, target, a_len, b_len);
708709
Caml_string.caml_blit_string(c, 0, target, a_len + b_len | 0, c_len);

jscomp/test/ocaml_parsetree_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10379,7 +10379,7 @@ var keyword_table = create_hashtable(149, /* :: */[
1037910379
]
1038010380
]);
1038110381

10382-
var initial_string_buffer = Caml_string.caml_create_bytes(256);
10382+
var initial_string_buffer = Caml_bytes.caml_create_bytes(256);
1038310383

1038410384
var string_buff = /* record */[/* contents */initial_string_buffer];
1038510385

@@ -10393,7 +10393,7 @@ function reset_string_buffer(param) {
1039310393

1039410394
function store_string_char(c) {
1039510395
if (string_index[0] >= string_buff[0].length) {
10396-
var new_buff = Caml_string.caml_create_bytes((string_buff[0].length << 1));
10396+
var new_buff = Caml_bytes.caml_create_bytes((string_buff[0].length << 1));
1039710397
Bytes.blit(string_buff[0], 0, new_buff, 0, string_buff[0].length);
1039810398
string_buff[0] = new_buff;
1039910399
}
@@ -10525,7 +10525,7 @@ function cvt_nativeint_literal(s) {
1052510525

1052610526
function remove_underscores(s) {
1052710527
var l = s.length;
10528-
var b = Caml_string.caml_create_bytes(l);
10528+
var b = Caml_bytes.caml_create_bytes(l);
1052910529
var _src = 0;
1053010530
var _dst = 0;
1053110531
while(true) {

jscomp/test/ocaml_re_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1873,8 +1873,8 @@ function colorize(c, regexp) {
18731873
}
18741874

18751875
function flatten_cmap(cm) {
1876-
var c = Caml_string.caml_create_bytes(256);
1877-
var col_repr = Caml_string.caml_create_bytes(256);
1876+
var c = Caml_bytes.caml_create_bytes(256);
1877+
var col_repr = Caml_bytes.caml_create_bytes(256);
18781878
var v = 0;
18791879
c[0] = /* "\000" */0;
18801880
col_repr[0] = /* "\000" */0;

jscomp/test/ocaml_typedtree_test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -20725,7 +20725,7 @@ var keyword_table = create_hashtable(149, /* :: */[
2072520725
]
2072620726
]);
2072720727

20728-
var initial_string_buffer = Caml_string.caml_create_bytes(256);
20728+
var initial_string_buffer = Caml_bytes.caml_create_bytes(256);
2072920729

2073020730
var string_buff = /* record */[/* contents */initial_string_buffer];
2073120731

@@ -20739,7 +20739,7 @@ function reset_string_buffer(param) {
2073920739

2074020740
function store_string_char(c) {
2074120741
if (string_index[0] >= string_buff[0].length) {
20742-
var new_buff = Caml_string.caml_create_bytes((string_buff[0].length << 1));
20742+
var new_buff = Caml_bytes.caml_create_bytes((string_buff[0].length << 1));
2074320743
Bytes.blit(string_buff[0], 0, new_buff, 0, string_buff[0].length);
2074420744
string_buff[0] = new_buff;
2074520745
}
@@ -20871,7 +20871,7 @@ function cvt_nativeint_literal(s) {
2087120871

2087220872
function remove_underscores(s) {
2087320873
var l = s.length;
20874-
var b = Caml_string.caml_create_bytes(l);
20874+
var b = Caml_bytes.caml_create_bytes(l);
2087520875
var _src = 0;
2087620876
var _dst = 0;
2087720877
while(true) {
@@ -47653,7 +47653,7 @@ function is_big(obj) {
4765347653
var size = error_size[0];
4765447654
if (size > 0) {
4765547655
if (buffer[0].length < size) {
47656-
buffer[0] = Caml_string.caml_create_bytes(size);
47656+
buffer[0] = Caml_bytes.caml_create_bytes(size);
4765747657
}
4765847658
try {
4765947659
Marshal.to_buffer(buffer[0], 0, size, obj, /* [] */0);

jscomp/test/parser_api.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ function create_hashtable(size, init) {
830830
}
831831

832832
function copy_file(ic, oc) {
833-
var buff = Caml_string.caml_create_bytes(4096);
833+
var buff = Caml_bytes.caml_create_bytes(4096);
834834
var _param = /* () */0;
835835
while(true) {
836836
var n = Pervasives.input(ic, buff, 0, 4096);
@@ -845,7 +845,7 @@ function copy_file(ic, oc) {
845845
}
846846

847847
function copy_file_chunk(ic, oc, len) {
848-
var buff = Caml_string.caml_create_bytes(4096);
848+
var buff = Caml_bytes.caml_create_bytes(4096);
849849
var _n = len;
850850
while(true) {
851851
var n = _n;
@@ -866,7 +866,7 @@ function copy_file_chunk(ic, oc, len) {
866866

867867
function string_of_file(ic) {
868868
var b = $$Buffer.create(65536);
869-
var buff = Caml_string.caml_create_bytes(4096);
869+
var buff = Caml_bytes.caml_create_bytes(4096);
870870
var _param = /* () */0;
871871
while(true) {
872872
var n = Pervasives.input(ic, buff, 0, 4096);
@@ -1106,9 +1106,9 @@ function create(str_size) {
11061106
var tbl_size = Caml_int32.div(str_size, Sys.max_string_length) + 1 | 0;
11071107
var tbl = Caml_array.caml_make_vect(tbl_size, Bytes.empty);
11081108
for(var i = 0 ,i_finish = tbl_size - 2 | 0; i <= i_finish; ++i){
1109-
Caml_array.caml_array_set(tbl, i, Caml_string.caml_create_bytes(Sys.max_string_length));
1109+
Caml_array.caml_array_set(tbl, i, Caml_bytes.caml_create_bytes(Sys.max_string_length));
11101110
}
1111-
Caml_array.caml_array_set(tbl, tbl_size - 1 | 0, Caml_string.caml_create_bytes(Caml_int32.mod_(str_size, Sys.max_string_length)));
1111+
Caml_array.caml_array_set(tbl, tbl_size - 1 | 0, Caml_bytes.caml_create_bytes(Caml_int32.mod_(str_size, Sys.max_string_length)));
11121112
return tbl;
11131113
}
11141114

@@ -13133,7 +13133,7 @@ var keyword_table = create_hashtable(149, /* :: */[
1313313133
]
1313413134
]);
1313513135

13136-
var initial_string_buffer = Caml_string.caml_create_bytes(256);
13136+
var initial_string_buffer = Caml_bytes.caml_create_bytes(256);
1313713137

1313813138
var string_buff = /* record */[/* contents */initial_string_buffer];
1313913139

@@ -13147,7 +13147,7 @@ function reset_string_buffer(param) {
1314713147

1314813148
function store_string_char(c) {
1314913149
if (string_index[0] >= string_buff[0].length) {
13150-
var new_buff = Caml_string.caml_create_bytes((string_buff[0].length << 1));
13150+
var new_buff = Caml_bytes.caml_create_bytes((string_buff[0].length << 1));
1315113151
Bytes.blit(string_buff[0], 0, new_buff, 0, string_buff[0].length);
1315213152
string_buff[0] = new_buff;
1315313153
}
@@ -13287,7 +13287,7 @@ function cvt_nativeint_literal(s) {
1328713287

1328813288
function remove_underscores(s) {
1328913289
var l = s.length;
13290-
var b = Caml_string.caml_create_bytes(l);
13290+
var b = Caml_bytes.caml_create_bytes(l);
1329113291
var _src = 0;
1329213292
var _dst = 0;
1329313293
while(true) {

jscomp/test/qcc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ var glo = Bytes.make(4096, /* "\000" */0);
116116

117117
var gpos = /* record */[/* contents */0];
118118

119-
var s = Caml_string.caml_create_bytes(100);
119+
var s = Caml_bytes.caml_create_bytes(100);
120120

121121
function getq(param) {
122122
var c = Curry._1(getch, /* () */0);

0 commit comments

Comments
 (0)