Skip to content

Check large switches for unused cases without comparing every pair - #9261

Draft
cknitt wants to merge 2 commits into
masterfrom
switch-unused-check-perf
Draft

cknitt wants to merge 2 commits into
masterfrom
switch-unused-check-perf

Conversation

@cknitt

@cknitt cknitt commented Nov 7, 2026

Copy link
Copy Markdown
Member

Part of #9256 (unused-case check of large switches).

Since #8900 removed the hash tables, the unused-case check of a switch with n constant or polymorphic variant cases is quadratic in several places:

  • check_unused filters all earlier rows for each case;
  • get_mins compares all pairs of the rows it is given;
  • filter_all finds a row's group by scanning all groups (this dominated: the last | _ row is checked against all n cases);
  • for polymorphic variants, each case's uncovered part is typed again against the whole n-tag type, to look for cases that GADTs make unreachable.

Rows are now indexed by the constant or tag at their head, in a Map ordered by const_compare, so no hashing key needs to agree with it. Two such heads are compatible, and one includes the other, only when they are equal, so a row with one is only compared with the rows of the same head and the rows whose head has none (wildcards, ranges, or-patterns, constructors). A range with equal bounds can share a group with a constant, so filter_all also looks through the groups without a key. get_mins_pats replaces get_mins le_pats in all three callers, including the exhaustiveness check and Matching.as_matrix. The GADT check now runs only when the pattern has a GADT constructor, since without one the pattern built from the case's own subpatterns always types.

Measured with bsc on linux-arm64, allocated words and wall time:

Switch Before After
3,000 int cases 111M, 437 ms 23.4M, 172 ms
6,000 int cases 430M, 1.65 s 73.7M, 555 ms
800 polymorphic variant cases 58.3M, 298 ms 31.8M, 182 ms
1,600 polymorphic variant cases 242M, 1.31 s 128M, 735 ms

Warnings are unchanged for the test suites and 2,630 generated switches over ints, chars, floats, strings, options, tuples, polymorphic variants and GADTs, with ranges, or-patterns, aliases and guards.

What remains: the match compiler (Matching.divide, Matching.add, the switch clustering) for constant switches, which grows 3.4× per doubling with the warnings off.

New test: warning_11_unused_constants covers a constant after an equal-bounds range, constants inside or-patterns, strings and polymorphic variants with payloads.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gK1dcjbe97yY1wRMgs

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • Speed up the unused-case check of switches with many constant or polymorphic variant cases.

Semantic Diff

2 commits · Full page

Check large switches for unused cases without comparing every pair

5d69ca2 · cknitt + Claude · Nov 07, 2026, 10:11 UTC

Why

The unused-case check compared each case with every earlier one and retyped part of each case, and its usefulness checks grouped rows by scanning all groups, so a switch with n constant or polymorphic variant cases cost O(n²) in several places: 6,000 int cases took 1.65 s and allocated 430M words, 1,600 polymorphic variant cases 1.31 s and 242M. They now take 0.56 s with 74M words, and 0.74 s with 128M.

Rows are indexed by the constant or tag at their head, which a row can only share with rows of the same head or of a head without one, such as a wildcard or a range: the earlier rows compatible with a case, the groups of filter_all and the most general rows of get_mins_pats are found through that index. A case is retyped to find GADT cases that can't match only when its pattern has a GADT constructor, as no other pattern can fail to type. Warnings are the same for the test suites and 2,600 generated switches.

Review

  • What remains superlinear? The match compiler (Matching.divide and the switch clustering) for constant switches: 6,000 int cases allocate 69M words with the warnings off, 3.4× the words for 3,000.
✨ Skip the row index for matches with one case

063dd71 · cknitt + Claude · Nov 07, 2026, 19:07 UTC · new

The benchmarks showed small matches, such as function parameters and let patterns, allocating up to 0.6% more for the index. get_mins_pats now returns fewer than two rows as they are, and the unused-case check doesn't index the last case, so such matches allocate less than before.

@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.78082% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.47%. Comparing base (5e5a3a7) to head (cc32a53).

Files with missing lines Patch % Lines
compiler/typing/parmatch.ml 91.66% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9261      +/-   ##
==========================================
- Coverage   82.47%   82.47%   -0.01%     
==========================================
  Files         466      466              
  Lines       66070    66132      +62     
