|
| 1 | +// The integer switcher plans over the set of *distinct* actions it is handed: |
| 2 | +// given enough of them across a dense range it emits a jump table, otherwise |
| 3 | +// it tests intervals. That count depends on how far the term has been |
| 4 | +// normalized when it arrives, because normalization merges arms that were |
| 5 | +// written apart. |
| 6 | +// |
| 7 | +// Folding now happens at construction, so the switcher sees merged arms. |
| 8 | +// These two cases are what that cost, and bought, when mk_builtin started |
| 9 | +// folding: across the runtime, Belt and every other test module the output |
| 10 | +// was unchanged, and only these two moved. Both plans are correct in each |
| 11 | +// case; only the emitted code differs. |
| 12 | +// |
| 13 | +// They stay here because the same sensitivity applies to any future change in |
| 14 | +// where normalization happens, and to the switcher's own thresholds - see the |
| 15 | +// `dense` predicate in switch.ml, where `switch_min` is what refuses the jump |
| 16 | +// table below. |
| 17 | + |
| 18 | +// Improvement: `10 + 10` merges with the two `20` arms, so three actions |
| 19 | +// become one, and four branches with `20` and `99` each duplicated collapse |
| 20 | +// to two branches with neither duplicated. |
| 21 | +let improves_when_merged = value => |
| 22 | + switch value { |
| 23 | + | 1 => 10 + 10 |
| 24 | + | 2 => 20 |
| 25 | + | 3 => 20 |
| 26 | + | _ => 99 |
| 27 | + } |
| 28 | + |
| 29 | +// Regression: the same merge drops the test count from three to two, below |
| 30 | +// `switch_min`, so `dense` refuses the jump table and this becomes a chain of |
| 31 | +// comparisons. The density check itself still passes; it is the minimum-tests |
| 32 | +// floor that rejects it. |
| 33 | +let effect = s => Console.log(s) |
| 34 | + |
| 35 | +let regresses_when_merged = x => |
| 36 | + switch x { |
| 37 | + | 1 => effect("d") |
| 38 | + | 2 => effect("d") |
| 39 | + | 3 => |
| 40 | + if 3 > 2 { |
| 41 | + effect("d") |
| 42 | + } else { |
| 43 | + effect("z") |
| 44 | + } |
| 45 | + | 4 => effect("q") |
| 46 | + | 5 => effect("r") |
| 47 | + | _ => effect("s") |
| 48 | + } |
0 commit comments