Skip to content

Reuse ordered property counts during intersection reduction - #64804

Merged
Jake Bailey (jakebailey) merged 2 commits into
jakebailey/mem/byte-sized-arena-chunksfrom
jakebailey/mem/intersection-count-reuse
Oct 14, 2026
Merged

Jake Bailey (jakebailey) merged 2 commits into
jakebailey/mem/byte-sized-arena-chunksfrom
jakebailey/mem/intersection-count-reuse

Conversation

@jakebailey

Copy link
Copy Markdown
Member

Intersection reduction allocates temporary property-count tables; on MUI this one site is about a quarter of all allocated bytes.

  • Reuses bounded, checker-local tables while preserving property order and recursive isolation.
  • MUI measurements showed 26.64% fewer allocated bytes, 7.59% fewer allocations, and 8.88% fewer production CLI instructions.
  • Retained heap did not materially change. The warmed regression case goes 21 allocations → 0.

Part of the memory optimization stack summarized in #64734.

Replaces #64790, which GitHub closed as merged when the stack was reordered.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The reuse logic preserves existing semantics and is covered by focused allocation and reentrancy tests.

0 open findings

What changed in this PR

Reuses checker-local ordered property-count maps to eliminate allocations during intersection reduction while preserving ordering and recursive isolation.

Changes:

  • Adds bounded reusable property-count storage.
  • Uses pooled storage during intersection reduction.
  • Adds allocation, reuse, ordering, and reentrancy tests.
File Description
tsc/​internal/​checker/​checker.go Uses temporary property-count storage.
tsc/​internal/​checker/​utilities.go Implements bounded storage reuse.
tsc/​internal/​checker/​types_test.go Tests allocations and lifecycle safety.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@jakebailey
Jake Bailey (jakebailey) force-pushed the jakebailey/mem/intersection-count-reuse branch from fd19cf8 to 1ba59d2 Compare October 13, 2026 21:39
@github-project-automation github-project-automation Bot moved this from Not started to Needs merge in PR Backlog Oct 14, 2026
@jakebailey
Jake Bailey (jakebailey) force-pushed the jakebailey/mem/intersection-count-reuse branch from 1ba59d2 to 1482441 Compare October 14, 2026 01:07
@jakebailey
Jake Bailey (jakebailey) force-pushed the jakebailey/mem/intersection-count-reuse branch from 1482441 to 0e717e2 Compare October 14, 2026 02:23
@DanielRosenwasser

Copy link
Copy Markdown
Member

Weird that we need a free list. Maybe it's worth exploring a pool of 16, a bitset that indicates which one is currently in use, and just allocates if they're all occupied.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, optimization is optimization.

@ahejlsberg
Anders Hejlsberg (ahejlsberg) force-pushed the jakebailey/mem/intersection-count-reuse branch from 0e717e2 to 28400fe Compare October 14, 2026 14:14
Resolved type nodes and signatures still kept AST pointers as map keys
despite already having stable node identities. Those maps add redundant
pointer storage, GC scanning, hashing, and growth allocations.

Reuse the existing node-link store's pointer-free paged indexes for
these two high-volume caches. Ordinary IDs and IDs outside the dense
reserved range retain the existing fallback, and caches stay
checker-local.

Six fresh-process interleaved VS Code comparisons against the parent
commit, using normal GC, reduce retained heap 1.20%, scan bytes 2.12%,
allocated bytes 2.11%, and allocation events 0.20%. Instructions are
inconclusive. This saves another roughly 43 MiB of heap and 66 MiB of
scanning.

Production diagnostics and semantic counts match exactly. Tests cover
cache identity, ordinary/reserved IDs, clone isolation, growth,
GC reachability, and independent checkers.

goos: linux
goarch: amd64
pkg: github.com/microsoft/TypeScript/tsc/internal/checker
cpu: Intel(R) Core(TM) i9-10900K CPU @ 3.70GHz
                   │ vscode-paged-before-6.txt │         vscode-paged-6.txt         │
                   │        retained-B         │  retained-B   vs base              │
ExternalProgram-20                3.497Gi ± 0%   3.455Gi ± 0%  -1.20% (p=0.002 n=6)

                   │ vscode-paged-before-6.txt │         vscode-paged-6.txt         │
                   │          scan-B           │    scan-B     vs base              │
