Skip to content

cmd/compile: add size cache to StdSizes to avoid exponential compile time#79314

Open
hesam-oxe wants to merge 1 commit into
golang:masterfrom
hesam-oxe:fix/generic-nested-compile-time
Open

cmd/compile: add size cache to StdSizes to avoid exponential compile time#79314
hesam-oxe wants to merge 1 commit into
golang:masterfrom
hesam-oxe:fix/generic-nested-compile-time

Conversation

@hesam-oxe
Copy link
Copy Markdown

Added a sizeCache map with sync.RWMutex to StdSizes to memoize
Sizeof results for deeply nested generic types.

Problem

When compiling deeply nested generic instantiations like:

type T[P any] struct { _,_,_,_,_,_,_,_ P }
type U T[T[T[T[T[T[T[T[T[T[int]]]]]]]]]]

StdSizes.Sizeof was called recursively without caching, resulting in
exponential time (8^N where N is nesting depth).

Root Cause

Sizeof for *Struct and *Array types recursively computes sizes
of their elements/fields. For nested generic types, the same underlying
type is recomputed millions of times.

Fix

  • Added sizeCache map[Type]int64 and sync.RWMutex to StdSizes
  • Sizeof checks the cache before computing, and stores results after
  • Lazy initialization of the cache map

Impact

  • Compilation time for deeply nested generics drops from exponential to linear
  • No API change — cache is transparent to callers
  • Thread-safe via RWMutex

Fixes #78342

…time

Added sizeCache map with RWMutex to StdSizes to memoize Sizeof results. This prevents exponential recomputation for deeply nested generic types. Fixes golang#78342
@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 10, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@hesam-oxe
Copy link
Copy Markdown
Author

/verify-cla

@hesam-oxe
Copy link
Copy Markdown
Author

@google-cla I've signed the Individual CLA with email
chngyzkhanwhsht@gmail.com. Can you please re-check?

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.

cmd/compile: exponential compile-time with nested generic instantiations

1 participant