Skip to content

checker: check nested overloaded calls in linear time - #1010

Merged
antonmedv merged 1 commit into
masterfrom
fix/1008-exponential-overload-check
Nov 22, 2026
Merged

antonmedv merged 1 commit into
masterfrom
fix/1008-exponential-overload-check

Conversation

@antonmedv

Copy link
Copy Markdown
Member

Fixes #1008.

An overloaded function, such as the replace, join or sort builtins, checks its arguments once per overload it tries. Nested calls multiplied this at every level, so compile time grew exponentially with nesting depth. A short expression of 42 nested replace calls never finished compiling. Functions registered through expr.Function with several overloads were affected too when they return any.

While overloads are being tried, the checker now caches the natures of call and builtin nodes for the rest of the pass, so trying the next overload doesn't check them again. The cache is cleared on reset, at the start of every pass, so it can't go stale after patchers change the tree. Expressions without overloaded calls don't touch it.

Depth replace before after
20 ~1 s 44 µs
42 never finishes 75 µs
2000 – 7 ms

Benchmarks against master (8 interleaved runs, benchstat): BenchmarkChecker unchanged within noise, 0 allocations. Compiling expressions that use builtins is 7–11% faster, with up to 11% fewer allocations.

test/issues/1008 compiles depth-100 nesting in four shapes. It hangs without the fix.

An overloaded function, such as the replace, join or sort builtins, checks
its arguments once per overload it tries. Nested calls multiplied this at
every level, so compile time grew exponentially with nesting depth: 42
nested replace calls never finished compiling.

While overloads are being tried, cache the natures of call and builtin
nodes for the rest of the pass, so trying the next overload doesn't check
them again. The cache is cleared on reset, at the start of every pass.

Fixes #1008
@antonmedv
antonmedv merged commit 399159a into master Nov 22, 2026
23 checks passed
@merlinfuchs

Copy link
Copy Markdown

Thanks for the immediate fix!

@antonmedv
antonmedv deleted the fix/1008-exponential-overload-check branch November 25, 2026 11:04
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.

Type checking nested builtin calls takes exponential time

2 participants