ExternalProgram-20                3.061Gi ± 0%   2.997Gi ± 0%  -2.12% (p=0.002 n=6)

                   │ vscode-paged-before-6.txt │         vscode-paged-6.txt         │
                   │           B/op            │     B/op      vs base              │
ExternalProgram-20                5.129Gi ± 0%   5.021Gi ± 0%  -2.11% (p=0.002 n=6)

                   │ vscode-paged-before-6.txt │        vscode-paged-6.txt         │
                   │         allocs/op         │  allocs/op   vs base              │
ExternalProgram-20                 31.16M ± 0%   31.09M ± 0%  -0.20% (p=0.002 n=6)

                        │ paged-before-instructions.txt │      paged-instructions.txt      │
                        │        instructions/op        │ instructions/op  vs base         │
ExternalInstructions-20                     279.8G ± 5%       270.5G ± 2%  ~ (p=0.589 n=6)
Intersection reduction repeatedly allocates temporary property-count
tables. On MUI, this one allocation site accounts for roughly a quarter
of all allocated bytes, despite none of the tables needing to survive
the reduction. Reusing this workspace removes substantial allocation
churn without changing the first-seen property order or caching results.

Each recursive reduction borrows an independent checker-local table.
Returning a table clears its names and counts, including on early
returns; tables exceeding 4096 properties are not retained.

Against the parent commit, six fresh-process MUI single-checker pairs
save 1.36 GiB of allocation per run, reduce allocation events by 7.59%,
and reduce instructions by 6.02%. Default-threaded production MUI
instructions decrease 8.88%. Retained heap and scannable heap are
essentially unchanged: this is an allocation-churn optimization.

VS Code allocation savings are smaller. Production VS Code instructions
remain inconclusive after twelve pairs, with a higher point estimate;
Webpack instructions are also inconclusive. Wall times are noisy on
this shared host. All production diagnostic text and semantic counts
match between revisions.

The regression test allocated 21 times per warmed reduction before this
change and now allocates zero times. Tests also cover nested borrowing,
property order, clearing, oversized tables, and early-return cleanup.

Six fresh-process single-checker pairs, ordinary GC:
                  │ mui-property-counts-memory-before.txt │ mui-property-counts-memory-after.txt │
                  │              retained-B               │     retained-B       vs base         │
ExternalMemory-20                            1.492Gi ± 0%          1.492Gi ± 0%  ~ (p=0.310 n=6)

                  │ mui-property-counts-memory-before.txt │ mui-property-counts-memory-after.txt │
                  │                scan-B                 │       scan-B         vs base         │
ExternalMemory-20                            1.270Gi ± 0%          1.270Gi ± 0%  ~ (p=0.310 n=6)

                  │ mui-property-counts-memory-before.txt │ mui-property-counts-memory-after.txt │
                  │                 B/op                  │     B/op       vs base               │
ExternalMemory-20                            5.122Gi ± 0%    3.757Gi ± 0%  -26.64% (p=0.002 n=6)

                  │ mui-property-counts-memory-before.txt │ mui-property-counts-memory-after.txt │
                  │               allocs/op               │   allocs/op     vs base              │
ExternalMemory-20                             24.77M ± 0%      22.89M ± 0%  -7.59% (p=0.002 n=6)
       │ mui-property-counts-memory-before.instructions │ mui-property-counts-memory-after.instructions │
       │                instructions/op                 │     instructions/op      vs base              │
mui-20                                      213.7G ± 3%               200.8G ± 3%  -6.02% (p=0.002 n=6)
                  │ vscode-property-counts-memory-before.txt │ vscode-property-counts-memory-after.txt │
                  │                retained-B                │       retained-B        vs base         │
ExternalMemory-20                               2.615Gi ± 0%             2.615Gi ± 0%  ~ (p=0.589 n=6)

                  │ vscode-property-counts-memory-before.txt │ vscode-property-counts-memory-after.txt │
                  │                  scan-B                  │         scan-B          vs base         │
ExternalMemory-20                               2.287Gi ± 0%             2.287Gi ± 0%  ~ (p=0.589 n=6)

                  │ vscode-property-counts-memory-before.txt │ vscode-property-counts-memory-after.txt │
                  │                   B/op                   │       B/op         vs base              │
ExternalMemory-20                               3.749Gi ± 0%        3.714Gi ± 0%  -0.92% (p=0.002 n=6)

                  │ vscode-property-counts-memory-before.txt │ vscode-property-counts-memory-after.txt │
                  │                allocs/op                 │     allocs/op      vs base              │
