Skip to content

Commit 467cc7b

Browse files
authored
Merge pull request #4286 from BuckleScript/optimize_array_for_loop
optimize array for loop
2 parents 8fade7b + 8aafc45 commit 467cc7b

File tree

137 files changed

+602
-514
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+602
-514
lines changed

jscomp/core/j.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ and exception_ident = ident
9494

9595
and for_ident = ident
9696

97-
and for_direction = Asttypes.direction_flag
97+
and for_direction = Js_op.direction_flag
9898

9999
and property_map =
100100
(property_name * expression) list

jscomp/core/js_dump.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ let pp_var_declare cxt f id =
265265

266266
let pp_direction f (direction : J.for_direction) =
267267
match direction with
268+
| Up
268269
| Upto -> P.string f L.plus_plus
269270
| Downto -> P.string f L.minus_minus
270271

@@ -1171,6 +1172,10 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11711172
let (_,_,right) = Js_op_util.op_prec Le in
11721173
P.string f L.le;
11731174
right
1175+
| Up ->
1176+
let (_,_,right) = Js_op_util.op_prec Lt in
1177+
P.string f L.lt;
1178+
right
11741179
| Downto ->
11751180
let (_,_,right) = Js_op_util.op_prec Ge in
11761181
P.string f L.ge ;

jscomp/core/js_dump_lit.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ let string_cap = "String"
8181
let fromCharcode = "fromCharCode"
8282
let eq = "="
8383
let le = "<="
84+
let lt = "<"
8485
let ge = ">="
86+
let gt = ">"
8587
let plus_plus = "++"
8688
(* FIXME: use (i = i + 1 | 0) instead *)
8789
let minus_minus = "--"

jscomp/core/js_op.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ type mutable_flag =
184184
| Mutable
185185
| Immutable
186186
| NA
187+
type direction_flag =
188+
| Upto
189+
| Downto
190+
| Up
187191

188192
(*
189193
{[

jscomp/core/lam_compile.ml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,13 @@ and compile_while (predicate : Lam.t) (body : Lam.t) (lambda_cxt : Lam_compile_c
954954
*)
955955

956956
and compile_for
957-
id start finish direction body (lambda_cxt : Lam_compile_context.t) =
957+
(id : J.for_ident)
958+
(start : Lam.t)
959+
(finish : Lam.t)
960+
(direction : Js_op.direction_flag)
961+
(body : Lam.t)
962+
(lambda_cxt : Lam_compile_context.t)
963+
=
958964
let new_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in
959965
let block =
960966
match compile_lambda new_cxt start,
@@ -1599,8 +1605,13 @@ and compile_lambda
15991605
compile_staticcatch cur_lam lambda_cxt
16001606
| Lwhile(p,body) ->
16011607
compile_while p body lambda_cxt
1602-
| Lfor (id,start,finish,direction,body) ->
1603-
compile_for id start finish direction body lambda_cxt
1608+
| Lfor (id,start,finish,direction,body) ->
1609+
begin match direction,finish with
1610+
| Upto, Lprim {primitive = Psubint ; args = [ new_finish ; Lconst (Const_int 1) ]} ->
1611+
compile_for id start new_finish Up body lambda_cxt
1612+
| _ ->
1613+
compile_for id start finish (if direction = Upto then Upto else Downto) body lambda_cxt
1614+
end
16041615
| Lassign(id,lambda) ->
16051616
compile_assign id lambda lambda_cxt
16061617
| Ltrywith(lam,id, catch) -> (* generate documentation *)

jscomp/test/array_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function starts_with(xs, prefix, p) {
1818
return false;
1919
}
2020
try {
21-
for(var i = 0 ,i_finish = len2 - 1 | 0; i <= i_finish; ++i){
21+
for(var i = 0; i < len2; ++i){
2222
if (!Curry._2(p, Caml_array.caml_array_get(xs, i), Caml_array.caml_array_get(prefix, i))) {
2323
throw H;
2424
}

jscomp/test/bdd.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ function random(param) {
331331

332332
function random_vars(n) {
333333
var vars = Caml_array.caml_make_vect(n, false);
334-
for(var i = 0 ,i_finish = n - 1 | 0; i <= i_finish; ++i){
334+
for(var i = 0; i < n; ++i){
335335
Caml_array.caml_array_set(vars, i, random(undefined));
336336
}
337337
return vars;
@@ -353,7 +353,7 @@ function bool_equal(a, b) {
353353

354354
function test_hwb(bdd, vars) {
355355
var ntrue = 0;
356-
for(var i = 0 ,i_finish = vars.length - 1 | 0; i <= i_finish; ++i){
356+
for(var i = 0 ,i_finish = vars.length; i < i_finish; ++i){
357357
if (Caml_array.caml_array_get(vars, i)) {
358358
ntrue = ntrue + 1 | 0;
359359
}

jscomp/test/bench.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function map(f, a) {
1212
return [];
1313
}
1414
var r = Caml_array.caml_make_vect(l, f$1(a[0]));
15-
for(var i = 1 ,i_finish = l - 1 | 0; i <= i_finish; ++i){
15+
for(var i = 1; i < l; ++i){
1616
r[i] = f$1(a[i]);
1717
}
1818
return r;
@@ -39,7 +39,7 @@ function init(l, f) {
3939
function fold_left(f, x, a) {
4040
var f$1 = Curry.__2(f);
4141
var r = x;
42-
for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){
42+
for(var i = 0 ,i_finish = a.length; i < i_finish; ++i){
4343
r = f$1(r, a[i]);
4444
}
4545
return r;

jscomp/test/bigarray_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ var Caml_external_polyfill = require("../../lib/js/caml_external_polyfill.js");
55

66
function sum(v) {
77
var result = 0;
8-
for(var i = 0 ,i_finish = Caml_external_polyfill.resolve("caml_ba_dim_1")(v) - 1 | 0; i <= i_finish; ++i){
8+
for(var i = 0 ,i_finish = Caml_external_polyfill.resolve("caml_ba_dim_1")(v); i < i_finish; ++i){
99
result = result + Caml_external_polyfill.resolve("caml_ba_get_1")(v, i) | 0;
1010
}
1111

1212
}
1313

1414
function init(v) {
15-
for(var i = 0 ,i_finish = Caml_external_polyfill.resolve("caml_ba_dim_1")(v) - 1 | 0; i <= i_finish; ++i){
15+
for(var i = 0 ,i_finish = Caml_external_polyfill.resolve("caml_ba_dim_1")(v); i < i_finish; ++i){
1616
v[i] = {
1717
re: Caml_int32.imul(i, i),
1818
im: Caml_int32.imul(Caml_int32.imul(i, i), i)
@@ -22,14 +22,14 @@ function init(v) {
2222
}
2323

2424
function init2(v) {
25-
for(var i = 0 ,i_finish = Caml_external_polyfill.resolve("caml_ba_dim_1")(v) - 1 | 0; i <= i_finish; ++i){
25+
for(var i = 0 ,i_finish = Caml_external_polyfill.resolve("caml_ba_dim_1")(v); i < i_finish; ++i){
2626
v[i] = i;
2727
}
2828

2929
}
3030

3131
function init3(v) {
32-
for(var i = 0 ,i_finish = Caml_external_polyfill.resolve("caml_ba_dim_1")(v) - 1 | 0; i <= i_finish; ++i){
32+
for(var i = 0 ,i_finish = Caml_external_polyfill.resolve("caml_ba_dim_1")(v); i < i_finish; ++i){
3333
Caml_external_polyfill.resolve("caml_ba_set_1")(v, i, i);
3434
}
3535

jscomp/test/bs_array_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,9 @@ function makeMatrixExn(sx, sy, init) {
392392
throw new Error("File \"bs_array_test.ml\", line 109, characters 4-10");
393393
}
394394
var res = new Array(sx);
395-
for(var x = 0 ,x_finish = sx - 1 | 0; x <= x_finish; ++x){
395+
for(var x = 0; x < sx; ++x){
396396
var initY = new Array(sy);
397-
for(var y = 0 ,y_finish = sy - 1 | 0; y <= y_finish; ++y){
397+
for(var y = 0; y < sy; ++y){
398398
initY[y] = init;
399399
}
400400
res[x] = initY;

0 commit comments

Comments
 (0)