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.21%. Comparing base (643e9c0) to head (cd8e26f).

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.21%   82.21%           
=======================================
  Files         460      460           
  Lines       65551    65613   +62     
=======================================
+ Hits        53891    53942   +51     
- Misses      11660    11671   +11     
Files with missing lines Coverage Δ
compiler/typing/matching.ml 78.50% <100.00%> (ø)
compiler/typing/parmatch.ml 78.03% <91.66%> (+0.20%) ⬆️

... and 1 file 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: cd8e26f

@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

ad22c8d · 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

cd8e26f · 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: 636,509,752 → 604,153,799 (-5.08%).

Generated projects: 629,030,159 → 628,671,814 (-0.06%).

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

Benchmark Base Head Change
stress/IntSwitch.res 29,766,198 8,128,478 -72.69%
stress/PolyVariantSwitch.res 14,274,344 8,104,993 -43.22%
packages/@rescript/belt/src/Belt_internalAVLtree.res 3,506,403 3,248,567 -7.35%
packages/@rescript/runtime/Stdlib_Result.res 1,268,255 1,176,184 -7.26%
packages/@rescript/belt/src/Belt_internalAVLset.res 2,876,034 2,703,342 -6.00%
packages/@rescript/belt/src/Belt_internalMapInt.res 1,408,499 1,326,791 -5.80%
packages/@rescript/belt/src/Belt_internalMapString.res 1,410,343 1,328,696 -5.79%
packages/@rescript/belt/src/Belt_MapDict.res 1,630,823 1,537,767 -5.71%
packages/@rescript/belt/src/Belt_MutableSetInt.res 1,664,555 1,575,429 -5.35%
packages/@rescript/belt/src/Belt_MutableSetString.res 1,665,801 1,576,728 -5.35%
194 more changes
Benchmark Base Head Change
packages/@rescript/belt/src/Belt_MapInt.res 1,181,363 1,121,460 -5.07%
packages/@rescript/belt/src/Belt_MapString.res 1,181,899 1,121,996 -5.07%
packages/@rescript/belt/src/Belt_MutableMapInt.res 1,154,742 1,096,961 -5.00%
packages/@rescript/belt/src/Belt_MutableMapString.res 1,155,019 1,097,242 -5.00%
packages/@rescript/belt/src/Belt_SetInt.res 1,289,624 1,227,803 -4.79%
packages/@rescript/belt/src/Belt_SetString.res 1,290,353 1,228,532 -4.79%
packages/@rescript/belt/src/Belt_MutableSet.res 1,902,475 1,815,618 -4.57%
packages/@rescript/belt/src/Belt_SetDict.res 1,445,985 1,384,082 -4.28%
packages/@rescript/belt/src/Belt_Result.res 409,437 393,290 -3.94%
packages/@rescript/belt/src/Belt_MutableMap.res 1,302,290 1,252,359 -3.83%
packages/@rescript/belt/src/Belt_HashMapInt.res 1,060,707 1,022,259 -3.62%
packages/@rescript/belt/src/Belt_HashMapString.res 1,061,419 1,022,967 -3.62%
packages/@rescript/belt/src/Belt_internalSetInt.res 730,723 704,682 -3.56%
packages/@rescript/belt/src/Belt_internalSetString.res 731,914 705,873 -3.56%
packages/@rescript/belt/src/Belt_MutableQueue.res 791,334 765,749 -3.23%
packages/@rescript/runtime/Stdlib_Option.res 753,633 729,394 -3.22%
packages/@rescript/belt/src/Belt_internalBuckets.res 966,624 935,671 -3.20%
packages/@rescript/belt/src/Belt_HashMap.res 1,200,049 1,161,929 -3.18%
packages/@rescript/belt/src/Belt_MutableStack.res 387,514 375,527 -3.09%
packages/@rescript/belt/src/Belt_HashSetInt.res 850,520 827,962 -2.65%
packages/@rescript/belt/src/Belt_HashSetString.res 850,837 828,286 -2.65%
packages/@rescript/runtime/Stdlib_List.res 2,686,422 2,624,844 -2.29%
packages/@rescript/belt/src/Belt_HashSet.res 978,208 955,921 -2.28%
packages/@rescript/belt/src/Belt_List.res 3,175,359 3,105,358 -2.20%
packages/@rescript/belt/src/Belt_internalSetBuckets.res 699,079 686,206 -1.84%
packages/@rescript/belt/src/Belt_Option.res 389,121 383,001 -1.57%
packages/@rescript/runtime/Primitive_module.res 337,540 334,301 -0.96%
packages/@rescript/runtime/Stdlib_Type.res 201,438 203,137 +0.84%
build/clean 236,610,604 234,689,794 -0.81%
packages/@rescript/runtime/Stdlib_AsyncIterableIterator.res 228,325 226,530 -0.79%
packages/@rescript/runtime/Primitive_list.res 203,745 202,167 -0.77%
packages/@rescript/runtime/Stdlib_JSON.res 296,903 294,749 -0.73%
build/shared-edit 72,589,510 72,068,952 -0.72%
packages/@rescript/runtime/Primitive_bool.res 147,218 146,228 -0.67%
packages/@rescript/runtime/Stdlib_Nullable.res 329,857 327,654 -0.67%
packages/@rescript/runtime/Stdlib_Null.res 332,359 330,156 -0.66%
packages/@rescript/runtime/Stdlib_AsyncIterator.res 294,756 292,818 -0.66%
packages/@rescript/runtime/Stdlib_Intl_NumberFormat_Grouping.res 159,421 158,484 -0.59%
packages/@rescript/runtime/Primitive_object.res 1,154,866 1,148,853 -0.52%
packages/@rescript/runtime/Primitive_list.resi 91,887 92,333 +0.49%
packages/@rescript/runtime/Primitive_float_extern.res 93,687 94,133 +0.48%
packages/@rescript/runtime/Stdlib_Blob.res 93,905 94,351 +0.47%
stress/InlineChain.res 3,511,206 3,494,573 -0.47%
packages/@rescript/runtime/Stdlib_File.res 95,714 96,160 +0.47%
packages/@rescript/runtime/Primitive_promise.res 97,062 97,508 +0.46%
packages/@rescript/runtime/Primitive_int_extern.res 97,915 98,361 +0.46%
packages/@rescript/runtime/Stdlib_Iterable.resi 98,169 98,615 +0.45%
packages/@rescript/runtime/Primitive_string_extern.res 98,223 98,669 +0.45%
packages/@rescript/runtime/Stdlib_AsyncIterable.resi 98,318 98,764 +0.45%
packages/@rescript/runtime/Stdlib_Iterable.res 98,944 99,390 +0.45%
packages/@rescript/runtime/Stdlib_AsyncIterable.res 99,015 99,461 +0.45%
packages/@rescript/runtime/Obj.res 99,164 99,610 +0.45%
packages/@rescript/runtime/Primitive_exceptions.resi 100,319 100,765 +0.44%
packages/@rescript/runtime/Primitive_dict.res 102,715 103,161 +0.43%
packages/@rescript/runtime/Primitive_hash.resi 102,744 103,190 +0.43%
packages/@rescript/runtime/Primitive_module.resi 103,182 103,628 +0.43%
packages/@rescript/runtime/RescriptTools.res 104,517 104,963 +0.43%
packages/@rescript/runtime/Stdlib_TaggedTemplate.resi 106,124 106,570 +0.42%
packages/@rescript/runtime/Stdlib_ArrayBuffer.res 107,537 107,983 +0.41%
packages/@rescript/runtime/Stdlib_Int.res 455,336 453,477 -0.41%
packages/@rescript/runtime/Stdlib_ArrayBuffer.resi 110,989 111,435 +0.40%
packages/@rescript/runtime/Primitive_hash.res 964,614 960,748 -0.40%
packages/@rescript/runtime/RescriptTools_ExtractCodeBlocks.res 112,140 112,586 +0.40%
packages/@rescript/runtime/Stdlib_Lazy.resi 112,434 112,880 +0.40%
packages/@rescript/belt/src/Belt_Array.res 2,154,235 2,145,750 -0.39%
packages/@rescript/runtime/Primitive_array_extern.res 114,220 114,666 +0.39%
packages/@rescript/runtime/Primitive_object_extern.res 114,729 115,175 +0.39%
packages/@rescript/runtime/Stdlib_Array.res 1,281,101 1,276,123 -0.39%
packages/@rescript/runtime/Stdlib_Intl_Segments.res 116,601 117,047 +0.38%
packages/@rescript/runtime/Stdlib_Bool.resi 116,818 117,264 +0.38%
packages/@rescript/runtime/Primitive_object.resi 117,125 117,571 +0.38%
packages/@rescript/runtime/Stdlib_TaggedTemplate.res 115,368 115,801 +0.38%
packages/@rescript/runtime/Dom_storage2.res 120,182 120,628 +0.37%
packages/@rescript/belt/src/Belt_Map.res 981,236 977,627 -0.37%
packages/@rescript/runtime/Jsx.res 121,738 122,184 +0.37%
packages/@rescript/runtime/Primitive_option.resi 122,265 122,711 +0.36%
packages/@rescript/runtime/Primitive_option.res 297,411 296,328 -0.36%
packages/@rescript/runtime/Stdlib_JsExn.resi 122,775 123,221 +0.36%
packages/@rescript/runtime/Stdlib_WeakSet.res 123,172 123,618 +0.36%
stress/LetRec.res 31,619,781 31,507,948 -0.35%
packages/@rescript/belt/src/Belt_Set.res 857,946 854,927 -0.35%
packages/@rescript/runtime/Stdlib_WeakMap.res 135,503 135,949 +0.33%
packages/@rescript/belt/src/Belt_SortArray.res 1,447,582 1,442,823 -0.33%
packages/@rescript/runtime/Stdlib_Type.resi 135,946 136,392 +0.33%
packages/@rescript/runtime/Stdlib_Global.res 141,743 142,189 +0.31%
packages/@rescript/runtime/Stdlib_Temporal_Now.res 143,495 143,941 +0.31%
build/leaf-edit 13,027,555 13,068,022 +0.31%
packages/@rescript/runtime/Stdlib_Bool.res 146,012 146,460 +0.31%
packages/@rescript/runtime/Stdlib_Symbol.res 146,511 146,957 +0.30%
packages/@rescript/runtime/Stdlib_IterableIterator.resi 149,406 149,852 +0.30%
packages/@rescript/belt/src/Belt_Int.resi 149,771 150,217 +0.30%
packages/@rescript/belt/src/Belt_Float.resi 151,251 151,697 +0.29%
packages/@rescript/belt/src/Belt_SortArrayInt.res 1,342,429 1,338,501 -0.29%
packages/@rescript/belt/src/Belt_SortArrayString.res 1,344,225 1,340,297 -0.29%
packages/@rescript/runtime/Stdlib_Intl_Common.res 154,360 154,806 +0.29%
packages/@rescript/runtime/Stdlib_Intl_Segmenter.res 154,540 154,986 +0.29%
packages/@rescript/runtime/Stdlib_Global.resi 155,548 155,994 +0.29%
packages/@rescript/runtime/Stdlib_Iterator.resi 159,246 159,692 +0.28%
packages/@rescript/runtime/Stdlib_AsyncIterableIterator.resi 159,925 160,371 +0.28%
packages/@rescript/runtime/Stdlib_Symbol.resi 160,141 160,587 +0.28%
packages/@rescript/runtime/Stdlib_Generator.resi 160,547 160,993 +0.28%
packages/@rescript/runtime/Stdlib_RegExp.res 163,713 164,159 +0.27%
packages/@rescript/belt/src/Belt_Range.res 288,346 287,561 -0.27%
packages/@rescript/belt/src/Belt_internalBucketsType.resi 164,724 165,170 +0.27%
packages/@rescript/runtime/Stdlib_AsyncGenerator.resi 165,925 166,371 +0.27%
packages/@rescript/belt/src/Belt_MutableStack.resi 169,281 169,727 +0.26%
packages/@rescript/belt/src/Belt_SortArrayString.resi 169,577 170,023 +0.26%
packages/@rescript/belt/src/Belt_SortArrayInt.resi 169,695 170,141 +0.26%
packages/@rescript/runtime/Stdlib_Intl_ListFormat.res 169,825 170,271 +0.26%
packages/@rescript/runtime/Stdlib_Intl.res 173,862 174,308 +0.26%
packages/@rescript/belt/src/Belt_HashSetString.resi 175,553 175,999 +0.25%
packages/@rescript/belt/src/Belt_HashSetInt.resi 175,666 176,112 +0.25%
packages/@rescript/belt/src/Belt_Range.resi 176,657 177,103 +0.25%
packages/@rescript/runtime/Stdlib_AsyncIterator.resi 177,818 178,264 +0.25%
packages/@rescript/runtime/Stdlib_Intl_Collator.res 178,667 179,113 +0.25%
packages/@rescript/runtime/Stdlib_Intl_RelativeTimeFormat.res 180,464 180,910 +0.25%
packages/@rescript/runtime/Stdlib_JsError.resi 184,968 185,414 +0.24%
packages/@rescript/runtime/Stdlib_BigInt.resi 186,569 187,015 +0.24%
packages/@rescript/runtime/Stdlib_RegExp.resi 187,800 188,246 +0.24%
packages/@rescript/belt/src/Belt.res 187,948 188,394 +0.24%
packages/@rescript/belt/src/Belt_Float.res 165,048 165,439 +0.24%
packages/@rescript/belt/src/Belt_Int.res 165,790 166,181 +0.24%
packages/@rescript/belt/src/Belt_internalSetBuckets.resi 194,250 194,696 +0.23%
packages/@rescript/runtime/Stdlib_Float.resi 194,292 194,738 +0.23%
packages/@rescript/runtime/Stdlib_Intl_Locale.res 198,068 198,514 +0.23%
packages/@rescript/runtime/Stdlib_Nullable.resi 199,672 200,118 +0.22%
packages/@rescript/runtime/Stdlib_Intl_PluralRules.res 201,889 202,335 +0.22%
packages/@rescript/runtime/Stdlib_Null.resi 204,431 204,877 +0.22%
packages/@rescript/runtime/Stdlib_Int.resi 206,103 206,549 +0.22%
packages/@rescript/belt/src/Belt_MutableQueue.resi 207,326 207,772 +0.22%
packages/@rescript/belt/src/Belt_HashMapString.resi 213,714 214,160 +0.21%
packages/@rescript/belt/src/Belt_HashMapInt.resi 213,828 214,274 +0.21%
packages/@rescript/runtime/Stdlib_Map.resi 217,108 217,554 +0.21%
packages/@rescript/belt/src/Belt_HashSet.resi 222,778 223,224 +0.20%
packages/@rescript/belt/src/Belt_internalBuckets.resi 223,042 223,488 +0.20%
packages/@rescript/runtime/Stdlib_Temporal.res 223,963 224,409 +0.20%
packages/@rescript/runtime/Stdlib_Set.resi 228,953 229,399 +0.19%
packages/@rescript/runtime/Stdlib_DataView.res 230,306 230,752 +0.19%
packages/@rescript/runtime/Stdlib_IteratorObject.resi 234,376 234,822 +0.19%
packages/@rescript/runtime/Stdlib_Map.res 205,648 206,038 +0.19%
packages/@rescript/runtime/Stdlib_Option.resi 236,507 236,953 +0.19%
packages/@rescript/runtime/Stdlib_JSON.resi 237,027 237,473 +0.19%
packages/@rescript/runtime/Pervasives.res 239,805 240,251 +0.19%
packages/@rescript/runtime/Primitive_js_extern.res 239,834 240,280 +0.19%
packages/@rescript/belt/src/Belt_Result.resi 240,168 240,614 +0.19%
packages/@rescript/runtime/Stdlib_Dict.resi 240,779 241,225 +0.19%
packages/@rescript/runtime/Stdlib_Set.res 210,990 211,380 +0.18%
packages/@rescript/belt/src/Belt_Option.resi 243,374 243,820 +0.18%
packages/@rescript/runtime/Stdlib_Object.res 247,331 247,777 +0.18%
packages/@rescript/belt/src/Belt_SortArray.resi 253,942 254,388 +0.18%
packages/@rescript/belt/src/Belt_SetString.resi 259,922 260,368 +0.17%
packages/@rescript/belt/src/Belt_SetInt.resi 260,049 260,495 +0.17%
packages/@rescript/belt/src/Belt_Id.resi 260,286 260,732 +0.17%
packages/@rescript/belt/src/Belt_MutableSetString.resi 264,996 265,442 +0.17%
packages/@rescript/belt/src/Belt_MutableSetInt.resi 265,113 265,559 +0.17%
packages/@rescript/runtime/Stdlib_DataView.resi 268,044 268,490 +0.17%
packages/@rescript/runtime/RescriptTools_Docgen.resi 273,524 273,970 +0.16%
packages/@rescript/runtime/RescriptTools_Docgen.res 283,791 284,237 +0.16%
packages/@rescript/runtime/Stdlib_Result.resi 296,377 296,823 +0.15%
packages/@rescript/belt/src/Belt_HashMap.resi 296,908 297,354 +0.15%
packages/@rescript/runtime/Stdlib_IterableIterator.res 178,852 179,109 +0.14%
packages/@rescript/belt/src/Belt_MutableMapString.resi 314,993 315,439 +0.14%
packages/@rescript/belt/src/Belt_MutableMapInt.resi 315,111 315,557 +0.14%
packages/@rescript/runtime/Stdlib_Math.resi 328,324 328,770 +0.14%
packages/@rescript/runtime/Stdlib_Int8Array.res 343,494 343,940 +0.13%
packages/@rescript/runtime/Stdlib_Uint8Array.res 343,507 343,953 +0.13%
packages/@rescript/runtime/Stdlib_Float32Array.res 343,614 344,060 +0.13%
packages/@rescript/runtime/Stdlib_Float64Array.res 343,614 344,060 +0.13%
packages/@rescript/runtime/Stdlib_Int16Array.res 343,639 344,085 +0.13%
packages/@rescript/runtime/Stdlib_Int32Array.res 343,641 344,087 +0.13%
packages/@rescript/runtime/Stdlib_Uint16Array.res 343,646 344,092 +0.13%
packages/@rescript/runtime/Stdlib_Uint32Array.res 343,646 344,092 +0.13%
packages/@rescript/runtime/Stdlib_BigInt64Array.res 343,671 344,117 +0.13%
packages/@rescript/runtime/Stdlib_BigUint64Array.res 343,748 344,194 +0.13%
packages/@rescript/runtime/Stdlib_Uint8ClampedArray.res 344,022 344,468 +0.13%
packages/@rescript/belt/src/Belt_internalAVLset.resi 354,869 355,315 +0.13%
packages/@rescript/runtime/Stdlib_Promise.resi 356,017 356,463 +0.13%
packages/@rescript/runtime/Stdlib_Temporal_Common.res 366,668 367,114 +0.12%
packages/@rescript/runtime/Stdlib_Console.res 370,179 370,625 +0.12%
packages/@rescript/runtime/Primitive_array.res 191,984 192,210 +0.12%
packages/@rescript/runtime/Stdlib_IteratorObject.res 250,722 251,013 +0.12%
packages/@rescript/runtime/Stdlib_Intl_DateTimeFormat.res 389,599 390,045 +0.11%
packages/@rescript/belt/src/Belt_MapString.resi 391,308 391,754 +0.11%
packages/@rescript/belt/src/Belt_MapInt.resi 391,351 391,797 +0.11%
packages/@rescript/belt/src/Belt_MutableMap.resi 395,444 395,890 +0.11%
packages/@rescript/belt/src/Belt_MutableSet.resi 397,546 397,992 +0.11%
packages/@rescript/belt/src/Belt_SetDict.resi 402,680 403,126 +0.11%
packages/@rescript/runtime/Stdlib_Intl_NumberFormat.res 404,810 405,256 +0.11%
packages/@rescript/runtime/Stdlib_Ordering.res 152,340 152,506 +0.11%
packages/@rescript/runtime/Stdlib_List.resi 409,863 410,309 +0.11%
packages/@rescript/runtime/Stdlib_Console.resi 411,610 412,056 +0.11%
packages/@rescript/runtime/Stdlib_BigInt.res 207,074 207,292 +0.11%
packages/@rescript/belt/src/Belt_Set.resi 437,438 437,884 +0.10%
packages/@rescript/runtime/Primitive_float.res 146,893 147,042 +0.10%