ExternalMemory-20                                23.60M ± 0%         23.49M ± 0%  -0.47% (p=0.002 n=6)
          │ vscode-property-counts-memory-before.instructions │ vscode-property-counts-memory-after.instructions │
          │                  instructions/op                  │         instructions/op          vs base         │
vscode-20                                         193.0G ± 4%                       193.2G ± 5%  ~ (p=0.818 n=6)

Default-threaded production CLI (six pairs; VS Code twelve pairs):
       │ mui-property-counts-cli-before.txt │ mui-property-counts-cli-after.txt │
       │             heap-B/op              │    heap-B/op      vs base         │
mui-20                         1.867Gi ± 0%       1.868Gi ± 0%  ~ (p=0.589 n=6)

       │ mui-property-counts-cli-before.txt │ mui-property-counts-cli-after.txt │
       │             allocs/op              │  allocs/op   vs base              │
mui-20                          29.52M ± 0%   27.84M ± 0%  -5.69% (p=0.002 n=6)

       │ mui-property-counts-cli-before.txt │ mui-property-counts-cli-after.txt  │
       │            check-sec/op            │ check-sec/op  vs base              │
mui-20                           11.59 ± 3%     10.67 ± 5%  -7.86% (p=0.004 n=6)
       │ mui-property-counts-cli-before.instructions │ mui-property-counts-cli-after.instructions │
       │               instructions/op               │   instructions/op     vs base              │
mui-20                                   274.6G ± 2%            250.2G ± 4%  -8.88% (p=0.002 n=6)
          │ vscode-property-counts-cli-before.txt │ vscode-property-counts-cli-after.txt │
          │               heap-B/op               │     heap-B/op       vs base          │
vscode-20                            2.828Gi ± 0%         2.828Gi ± 0%  ~ (p=0.843 n=12)

          │ vscode-property-counts-cli-before.txt │ vscode-property-counts-cli-after.txt │
          │               allocs/op               │   allocs/op    vs base               │
vscode-20                             36.62M ± 0%     36.54M ± 0%  -0.21% (p=0.000 n=12)

          │ vscode-property-counts-cli-before.txt │ vscode-property-counts-cli-after.txt │
          │             check-sec/op              │    check-sec/op     vs base          │
vscode-20                             8.671 ± 21%          8.409 ± 26%  ~ (p=0.799 n=12)
          │ vscode-property-counts-cli-before.instructions │ vscode-property-counts-cli-after.instructions │
          │                instructions/op                 │       instructions/op        vs base          │
vscode-20                                      296.5G ± 4%                   307.6G ± 3%  ~ (p=0.242 n=12)
           │ webpack-property-counts-cli-before.txt │ webpack-property-counts-cli-after.txt │
           │               heap-B/op                │      heap-B/op        vs base         │
webpack-20                             663.0Mi ± 0%           663.1Mi ± 0%  ~ (p=0.699 n=6)

           │ webpack-property-counts-cli-before.txt │ webpack-property-counts-cli-after.txt │
           │               allocs/op                │    allocs/op     vs base              │
webpack-20                              6.208M ± 0%       6.096M ± 0%  -1.80% (p=0.002 n=6)

           │ webpack-property-counts-cli-before.txt │ webpack-property-counts-cli-after.txt │
           │              check-sec/op              │     check-sec/op      vs base         │
webpack-20                               1.765 ± 8%             1.711 ± 6%  ~ (p=0.180 n=6)
           │ webpack-property-counts-cli-before.instructions │ webpack-property-counts-cli-after.instructions │
           │                 instructions/op                 │        instructions/op         vs base         │
webpack-20                                       52.37G ± 2%                     51.78G ± 3%  ~ (p=0.394 n=6)
@jakebailey
Jake Bailey (jakebailey) force-pushed the jakebailey/mem/intersection-count-reuse branch from 28400fe to e9ef3a7 Compare October 14, 2026 14:17
@jakebailey
Jake Bailey (jakebailey) added this pull request to the merge queue Oct 14, 2026
Merged via the queue into main with commit 6c976da Oct 14, 2026
54 checks passed
Base automatically changed from jakebailey/mem/indexed-type-node-caches to jakebailey/mem/byte-sized-arena-chunks October 14, 2026 16:02
@jakebailey
Jake Bailey (jakebailey) deleted the jakebailey/mem/intersection-count-reuse branch October 14, 2026 16:02
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants