Skip to content

Commit 26997ef

Browse files
committed
Optimise Switch code generation on booleans.
If the value being matched is known to be 0 or 1, we need not do a further comparison with 0 before dispatching.
1 parent 72b4ec7 commit 26997ef

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

Changes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ Working version
2020
[Flambda_middle_end]. Run [Un_anf] from the middle end, not [Cmmgen].
2121
(Mark Shinwell, review by Pierre Chambart)
2222

23+
### Code generation and optimizations:
24+
25+
- #8672: Optimise Switch code generation on booleans.
26+
(Stephen Dolan, review by ??)
27+
2328
### Runtime system:
2429

2530
- #8619: Ensure Gc.minor_words remains accurate after a GC.

lambda/switch.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ let prerr_inter i = Printf.fprintf stderr
186186
and get_low cases i =
187187
let r,_,_ = cases.(i) in
188188
r
189+
and get_high cases i =
190+
let _,r,_ = cases.(i) in
191+
r
189192

190193
type ctests = {
191194
mutable n : int ;
@@ -659,9 +662,14 @@ let rec pkey chan = function
659662
and right = {s with cases=right} in
660663

661664
if i=1 && (lim+ctx.off)=1 && get_low cases 0+ctx.off=0 then
662-
make_if_ne
663-
ctx.arg 0
664-
(c_test ctx right) (c_test ctx left)
665+
if lcases = 2 && get_high cases 1+ctx.off = 1 then
666+
Arg.make_if
667+
ctx.arg
668+
(c_test ctx right) (c_test ctx left)
669+
else
670+
make_if_ne
671+
ctx.arg 0
672+
(c_test ctx right) (c_test ctx left)
665673
else if less_tests cright cleft then
666674
make_if_lt
667675
ctx.arg (lim+ctx.off)

0 commit comments

Comments
 (0)