Skip to content

only build intersection props that can reduce it to never - #64476

Draft
Max Schwenk (maschwenk) wants to merge 4 commits into
microsoft:mainfrom
maschwenk:perf/intersection-never-reduction
Draft

Max Schwenk (maschwenk) wants to merge 4 commits into
microsoft:mainfrom
maschwenk:perf/intersection-never-reduction

Conversation

@maschwenk

@maschwenk Max Schwenk (maschwenk) commented Sep 27, 2026 •

Copy link
Copy Markdown

Fixes #64474 (part 2, #64475 is part 1). doesnt depend on #64475

getReducedType checks whether an intersection reduces to never by calling getPropertiesOfUnionOrIntersectionType, which creates the combined property (and the intersection of its types) for every name in the intersection and keeps all of them around. but a combined property can only reduce the intersection if one of the constituent properties has a literal type or is private

so this moves the loop in createUnionOrIntersectionProperty that collects each constituent's property of a name into collectUnionOrIntersectionPropertyParts, and both use it. getReducedType looks at what it collected and only creates the combined property when it could reduce the intersection. theres no second copy of the checking logic. the code change is about 80 lines, the rest of the diff is one test and its baselines

similar goal to #53346, but instead of stopping early it skips creating the properties that can't reduce, which is where the memory goes

this PR alone vs main (typescript-benchmarking, median of 3):

heap, 4 checkers peak rss, 4 checkers check, 4 checkers heap, single check, single
mui-docs −41.8% −38.2% −38.9% −33.7% −28.8%
Compiler-Unions −4.7% −3.0% 0.0% −5.1% −3.8%
webpack −2.5% +1.9% −3.9% −2.9% −2.7%
xstate-main −1.2% −0.9% +3.6% −0.9% +0.5%
vscode −0.4% −0.7% −2.2% −0.3% +0.8%
Compiler −1.8% −1.0% +1.4% −0.4% −1.2%

mui-docs gets the big win since its styling types build some really big intersections. the check times on the small projects are within run to run noise. raw results and scripts: https://gist.github.com/maschwenk/c83d9185c6961b6fb43d7d071ef39cf6

i also tried an even simpler check, create the property for any name more than one constituent has (d33ab85). less code, but on our 37k-file program it held 0.28 GiB more heap, so i went back

full go suite passes (also multiple checkers and -race), lint and format are clean. added intersectionNeverReductionByProperty.ts with baselines generated on unmodified main. it covers literal/boolean/template literal discriminants, optional and never props, private vs protected, instantiations of the same generic class, generic aliases and type params with union constraints

used claude code to help write this, ive reviewed it

getReducedType decided whether an intersection reduces to never by creating
the combined property of every property name of every constituent
(getPropertiesOfUnionOrIntersectionType). In large intersections most of
those synthetic properties, and the intersections of their types, are
never used again.

Only a combination of distinct constituent properties where one has a
literal type (a discriminant), a private property, or a constituent
property that is itself such a combination can satisfy
isNeverReducedProperty. isNeverReducedIntersection now collects the
constituent properties of each name as createUnionOrIntersectionProperty
does, including comparing instantiations of the same property, and only
creates the combined property for those names. When the properties of the
intersection are already resolved it checks them as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Sep 27, 2026
@maschwenk Max Schwenk (maschwenk) changed the title Check intersections for never-reduction without creating every property Only build intersection properties that can reduce it to never Sep 27, 2026
@maschwenk Max Schwenk (maschwenk) changed the title Only build intersection properties that can reduce it to never only build intersection props that can reduce it to never Sep 27, 2026
Instead of collecting the constituent properties of every name the way
createUnionOrIntersectionProperty does, create the combined property for
any name that more than one constituent has. The combined property of a
name only one constituent has is that constituent's own property, which
can only reduce the intersection if it already combines the properties of
a union.

This creates more combined properties than before, but is much simpler
and saves the per-name lookups in every constituent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This reverts d33ab85. The simpler check was faster on the
typescript-benchmarking projects, but on a 37k-file program it created
356k more symbols and held 0.28 GiB more heap (about 1.3 GiB more peak
RSS with 4 checkers), which is the problem this change is for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
isNeverReducedIntersection re-implemented how
createUnionOrIntersectionProperty collects each constituent's property of
a name. That loop is now collectUnionOrIntersectionPropertyParts, and both
use it. From what it collects, isNeverReducedIntersection checks whether
the combined property could satisfy isNeverReducedProperty (it needs a
literal-typed or a private constituent property) before creating it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

checker builds member tables and intersection props it never uses

1 participant