==========================================
+ Hits        54493    54542      +49     
- Misses      11577    11590      +13     
Files with missing lines Coverage Δ
compiler/typing/matching.ml 79.90% <100.00%> (ø)
compiler/typing/parmatch.ml 78.03% <91.66%> (+0.20%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9261

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9261

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9261

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9261

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9261

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9261

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9261

commit: cc32a53

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

@cknitt
cknitt force-pushed the switch-unused-check-perf branch from 5cf0013 to 5d69ca2 Compare November 7, 2026 18:06
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

🎉 Compiler benchmarks

Allocated words

In total: 456,989,425 → 426,589,061 (-6.65%).

🎉 Improvement: 35 benchmarks allocate more than 1% less.

Benchmark Base Head Change
stress/IntSwitch.res 30,067,493 8,432,773 -71.95%
stress/PolyVariantSwitch.res 14,563,254 8,393,105 -42.37%
packages/@rescript/belt/src/Belt_internalAVLtree.res 4,426,588 4,116,151 -7.01%
packages/@rescript/runtime/Stdlib_Result.res 1,903,060 1,772,903 -6.84%
packages/@rescript/belt/src/Belt_internalAVLset.res 3,657,958 3,457,391 -5.48%
packages/@rescript/belt/src/Belt_MapDict.res 2,359,280 2,242,544 -4.95%
packages/@rescript/belt/src/Belt_internalMapInt.res 2,090,736 1,987,320 -4.95%
packages/@rescript/belt/src/Belt_internalMapString.res 2,092,574 1,989,156 -4.94%
packages/@rescript/belt/src/Belt_MutableSetInt.res 2,313,192 2,205,605 -4.65%
packages/@rescript/belt/src/Belt_MutableSetString.res 2,314,432 2,206,807 -4.65%
50 more changes
Benchmark Base Head Change
packages/@rescript/belt/src/Belt_MapInt.res 1,816,494 1,741,069 -4.15%
packages/@rescript/belt/src/Belt_MapString.res 1,817,038 1,741,613 -4.15%
packages/@rescript/belt/src/Belt_MutableMapInt.res 1,773,700 1,701,156 -4.09%
packages/@rescript/belt/src/Belt_MutableMapString.res 1,773,969 1,701,439 -4.09%
packages/@rescript/belt/src/Belt_MutableSet.res 2,609,727 2,505,185 -4.01%
packages/@rescript/belt/src/Belt_SetInt.res 1,895,032 1,820,424 -3.94%
packages/@rescript/belt/src/Belt_SetString.res 1,895,759 1,821,149 -3.94%
packages/@rescript/belt/src/Belt_SetDict.res 2,121,382 2,046,337 -3.54%
packages/@rescript/belt/src/Belt_MutableMap.res 1,941,992 1,879,876 -3.20%
packages/@rescript/belt/src/Belt_HashMapInt.res 1,671,321 1,624,264 -2.82%
packages/@rescript/belt/src/Belt_HashMapString.res 1,672,033 1,624,972 -2.81%
packages/@rescript/belt/src/Belt_HashMap.res 1,848,233 1,801,927 -2.51%
packages/@rescript/belt/src/Belt_internalSetInt.res 1,267,668 1,236,057 -2.49%
packages/@rescript/belt/src/Belt_internalSetString.res 1,268,854 1,237,243 -2.49%
packages/@rescript/runtime/Stdlib_Option.res 1,288,904 1,257,681 -2.42%
packages/@rescript/belt/src/Belt_Result.res 920,631 898,770 -2.37%
packages/@rescript/belt/src/Belt_internalBuckets.res 1,556,445 1,519,960 -2.34%
packages/@rescript/belt/src/Belt_MutableQueue.res 1,328,287 1,298,722 -2.23%
packages/@rescript/runtime/Stdlib_List.res 3,396,284 3,327,528 -2.02%
packages/@rescript/belt/src/Belt_List.res 3,950,091 3,871,324 -1.99%
packages/@rescript/belt/src/Belt_HashSetInt.res 1,412,456 1,385,871 -1.88%
packages/@rescript/belt/src/Belt_HashSetString.res 1,412,763 1,386,182 -1.88%
packages/@rescript/belt/src/Belt_HashSet.res 1,573,656 1,547,629 -1.65%
packages/@rescript/belt/src/Belt_MutableStack.res 893,146 879,170 -1.56%
packages/@rescript/belt/src/Belt_internalSetBuckets.res 1,239,575 1,224,945 -1.18%
packages/@rescript/belt/src/Belt_Option.res 885,745 878,452 -0.82%
packages/@rescript/runtime/Primitive_object.res 1,720,232 1,712,180 -0.47%
stress/InlineChain.res 3,977,341 3,960,708 -0.42%
packages/@rescript/runtime/Primitive_module.res 834,826 831,557 -0.39%
stress/LetRec.res 35,120,632 35,008,799 -0.32%
packages/@rescript/belt/src/Belt_Array.res 2,842,879 2,834,295 -0.30%
packages/@rescript/runtime/Stdlib_Nullable.res 828,570 826,073 -0.30%
packages/@rescript/runtime/Stdlib_Null.res 830,463 827,966 -0.30%
packages/@rescript/runtime/Stdlib_AsyncIterableIterator.res 719,827 717,742 -0.29%
packages/@rescript/runtime/Stdlib_AsyncIterator.res 792,369 790,142 -0.28%
packages/@rescript/runtime/Primitive_hash.res 1,501,651 1,497,433 -0.28%
packages/@rescript/runtime/Stdlib_JSON.res 784,466 782,324 -0.27%
packages/@rescript/runtime/Stdlib_Array.res 1,864,605 1,859,527 -0.27%
packages/@rescript/runtime/Primitive_list.res 690,682 688,935 -0.25%
packages/@rescript/runtime/Stdlib_Type.res 682,516 684,231 +0.25%
packages/@rescript/belt/src/Belt_SortArray.res 2,045,354 2,040,601 -0.23%
packages/@rescript/belt/src/Belt_Map.res 1,561,134 1,557,533 -0.23%
packages/@rescript/runtime/Stdlib_Int.res 953,023 950,980 -0.21%
packages/@rescript/belt/src/Belt_Set.res 1,419,186 1,416,156 -0.21%
packages/@rescript/belt/src/Belt_SortArrayInt.res 1,929,237 1,925,307 -0.20%
packages/@rescript/belt/src/Belt_SortArrayString.res 1,931,027 1,927,097 -0.20%
packages/@rescript/runtime/Primitive_bool.res 626,751 625,599 -0.18%
packages/@rescript/runtime/Stdlib_Intl_NumberFormat_Grouping.res 638,049 637,072 -0.15%
packages/@rescript/runtime/Primitive_option.res 793,631 792,479 -0.15%
packages/@rescript/belt/src/Belt_Range.res 780,043 779,258 -0.10%

Instructions

In total: 10,226,962,100 → 8,709,795,247 (-14.83%).

🎉 Improvement: 4 benchmarks execute more than 1% fewer instructions.

Benchmark Base Head Change
stress/IntSwitch.res 1,742,110,561 517,655,291 -70.29%
stress/PolyVariantSwitch.res 645,608,631 364,414,154 -43.55%
packages/@rescript/belt/src/Belt_internalAVLtree.res 166,149,547 155,368,941 -6.49%
packages/@rescript/belt/src/Belt_List.res 145,580,923 143,146,416 -1.67%
stress/ObjectFields.res 957,097,806 958,457,576 +0.14%
packages/@rescript/runtime/Stdlib_Math.resi 44,160,992 44,216,549 +0.13%
packages/@rescript/runtime/Stdlib_List.resi 47,643,168 47,700,067 +0.12%
parse/RedBlackTree.res 26,360,354 26,390,088 +0.11%
packages/@rescript/runtime/Stdlib_Date.resi 57,209,106 57,269,898 +0.11%
parse/RedBlackTreeNoComments.res 25,954,530 25,981,406 +0.10%

Pull request 063dd71 compared with base bf35764 in this run.

@cknitt
cknitt force-pushed the switch-unused-check-perf branch from 063dd71 to d0f2c46 Compare November 14, 2026 19:53
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • Speed up the unused-case check of switches with many constant or polymorphic variant cases.

Semantic Diff

2 commits · Full page

Check large switches for unused cases without comparing every pair

78f0592 · cknitt + Claude · Nov 07, 2026, 10:11 UTC

Why

The unused-case check compared each case with every earlier one and retyped part of each case, and its usefulness checks grouped rows by scanning all groups, so a switch with n constant or polymorphic variant cases cost O(n²) in several places: 6,000 int cases took 1.65 s and allocated 430M words, 1,600 polymorphic variant cases 1.31 s and 242M. They now take 0.56 s with 74M words, and 0.74 s with 128M.

Rows are indexed by the constant or tag at their head, which a row can only share with rows of the same head or of a head without one, such as a wildcard or a range: the earlier rows compatible with a case, the groups of filter_all and the most general rows of get_mins_pats are found through that index. A case is retyped to find GADT cases that can't match only when its pattern has a GADT constructor, as no other pattern can fail to type. Warnings are the same for the test suites and 2,600 generated switches.

Review

  • What remains superlinear? The match compiler (Matching.divide and the switch clustering) for constant switches: 6,000 int cases allocate 69M words with the warnings off, 3.4× the words for 3,000.
Skip the row index for matches with one case

cc32a53 · cknitt + Claude · Nov 07, 2026, 19:07 UTC

The benchmarks showed small matches, such as function parameters and let patterns, allocating up to 0.6% more for the index. get_mins_pats now returns fewer than two rows as they are, and the unused-case check doesn't index the last case, so such matches allocate less than before.

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

🎉 Compiler benchmarks

Allocated words

In total: 674,904,642 → 642,381,151 (-4.82%).

Generated projects: 630,567,032 → 630,209,697 (-0.06%).

🎉 Improvement: 36 benchmarks allocate more than 1% less.

Benchmark Base Head Change
stress/IntSwitch.res 29,766,446 8,128,726 -72.69%
stress/PolyVariantSwitch.res 14,274,592 8,105,241 -43.22%
packages/@rescript/belt/src/Belt_internalAVLtree.res 3,510,171 3,252,335 -7.35%
packages/@rescript/runtime/Stdlib_Result.res 1,273,829 1,181,758 -7.23%
packages/@rescript/belt/src/Belt_internalAVLset.res 2,882,050 2,709,271 -6.00%
packages/@rescript/belt/src/Belt_internalMapInt.res 1,409,657 1,328,010 -5.79%
packages/@rescript/belt/src/Belt_internalMapString.res 1,411,501 1,329,793 -5.79%
packages/@rescript/belt/src/Belt_MapDict.res 1,635,172 1,542,140 -5.69%
packages/@rescript/belt/src/Belt_MutableSetInt.res 1,662,857 1,573,832 -5.35%
packages/@rescript/belt/src/Belt_MutableSetString.res 1,664,103 1,575,072 -5.35%
193 more changes
Benchmark Base Head Change
packages/@rescript/belt/src/Belt_MapInt.res 1,185,508 1,125,607 -5.05%
packages/@rescript/belt/src/Belt_MapString.res 1,186,044 1,126,143 -5.05%
packages/@rescript/belt/src/Belt_MutableMapInt.res 1,154,189 1,096,417 -5.01%
packages/@rescript/belt/src/Belt_MutableMapString.res 1,154,468 1,096,696 -5.00%
packages/@rescript/belt/src/Belt_SetInt.res 1,293,080 1,231,265 -4.78%
packages/@rescript/belt/src/Belt_SetString.res 1,293,809 1,231,988 -4.78%
packages/@rescript/belt/src/Belt_MutableSet.res 1,900,871 1,814,075 -4.57%
packages/@rescript/belt/src/Belt_SetDict.res 1,449,423 1,387,522 -4.27%
packages/@rescript/belt/src/Belt_Result.res 410,356 394,209 -3.93%
packages/@rescript/belt/src/Belt_MutableMap.res 1,302,107 1,252,191 -3.83%
packages/@rescript/belt/src/Belt_HashMapInt.res 1,062,335 1,023,885 -3.62%
packages/@rescript/belt/src/Belt_HashMapString.res 1,063,043 1,024,595 -3.62%
packages/@rescript/belt/src/Belt_internalSetInt.res 731,748 705,707 -3.56%
packages/@rescript/belt/src/Belt_internalSetString.res 732,939 706,898 -3.55%
packages/@rescript/belt/src/Belt_MutableQueue.res 792,242 766,657 -3.23%
packages/@rescript/runtime/Stdlib_Option.res 757,501 733,264 -3.20%
packages/@rescript/belt/src/Belt_internalBuckets.res 970,123 939,157 -3.19%
packages/@rescript/belt/src/Belt_HashMap.res 1,201,622 1,163,496 -3.17%
packages/@rescript/belt/src/Belt_MutableStack.res 388,072 376,085 -3.09%
packages/@rescript/belt/src/Belt_HashSetInt.res 851,911 829,360 -2.65%
packages/@rescript/belt/src/Belt_HashSetString.res 852,233 829,675 -2.65%
packages/@rescript/runtime/Stdlib_List.res 2,693,088 2,631,520 -2.29%
packages/@rescript/belt/src/Belt_HashSet.res 979,582 957,295 -2.28%
packages/@rescript/belt/src/Belt_List.res 3,182,676 3,112,675 -2.20%
packages/@rescript/belt/src/Belt_internalSetBuckets.res 702,145 689,268 -1.83%
packages/@rescript/belt/src/Belt_Option.res 389,772 383,652 -1.57%
packages/@rescript/runtime/Primitive_module.res 337,202 333,963 -0.96%
packages/@rescript/runtime/Stdlib_Type.res 201,856 203,555 +0.84%
packages/@rescript/runtime/Stdlib_AsyncIterableIterator.res 229,132 227,336 -0.78%
packages/@rescript/runtime/Primitive_list.res 204,184 202,606 -0.77%
build/clean 260,349,062 258,364,797 -0.76%
packages/@rescript/runtime/Stdlib_JSON.res 297,732 295,578 -0.72%
packages/@rescript/runtime/Primitive_bool.res 147,472 146,482 -0.67%
packages/@rescript/runtime/Stdlib_Nullable.res 332,940 330,737 -0.66%
build/shared-edit 87,818,662 87,238,463 -0.66%
packages/@rescript/runtime/Stdlib_Null.res 335,436 333,233 -0.66%
packages/@rescript/runtime/Stdlib_AsyncIterator.res 295,123 293,185 -0.66%
packages/@rescript/runtime/Stdlib_Intl_NumberFormat_Grouping.res 159,721 158,784 -0.59%
packages/@rescript/runtime/Primitive_object.res 1,157,633 1,151,624 -0.52%
packages/@rescript/runtime/Primitive_list.resi 92,109 92,555 +0.48%
packages/@rescript/runtime/Primitive_float_extern.res 93,969 94,415 +0.47%
packages/@rescript/runtime/Stdlib_Blob.res 94,186 94,632 +0.47%
stress/InlineChain.res 3,559,996 3,543,363 -0.47%
packages/@rescript/runtime/Stdlib_File.res 95,995 96,441 +0.46%
packages/@rescript/runtime/Primitive_promise.res 97,345 97,791 +0.46%
packages/@rescript/runtime/Primitive_int_extern.res 98,198 98,644 +0.45%
packages/@rescript/runtime/Stdlib_Iterable.resi 98,315 98,761 +0.45%
packages/@rescript/runtime/Stdlib_AsyncIterable.resi 98,464 98,910 +0.45%
packages/@rescript/runtime/Primitive_string_extern.res 98,506 98,952 +0.45%
packages/@rescript/runtime/Stdlib_Iterable.res 99,226 99,672 +0.45%
packages/@rescript/runtime/Stdlib_AsyncIterable.res 99,297 99,743 +0.45%
packages/@rescript/runtime/Obj.res 99,446 99,892 +0.45%
packages/@rescript/runtime/Primitive_exceptions.resi 100,537 100,983 +0.44%
packages/@rescript/runtime/Primitive_hash.resi 102,954 103,400 +0.43%
packages/@rescript/runtime/Primitive_dict.res 102,998 103,444 +0.43%
packages/@rescript/runtime/Primitive_module.resi 103,396 103,842 +0.43%
packages/@rescript/runtime/RescriptTools.res 104,869 105,315 +0.43%
packages/@rescript/runtime/Stdlib_TaggedTemplate.resi 106,280 106,726 +0.42%
packages/@rescript/runtime/Stdlib_ArrayBuffer.res 107,822 108,268 +0.41%
packages/@rescript/runtime/Stdlib_Int.res 455,641 453,782 -0.41%
packages/@rescript/runtime/Stdlib_ArrayBuffer.resi 111,078 111,524 +0.40%
packages/@rescript/runtime/Primitive_hash.res 963,863 959,999 -0.40%
packages/@rescript/runtime/Stdlib_Lazy.resi 112,370 112,816 +0.40%
packages/@rescript/runtime/RescriptTools_ExtractCodeBlocks.res 112,422 112,868 +0.40%
packages/@rescript/belt/src/Belt_Array.res 2,155,725 2,147,183 -0.40%
packages/@rescript/runtime/Primitive_array_extern.res 114,506 114,952 +0.39%
packages/@rescript/runtime/Primitive_object_extern.res 115,017 115,463 +0.39%
packages/@rescript/runtime/Stdlib_Array.res 1,284,814 1,279,832 -0.39%
packages/@rescript/runtime/Stdlib_Bool.resi 116,590 117,036 +0.38%
packages/@rescript/runtime/Stdlib_Intl_Segments.res 116,885 117,331 +0.38%
packages/@rescript/runtime/Primitive_object.resi 117,311 117,757 +0.38%
packages/@rescript/runtime/Stdlib_TaggedTemplate.res 115,760 116,193 +0.37%
packages/@rescript/runtime/Dom_storage2.res 120,471 120,917 +0.37%
packages/@rescript/belt/src/Belt_Map.res 981,032 977,425 -0.37%
packages/@rescript/runtime/Jsx.res 122,026 122,472 +0.37%
packages/@rescript/runtime/Primitive_option.resi 122,401 122,847 +0.36%
packages/@rescript/runtime/Stdlib_JsExn.resi 122,487 122,933 +0.36%
packages/@rescript/belt/src/Belt_Set.res 857,736 854,618 -0.36%
packages/@rescript/runtime/Primitive_option.res 298,158 297,075 -0.36%
packages/@rescript/runtime/Stdlib_WeakSet.res 123,458 123,904 +0.36%
stress/LetRec.res 31,641,992 31,530,159 -0.35%
packages/@rescript/runtime/Stdlib_WeakMap.res 135,790 136,236 +0.33%
packages/@rescript/runtime/Stdlib_Type.resi 135,942 136,388 +0.33%
packages/@rescript/belt/src/Belt_SortArray.res 1,448,739 1,443,990 -0.33%
packages/@rescript/runtime/Stdlib_Global.res 142,114 142,560 +0.31%
packages/@rescript/runtime/Stdlib_Temporal_Now.res 143,782 144,228 +0.31%
packages/@rescript/runtime/Stdlib_Bool.res 146,673 147,121 +0.31%
packages/@rescript/runtime/Stdlib_Symbol.res 146,811 147,257 +0.30%
packages/@rescript/runtime/Stdlib_IterableIterator.resi 149,163 149,609 +0.30%
packages/@rescript/belt/src/Belt_Int.resi 149,428 149,874 +0.30%
packages/@rescript/belt/src/Belt_Float.resi 150,908 151,354 +0.30%
packages/@rescript/belt/src/Belt_SortArrayInt.res 1,343,380 1,339,448 -0.29%
packages/@rescript/belt/src/Belt_SortArrayString.res 1,345,170 1,341,246 -0.29%
packages/@rescript/runtime/Stdlib_Intl_Common.res 154,641 155,087 +0.29%
packages/@rescript/runtime/Stdlib_Intl_Segmenter.res 154,826 155,272 +0.29%
packages/@rescript/runtime/Stdlib_Global.resi 155,424 155,870 +0.29%
packages/@rescript/runtime/Stdlib_Iterator.resi 158,762 159,208 +0.28%
packages/@rescript/runtime/Stdlib_AsyncIterableIterator.resi 159,631 160,077 +0.28%
packages/@rescript/runtime/Stdlib_Symbol.resi 159,945 160,391 +0.28%
packages/@rescript/runtime/Stdlib_Generator.resi 160,253 160,699 +0.28%
packages/@rescript/runtime/Stdlib_RegExp.res 164,051 164,497 +0.27%
packages/@rescript/belt/src/Belt_Range.res 289,195 288,410 -0.27%
packages/@rescript/belt/src/Belt_internalBucketsType.resi 164,926 165,372 +0.27%
packages/@rescript/runtime/Stdlib_AsyncGenerator.resi 165,631 166,077 +0.27%
packages/@rescript/belt/src/Belt_MutableStack.resi 169,059 169,505 +0.26%
packages/@rescript/belt/src/Belt_SortArrayString.resi 169,375 169,821 +0.26%
packages/@rescript/belt/src/Belt_SortArrayInt.resi 169,493 169,939 +0.26%
packages/@rescript/runtime/Stdlib_Intl_ListFormat.res 170,112 170,558 +0.26%
packages/@rescript/runtime/Stdlib_Intl.res 174,496 174,942 +0.26%
packages/@rescript/belt/src/Belt_HashSetString.resi 175,579 176,025 +0.25%
packages/@rescript/belt/src/Belt_HashSetInt.resi 175,692 176,138 +0.25%
packages/@rescript/belt/src/Belt_Range.resi 176,183 176,629 +0.25%
packages/@rescript/runtime/Stdlib_AsyncIterator.resi 177,264 177,710 +0.25%
packages/@rescript/runtime/Stdlib_Intl_Collator.res 178,949 179,395 +0.25%
packages/@rescript/runtime/Stdlib_Intl_RelativeTimeFormat.res 180,751 181,197 +0.25%
packages/@rescript/runtime/Stdlib_JsError.resi 184,185 184,631 +0.24%
packages/@rescript/runtime/Stdlib_BigInt.resi 185,473 185,919 +0.24%
packages/@rescript/runtime/Stdlib_RegExp.resi 187,643 188,089 +0.24%
packages/@rescript/belt/src/Belt_Float.res 165,331 165,722 +0.24%
packages/@rescript/belt/src/Belt.res 188,801 189,247 +0.24%
packages/@rescript/belt/src/Belt_Int.res 166,070 166,461 +0.24%
packages/@rescript/runtime/Stdlib_Float.resi 193,609 194,055 +0.23%
packages/@rescript/belt/src/Belt_internalSetBuckets.resi 194,448 194,894 +0.23%
packages/@rescript/runtime/Stdlib_Intl_Locale.res 198,363 198,809 +0.22%
packages/@rescript/runtime/Stdlib_Nullable.resi 198,836 199,282 +0.22%
packages/@rescript/runtime/Stdlib_Intl_PluralRules.res 202,178 202,624 +0.22%
packages/@rescript/runtime/Stdlib_Null.resi 203,543 203,989 +0.22%
packages/@rescript/runtime/Stdlib_Int.resi 205,591 206,037 +0.22%
packages/@rescript/belt/src/Belt_MutableQueue.resi 206,148 206,594 +0.22%
packages/@rescript/belt/src/Belt_HashMapString.resi 213,588 214,034 +0.21%
packages/@rescript/belt/src/Belt_HashMapInt.resi 213,702 214,148 +0.21%
packages/@rescript/runtime/Stdlib_Map.resi 216,838 217,284 +0.21%
packages/@rescript/belt/src/Belt_HashSet.resi 222,672 223,118 +0.20%
packages/@rescript/belt/src/Belt_internalBuckets.resi 223,228 223,674 +0.20%
packages/@rescript/runtime/Stdlib_Temporal.res 224,567 225,013 +0.20%
packages/@rescript/runtime/Stdlib_Set.resi 228,617 229,063 +0.20%
packages/@rescript/runtime/Stdlib_DataView.res 230,614 231,060 +0.19%
packages/@rescript/runtime/Stdlib_IteratorObject.resi 233,994 234,440 +0.19%
packages/@rescript/runtime/Stdlib_Option.resi 235,309 235,755 +0.19%
packages/@rescript/runtime/Stdlib_JSON.resi 235,736 236,182 +0.19%
packages/@rescript/runtime/Stdlib_Map.res 206,214 206,604 +0.19%
packages/@rescript/belt/src/Belt_Result.resi 239,344 239,790 +0.19%
packages/@rescript/runtime/Pervasives.res 240,136 240,582 +0.19%
packages/@rescript/runtime/Primitive_js_extern.res 240,162 240,608 +0.19%
packages/@rescript/runtime/Stdlib_Dict.resi 240,212 240,658 +0.19%
packages/@rescript/belt/src/Belt_Option.resi 241,658 242,104 +0.18%
packages/@rescript/runtime/Stdlib_Set.res 211,558 211,948 +0.18%
packages/@rescript/runtime/Stdlib_Object.res 247,689 248,135 +0.18%
packages/@rescript/belt/src/Belt_SortArray.resi 253,180 253,626 +0.18%
packages/@rescript/belt/src/Belt_SetString.resi 258,680 259,126 +0.17%
packages/@rescript/belt/src/Belt_SetInt.resi 258,807 259,253 +0.17%
packages/@rescript/belt/src/Belt_Id.resi 260,316 260,762 +0.17%
packages/@rescript/belt/src/Belt_MutableSetString.resi 264,006 264,452 +0.17%
packages/@rescript/belt/src/Belt_MutableSetInt.resi 264,123 264,569 +0.17%
packages/@rescript/runtime/Stdlib_DataView.resi 267,696 268,142 +0.17%
packages/@rescript/runtime/RescriptTools_Docgen.resi 273,746 274,192 +0.16%
packages/@rescript/runtime/RescriptTools_Docgen.res 283,979 284,425 +0.16%
packages/@rescript/runtime/Stdlib_Result.resi 295,049 295,495 +0.15%
packages/@rescript/belt/src/Belt_HashMap.resi 295,462 295,908 +0.15%
packages/@rescript/runtime/Stdlib_IterableIterator.res 179,669 179,926 +0.14%
packages/@rescript/belt/src/Belt_MutableMapString.resi 313,673 314,119 +0.14%
packages/@rescript/belt/src/Belt_MutableMapInt.resi 313,791 314,237 +0.14%
packages/@rescript/runtime/Stdlib_Math.resi 327,333 327,779 +0.14%
packages/@rescript/runtime/Stdlib_Int8Array.res 343,831 344,277 +0.13%
packages/@rescript/runtime/Stdlib_Uint8Array.res 343,844 344,290 +0.13%
packages/@rescript/runtime/Stdlib_Float32Array.res 343,951 344,397 +0.13%
packages/@rescript/runtime/Stdlib_Float64Array.res 343,951 344,397 +0.13%
packages/@rescript/runtime/Stdlib_Int16Array.res 343,976 344,422 +0.13%
packages/@rescript/runtime/Stdlib_Int32Array.res 343,978 344,424 +0.13%
packages/@rescript/runtime/Stdlib_Uint16Array.res 343,983 344,429 +0.13%
packages/@rescript/runtime/Stdlib_Uint32Array.res 343,983 344,429 +0.13%
packages/@rescript/runtime/Stdlib_BigInt64Array.res 344,008 344,454 +0.13%
packages/@rescript/runtime/Stdlib_BigUint64Array.res 344,085 344,531 +0.13%
packages/@rescript/runtime/Stdlib_Uint8ClampedArray.res 344,359 344,805 +0.13%
packages/@rescript/belt/src/Belt_internalAVLset.resi 354,811 355,257 +0.13%
packages/@rescript/runtime/Stdlib_Promise.resi 355,694 356,140 +0.13%
packages/@rescript/runtime/Stdlib_Temporal_Common.res 366,949 367,395 +0.12%
packages/@rescript/runtime/Stdlib_Console.res 370,515 370,961 +0.12%
packages/@rescript/runtime/Primitive_array.res 192,256 192,482 +0.12%
packages/@rescript/runtime/Stdlib_IteratorObject.res 251,466 251,757 +0.12%
packages/@rescript/belt/src/Belt_MapString.resi 389,490 389,936 +0.11%
packages/@rescript/belt/src/Belt_MapInt.resi 389,533 389,979 +0.11%
packages/@rescript/runtime/Stdlib_Intl_DateTimeFormat.res 389,888 390,334 +0.11%
packages/@rescript/belt/src/Belt_MutableMap.resi 394,186 394,632 +0.11%
packages/@rescript/belt/src/Belt_MutableSet.resi 395,038 395,484 +0.11%
packages/@rescript/belt/src/Belt_SetDict.resi 400,250 400,696 +0.11%
packages/@rescript/runtime/Stdlib_Intl_NumberFormat.res 405,144 405,590 +0.11%
packages/@rescript/runtime/Stdlib_List.resi 406,435 406,881 +0.11%
packages/@rescript/runtime/Stdlib_Console.resi 410,791 411,237 +0.11%
packages/@rescript/runtime/Stdlib_Ordering.res 155,305 155,471 +0.11%
packages/@rescript/runtime/Stdlib_BigInt.res 207,630 207,848 +0.10%
packages/@rescript/belt/src/Belt_Set.resi 434,798 435,244 +0.10%
packages/@rescript/runtime/Primitive_float.res 147,214 147,363 +0.10%

Instructions

In total: 9,225,173,296 → 7,710,967,446 (-16.41%).

🎉 Improvement: 4 benchmarks execute more than 1% fewer instructions.

Benchmark Base Head Change
stress/IntSwitch.res 1,720,955,023 499,864,422 -70.95%
stress/PolyVariantSwitch.res 634,010,776 353,290,621 -44.28%
packages/@rescript/belt/src/Belt_internalAVLtree.res 136,452,273 126,661,675 -7.18%
packages/@rescript/belt/src/Belt_List.res 118,212,318 115,904,804 -1.95%
packages/@rescript/runtime/Stdlib_Math.resi 21,248,775 21,294,678 +0.22%
packages/@rescript/runtime/Stdlib_List.resi 24,486,964 24,530,889 +0.18%
parse/RedBlackTreeNoComments.res 14,514,931 14,539,229 +0.17%
packages/@rescript/belt/src/Belt_Array.resi 33,643,654 33,698,386 +0.16%
packages/@rescript/runtime/Stdlib_String.resi 29,394,025 29,438,943 +0.15%
packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res 24,092,374 24,128,023 +0.15%
6 more changes
Benchmark Base Head Change
parse/RedBlackTree.res 14,925,884 14,947,781 +0.15%
packages/@rescript/belt/src/Belt_List.resi 38,858,402 38,911,252 +0.14%
packages/@rescript/runtime/Stdlib_Date.resi 34,193,967 34,237,347 +0.13%
packages/@rescript/runtime/Stdlib_Array.resi 39,996,691 40,046,970 +0.13%
packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res 31,205,668 31,242,839 +0.12%
packages/@rescript/runtime/Stdlib_TypedArray.res 41,345,714 41,387,648 +0.10%

Output size

In total: 27,368,415 → 27,371,106 (+0.01%).

Benchmark Base Head Change
output/playground/compiler.js 1,899,363 1,901,314 +0.10%

Pull request cc32a53 compared with base 5e5a3a7 in this run.

@github-actions

Copy link
Copy Markdown

@cknitt
cknitt force-pushed the switch-unused-check-perf branch 2 times, most recently from 136c8f1 to cd8e26f Compare November 23, 2026 06:47
cknitt and others added 2 commits November 30, 2026 09:31
Why:
    The unused-case check compared each case with every earlier one and
    retyped part of each case, and its usefulness checks grouped rows by
    scanning all groups, so a switch with n constant or polymorphic
    variant cases cost O(n²) in several places: 6,000 int cases took
    1.65 s and allocated 430M words, 1,600 polymorphic variant cases
    1.31 s and 242M. They now take 0.56 s with 74M words, and 0.74 s with
    128M.

Rows are indexed by the constant or tag at their head, which a row can
only share with rows of the same head or of a head without one, such as a
wildcard or a range: the earlier rows compatible with a case, the groups
of `filter_all` and the most general rows of `get_mins_pats` are found
through that index. A case is retyped to find GADT cases that can't match
only when its pattern has a GADT constructor, as no other pattern can fail
to type. Warnings are the same for the test suites and 2,600 generated
switches.

Review: What remains superlinear? The match compiler (`Matching.divide`
and the switch clustering) for constant switches: 6,000 int cases allocate
69M words with the warnings off, 3.4× the words for 3,000.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gK1dcjbe97yY1wRMgs
Signed-off-by: Christoph Knittel <ck@cca.io>
The benchmarks showed small matches, such as function parameters and let
patterns, allocating up to 0.6% more for the index. get_mins_pats now
returns fewer than two rows as they are, and the unused-case check doesn't
index the last case, so such matches allocate less than before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gK1dcjbe97yY1wRMgs
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the switch-unused-check-perf branch from cd8e26f to cc32a53 Compare November 30, 2026 09:44

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant