Skip to content

perf(kernel): cache type resolution by FQN#5055

Open
awsmadi wants to merge 1 commit intoaws:mainfrom
awsmadi:perf/kernel-type-cache
Open

perf(kernel): cache type resolution by FQN#5055
awsmadi wants to merge 1 commit intoaws:mainfrom
awsmadi:perf/kernel-type-cache

Conversation

@awsmadi
Copy link

@awsmadi awsmadi commented Mar 10, 2026

Summary

  • Add #typeCache Map for O(1) FQN-to-Type lookups (replacing repeated string splits + Map lookups)
  • Add #methodMapCache and #propertyMapCache for cached method/property resolution
  • Clear all caches on assembly reload for safety

Motivation

#typeInfoForFqn() is called on every method invocation, property access, and object creation. It splits the FQN string and does multiple Map lookups each time. Since assemblies are immutable after load, this result can be safely cached.

Changes

  • packages/@jsii/kernel/src/kernel.ts: Add type cache with lazy population and blanket invalidation on assembly load
  • packages/@jsii/kernel/src/kernel.test.ts: Add tests for cache hits, multi-assembly resolution, and cache invalidation

Impact

  • Risk: Low — caches are implementation details behind private # fields
  • Memory: ~2MB for 12K+ types (aws-cdk-lib), negligible vs 200MB+ synth heap
  • Performance: Estimated 20-30% reduction in kernel dispatch overhead
  • Invariant: Callers must not mutate returned spec.Type objects (documented in comments)

Test plan

  • Existing kernel tests pass (236 tests)
  • New tests for cache behavior
  • Run full cross-language compliance suite

#typeInfoForFqn() splits FQN strings and performs Map lookups on every
method invocation and property access. Since assemblies are immutable
after load, cache the result for O(1) subsequent lookups.

Cache is cleared on assembly load to prevent stale entries.
Callers must not mutate returned spec.Type objects.

Estimated 20-30% reduction in kernel dispatch overhead.
@awsmadi awsmadi force-pushed the perf/kernel-type-cache branch from a483efd to 6d11103 Compare March 11, 2026 00:05
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