Skip to content

refactor(type-inference): memoize resolveExpressionTypeRich at the boundary#571

Merged
cs01 merged 1 commit intomainfrom
refactor/memoize-resolver
Apr 20, 2026
Merged

refactor(type-inference): memoize resolveExpressionTypeRich at the boundary#571
cs01 merged 1 commit intomainfrom
refactor/memoize-resolver

Conversation

@cs01
Copy link
Copy Markdown
Owner

@cs01 cs01 commented Apr 20, 2026

What changed for users

Compiler now caches type-resolution results per AST expression node. Today this is a wash (existing call sites don't repeat-query), but it unblocks the upcoming TypedAST annotator pass — which previously hung stage 2 self-compile because eager use of the unmemoized resolver was O(N^2).

Details

  • Added parallel-array memoization in TypeInference.resolveExpressionTypeRich, keyed by AST expression reference identity. Chose parallel arrays over Map<object, ResolvedType> because native ChadScript's Map doesn't support object keys (a minimal repro segfaults). The previously-dormant LLVMGenerator.typeCache (PR feat(codegen): phase E substrate — ctx.typeOf() type cache #540) had this same latent bug; consolidated it away.
  • Cached only for context-independent expression shapes (literals, new, array/map/set, object). Skipped for variable/member_access/index_access/call/method_call because those resolve through the symbol table, which is refined mid-codegen by variable-allocator and friends — caching them could freeze an early, less-specific answer.
  • LLVMGenerator.typeOf() now delegates to the inner cache, removing the duplicate Map.

Test plan

  • npm run verify (full, including stage 2 self-host) green locally
  • npm run format:check clean
  • Cache validated via CHAD_DEBUG_RESOLVER_CACHE instrumentation during development (removed before commit)

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark Results (Linux x86-64)

Benchmark C ChadScript Go Node Place
Binary Trees 1.321s 1.055s 2.099s 1.048s 🥈
Cold Start 0.8ms 0.7ms 1.0ms 24.6ms 🥇
Fibonacci 0.705s 0.706s 1.341s 2.607s 🥈
File I/O 0.075s 0.080s 0.074s 0.155s 🥉
JSON Parse/Stringify 0.003s 0.005s 0.013s 0.013s 🥈
Matrix Multiply 0.379s 0.384s 0.681s 0.330s 🥉
Monte Carlo Pi 0.341s 0.348s 0.355s 2.011s 🥈
N-Body Simulation 1.384s 1.754s 1.773s 1.851s 🥈
Quicksort 0.191s 0.218s 0.188s 0.230s 🥉
SQLite 0.253s 0.263s 0.330s 🥈
Sieve of Eratosthenes 0.011s 0.021s 0.016s 0.033s 🥉
String Manipulation 0.007s 0.014s 0.013s 0.030s 🥉

CLI Tool Benchmarks

Benchmark ChadScript grep node xxd Place
Hex Dump 0.472s 0.822s 0.107s 🥈
Recursive Grep 0.018s 0.010s 0.084s 🥈

@cs01 cs01 merged commit 50f6cb2 into main Apr 20, 2026
13 checks passed
@cs01 cs01 deleted the refactor/memoize-resolver branch April 20, 2026 05:23
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