Skip to content

Commit 693fedf

Browse files
committed
Heuristic: size of generated code.
1 parent c825694 commit 693fedf

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

compiler/ml/ast_untagged_variants.ml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ module DynamicChecks = struct
372372
| Not of 'a t
373373
| Expr of 'a
374374

375+
let rec size = function
376+
| BinOp (_, x, y) -> 1 + size x + size y
377+
| TagType _ -> 1
378+
| TypeOf x -> 1 + size x
379+
| IsInstanceOf (_, x) -> 1 + size x
380+
| Not x -> 1 + size x
381+
| Expr _ -> 1
382+
375383
let bin op x y = BinOp (op, x, y)
376384
let tag_type t = TagType t
377385
let typeof x = TypeOf x
@@ -396,7 +404,7 @@ module DynamicChecks = struct
396404
let ( &&& ) x y = bin And x y
397405

398406
let rec is_a_literal_case ~(literal_cases : tag_type list) ~block_cases
399-
(e : _ t) =
407+
~list_literal_cases (e : _ t) =
400408
let literals_overlaps_with_string () =
401409
Ext_list.exists literal_cases (function
402410
| String _ -> true
@@ -458,7 +466,6 @@ module DynamicChecks = struct
458466
Ext_list.fold_right others is_literal_1 (fun literal_n acc ->
459467
is_literal_case literal_n ||| acc))
460468
in
461-
let list_literal_cases = true in
462469
if list_literal_cases then
463470
let rec mk cases =
464471
match cases with
@@ -472,9 +479,21 @@ module DynamicChecks = struct
472479
| [c] -> is_not_block_case c
473480
| c1 :: (_ :: _ as rest) ->
474481
is_not_block_case c1
475-
&&& is_a_literal_case ~literal_cases ~block_cases:rest e
482+
&&& is_a_literal_case ~literal_cases ~block_cases:rest
483+
~list_literal_cases e
476484
| [] -> assert false
477485

486+
let is_a_literal_case ~literal_cases ~block_cases e =
487+
let with_literal_cases =
488+
is_a_literal_case ~literal_cases ~block_cases ~list_literal_cases:true e
489+
in
490+
let without_literal_cases =
491+
is_a_literal_case ~literal_cases ~block_cases ~list_literal_cases:false e
492+
in
493+
if size with_literal_cases <= size without_literal_cases then
494+
with_literal_cases
495+
else without_literal_cases
496+
478497
let is_int_tag ?(has_null_undefined_other = (false, false, false)) (e : _ t) :
479498
_ t =
480499
let has_null, has_undefined, has_other = has_null_undefined_other in

tests/tests/src/UntaggedVariants.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function classify2(x) {
2424
}
2525

2626
function cls(x) {
27-
if (x === "Two" || x === "One") {
27+
if (typeof x !== "object") {
2828
if (x === "One") {
2929
return "one";
3030
} else {
@@ -270,7 +270,7 @@ let OverlapNumber = {
270270
};
271271

272272
function checkEnum$2(e) {
273-
if (!(e === "Three" || e === "Two" || e === null)) {
273+
if (!(e === null || typeof e !== "object")) {
274274
return "Object...";
275275
}
276276
switch (e) {

tests/tests/src/untagged_variants_heuristic.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function f(x) {
99
}
1010

1111
function ff(x) {
12-
if (x === "G" || x === "F" || x === "E" || x === "D" || x === "C" || x === "B" || x === "A") {
12+
if (typeof x !== "number") {
1313
return;
1414
}
1515
console.log("abc");

0 commit comments

Comments
 (0)