Instructions

In total: 9,221,430,593 → 7,706,784,229 (-16.43%).

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

Benchmark Base Head Change
stress/IntSwitch.res 1,720,934,657 499,787,928 -70.96%
stress/PolyVariantSwitch.res 633,956,365 353,237,431 -44.28%
packages/@rescript/belt/src/Belt_internalAVLtree.res 136,419,398 126,612,011 -7.19%
packages/@rescript/belt/src/Belt_List.res 118,135,152 115,797,934 -1.98%
packages/@rescript/runtime/Stdlib_Math.resi 21,296,451 21,326,176 +0.14%
parse/RedBlackTreeNoComments.res 14,487,525 14,506,273 +0.13%
packages/@rescript/runtime/Stdlib_List.resi 24,523,883 24,554,290 +0.12%
packages/@rescript/runtime/Stdlib_String.resi 29,450,179 29,485,339 +0.12%
parse/RedBlackTree.res 14,919,506 14,935,264 +0.11%

Output size

In total: 25,962,507 → 25,965,169 (+0.01%).

Benchmark Base Head Change
output/playground/compiler.js 1,889,071 1,890,993 +0.10%

Pull request cd8e26f compared with base 643e9c0 in this run.

@github-actions

Copy link
Copy Markdown

@cknitt
cknitt force-pushed the switch-unused-check-perf branch from d0f2c46 to 136c8f1 Compare November 20, 2026 21:10
cknitt and others added 2 commits November 23, 2026 06:46
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 136c8f1 to cd8e26f Compare November 23, 2026 06:47

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