Skip to content

Commit 87d375b

Browse files
bobzhangHongbo Zhang
authored andcommitted
better string processing
1 parent 786428f commit 87d375b

15 files changed

+214
-83
lines changed

jscomp/Makefile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@ releasebuild:
5757
stdlib:
5858
cd stdlib && ./build.sh
5959

60-
61-
62-
63-
# Example:
64-
# MODULE_FLAGS='-bs-module amdjs' make world
65-
# MODULE_FLAGS='-bs-module commonjs' make world
66-
# MODULE_FLAGS='-bs-module goog:buckle' make world
67-
6860
world:
6961
@echo "Making compiler"
7062
$(NATIVE) -g -inline 1000 -linkall -w -a -I +compiler-libs -I bin ocamlcommon.cmxa bin/compiler.mli bin/compiler.ml -o bin/bsc

jscomp/lam.ml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,25 @@ let if_ (a : t) (b : t) c =
268268
end
269269
| _ -> Lifthenelse (a,b,c)
270270

271-
let switch lam lam_switch : t =
272-
Lswitch(lam,lam_switch)
271+
let switch lam (lam_switch : switch) : t =
272+
match lam with
273+
| Lconst ((Const_pointer (i,_) | Const_base (Const_int i)))
274+
->
275+
begin try List.assoc i lam_switch.sw_consts
276+
with Not_found ->
277+
match lam_switch.sw_failaction with
278+
| Some x -> x
279+
| None -> assert false
280+
end
281+
| Lconst (Const_block (i,_,_)) ->
282+
begin try List.assoc i lam_switch.sw_blocks
283+
with Not_found ->
284+
match lam_switch.sw_failaction with
285+
| Some x -> x
286+
| None -> assert false
287+
end
288+
| _ ->
289+
Lswitch(lam,lam_switch)
273290

274291
let stringswitch (lam : t) cases default : t =
275292
match lam with
@@ -410,9 +427,10 @@ let prim ~primitive:(prim : Prim.t) ~args:(ll : t list) : t =
410427
-> Lift.bool (comparison cmp a b)
411428
| Pfloatcomp cmp, Const_base (Const_nativeint a), Const_base (Const_nativeint b)
412429
-> Lift.bool (comparison cmp a b)
413-
| Pintcomp cmp , Const_base (Const_int a), Const_base (Const_int b)
430+
| Pintcomp cmp ,
431+
(Const_base (Const_int a) | Const_pointer (a,_)),
432+
(Const_base (Const_int b) | Const_pointer (b,_))
414433
-> Lift.bool (comparison cmp a b)
415-
416434
| (Paddint
417435
| Psubint
418436
| Pmulint

jscomp/lam_analysis.ml

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,50 @@ let args_all_const args =
286286
let exit_inline_size = 7
287287
let small_inline_size = 5
288288

289+
(** destruct pattern will work better
290+
if it is closed lambda, otherwise
291+
you can not do full evaluation
292+
293+
We still should avoid inline too big code,
294+
295+
ideally we should also evaluate its size after inlining,
296+
since after partial evaluation, it might still be *very big*
297+
*)
298+
let destruct_pattern (body : Lam.t) params args =
299+
let rec aux v params args =
300+
match params, args with
301+
| x::xs, b::bs ->
302+
if Ident.same x v then Some b
303+
else aux v xs bs
304+
| [] , _ -> None
305+
| x::xs, [] -> assert false
306+
in
307+
match body with
308+
| Lswitch (Lvar v , switch)
309+
->
310+
begin match aux v params args with
311+
| Some (Lam.Lconst _ as lam) ->
312+
size (Lam.switch lam switch) < small_inline_size
313+
| Some _ | None -> false
314+
end
315+
| Lifthenelse(Lvar v, then_, else_)
316+
->
317+
begin match aux v params args with
318+
| Some (Lconst _ as lam) ->
319+
size (Lam.if_ lam then_ else_) < small_inline_size
320+
| Some _ | None -> false
321+
end
322+
| _ -> false
323+
289324
(** Hints to inlining *)
290-
let ok_to_inline fn args =
291-
let s = size fn in
292-
s < small_inline_size (* || *)
293-
(* (args_all_const args && s < 10 && no_side_effects fn) *)
294-
325+
let ok_to_inline ~body params args =
326+
let s = size body in
327+
s < small_inline_size ||
328+
(destruct_pattern body params args) ||
329+
(args_all_const args &&
330+
(s < 10 && no_side_effects body ))
331+
332+
295333
(* compared two lambdas in case analysis, note that we only compare some small lambdas
296334
Actually this patten is quite common in GADT, people have to write duplicated code
297335
due to the type system restriction

jscomp/lam_analysis.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ val no_side_effects : Lam.t -> bool
3434

3535
val size : Lam.t -> int
3636

37-
val ok_to_inline : Lam.t -> Lam.t list -> bool
37+
val ok_to_inline : body:Lam.t -> Lam.ident list -> Lam.t list -> bool
38+
3839
val eq_lambda : Lam.t -> Lam.t -> bool
3940
(** a conservative version of comparing two lambdas, mostly
4041
for looking for similar cases in switch

jscomp/lam_pass_remove_alias.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ let simplify_alias
172172
end
173173
else
174174
if (* Lam_analysis.size body < Lam_analysis.small_inline_size *)
175-
Lam_analysis.ok_to_inline body args
175+
Lam_analysis.ok_to_inline ~body params args
176176
then
177177

178178
(* let param_map = *)

jscomp/test/.depend

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ inline_regression_test.cmj : ../stdlib/string.cmi mt.cmi \
292292
../stdlib/filename.cmi
293293
inline_regression_test.cmx : ../stdlib/string.cmx mt.cmx \
294294
../stdlib/filename.cmx
295+
inline_string_test.cmj : ../runtime/js.cmj
296+
inline_string_test.cmx : ../runtime/js.cmx
295297
int32_test.cmj : mt.cmi ../stdlib/int32.cmi ../stdlib/format.cmi \
296298
ext_array.cmj ../stdlib/array.cmi
297299
int32_test.cmx : mt.cmx ../stdlib/int32.cmx ../stdlib/format.cmx \
@@ -1036,6 +1038,8 @@ inline_regression_test.cmo : ../stdlib/string.cmi mt.cmi \
10361038
../stdlib/filename.cmi
10371039
inline_regression_test.cmj : ../stdlib/string.cmj mt.cmj \
10381040
../stdlib/filename.cmj
1041+
inline_string_test.cmo : ../runtime/js.cmo
1042+
inline_string_test.cmj : ../runtime/js.cmj
10391043
int32_test.cmo : mt.cmi ../stdlib/int32.cmi ../stdlib/format.cmi \
10401044
ext_array.cmo ../stdlib/array.cmi
10411045
int32_test.cmj : mt.cmj ../stdlib/int32.cmj ../stdlib/format.cmj \

jscomp/test/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ OTHERS := a test_ari test_export2 test_internalOO test_obj_simple_ffi test_scope
5757
derive_dyntype derive_type_test nested_include simple_derive_test simple_derive_use\
5858
mutual_non_recursive_type external_ppx \
5959
optional_ffi_test poly_variant_test \
60-
bs_rest_test infer_type_test fs_test module_as_function \
61-
test_case_set test_mutliple string_bound_get_test
60+
bs_rest_test infer_type_test fs_test module_as_function\
61+
test_case_set test_mutliple string_bound_get_test inline_string_test
6262

6363

6464
SOURCE_LIST := js_dyn $(OTHERS)

jscomp/test/ari_regress_test.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ var g = 7;
88

99
var h = [0];
1010

11-
function gg(x, y) {
12-
var u = x + y | 0;
13-
return function (z) {
14-
return u + z | 0;
15-
};
16-
}
17-
1811
function g1(x, y) {
1912
var u = x + y | 0;
2013
h[0] = h[0] + 1 | 0;
@@ -23,7 +16,11 @@ function g1(x, y) {
2316
};
2417
}
2518

26-
var x = gg(3, 5)(6);
19+
var u = 8;
20+
21+
var x = function (z) {
22+
return u + z | 0;
23+
}(6);
2724

2825
var partial_arg = g1(3, 4);
2926

jscomp/test/ext_pervasives.js

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@ function dump(r) {
165165
];
166166
}
167167
};
168-
var opaque = function (name) {
169-
return "<" + (name + ">");
170-
};
171168
var s = r.length;
172169
var t = r.tag | 0;
173170
if (is_list(r)) {
@@ -176,10 +173,10 @@ function dump(r) {
176173
}
177174
else if (t !== 0) {
178175
if (t === Obj.lazy_tag) {
179-
return opaque("lazy");
176+
return "<lazy>";
180177
}
181178
else if (t === Obj.closure_tag) {
182-
return opaque("closure");
179+
return "<closure>";
183180
}
184181
else if (t === Obj.object_tag) {
185182
var fields$1 = get_fields(/* [] */0, s);
@@ -217,10 +214,10 @@ function dump(r) {
217214
return "Object #" + (dump(match[1]) + (" (" + ($$String.concat(", ", List.map(dump, match[2])) + ")")));
218215
}
219216
else if (t === Obj.infix_tag) {
220-
return opaque("infix");
217+
return "<infix>";
221218
}
222219
else if (t === Obj.forward_tag) {
223-
return opaque("forward");
220+
return "<forward>";
224221
}
225222
else if (t < Obj.no_scan_tag) {
226223
var fields$2 = get_fields(/* [] */0, s);
@@ -233,38 +230,39 @@ function dump(r) {
233230
return Pervasives.string_of_float(r);
234231
}
235232
else if (t === Obj.abstract_tag) {
236-
return opaque("abstract");
233+
return "<abstract>";
237234
}
238235
else if (t === Obj.custom_tag) {
239-
return opaque("custom");
236+
return "<custom>";
240237
}
241238
else if (t === Obj.custom_tag) {
242-
return opaque("final");
239+
return "<final>";
243240
}
244241
else if (t === Obj.double_array_tag) {
245242
return "[|" + ($$String.concat(";", $$Array.to_list($$Array.map(Pervasives.string_of_float, r))) + "|]");
246243
}
247244
else {
248-
return opaque(Curry._2(Printf.sprintf(/* Format */[
249-
/* String_literal */Block.__(11, [
250-
"unknown: tag ",
251-
/* Int */Block.__(4, [
252-
/* Int_d */0,
253-
/* No_padding */0,
254-
/* No_precision */0,
255-
/* String_literal */Block.__(11, [
256-
" size ",
257-
/* Int */Block.__(4, [
258-
/* Int_d */0,
259-
/* No_padding */0,
260-
/* No_precision */0,
261-
/* End_of_format */0
262-
])
263-
])
264-
])
265-
]),
266-
"unknown: tag %d size %d"
267-
]), t, s));
245+
var name = Curry._2(Printf.sprintf(/* Format */[
246+
/* String_literal */Block.__(11, [
247+
"unknown: tag ",
248+
/* Int */Block.__(4, [
249+
/* Int_d */0,
250+
/* No_padding */0,
251+
/* No_precision */0,
252+
/* String_literal */Block.__(11, [
253+
" size ",
254+
/* Int */Block.__(4, [
255+
/* Int_d */0,
256+
/* No_padding */0,
257+
/* No_precision */0,
258+
/* End_of_format */0
259+
])
260+
])
261+
])
262+
]),
263+
"unknown: tag %d size %d"
264+
]), t, s);
265+
return "<" + (name + ">");
268266
}
269267
}
270268
else {

jscomp/test/inline_string_test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
4+
console.log("list");
5+
6+
console.log("list");
7+
8+
function f(param) {
9+
if (param) {
10+
return "Some";
11+
}
12+
else {
13+
return "None";
14+
}
15+
}
16+
17+
console.log(/* tuple */[
18+
f(/* Some */[3]),
19+
"None",
20+
"Some"
21+
]);
22+
23+
console.log(/* tuple */[
24+
"A",
25+
"A"
26+
]);
27+
28+
/* Not a pure module */

0 commit comments

Comments
 (0)