Skip to content

Add missing cache checks to scopeInspection.ts#282

Merged
JordanBoltonMN merged 2 commits into
masterfrom
dev/jobolton/fixDuplicateScopeGeneration
Mar 30, 2026
Merged

Add missing cache checks to scopeInspection.ts#282
JordanBoltonMN merged 2 commits into
masterfrom
dev/jobolton/fixDuplicateScopeGeneration

Conversation

@JordanBoltonMN

Copy link
Copy Markdown
Contributor

Problem

tryNodeScope is called once per identifier during validation. Each call walks the ancestry and hits
inspectLetExpression / inspectSection / inspectKeyValuePairs, which call scopeItemFactoryForKeyValuePairs to
build scope entries — an O(n) scan of all sibling bindings.

The problem: this O(n) entry computation ran even when the scope was already cached in scopeById.
assignScopeForNodeId has an internal cache check, but the expensive work happened before reaching it. For a
document with N identifiers and B bindings, this produced O(N × B²) total work.

Fix

Add three cache checks to skip scopeItemFactoryForKeyValuePairs when the target scope is already in scopeById:

  • inspectKeyValuePairs: skip binding if scopeById.has(kvp.value.node.id)
  • inspectSection: same guard
  • inspectLetExpression: skip in-expression scope if already cached

Benchmark — Kusto.pq (3,529 lines, 4,220 identifiers)

Operation: tryNodeScope called once per value-context identifier (the hot path in
validateUnknownIdentifiers).

Before After
Median 34.8s 740ms
Speedup 47×

Additional connectors

Connector Before After Speedup
WorkplaceAnalytics (1.6K lines) 1.73s 97ms 18×
BitSight (2.3K lines) 34.4s 274ms 126×
BQECore (2.5K lines) 70.5s 398ms 177×

Changes

  • scopeInspection.ts — 3 lines changed: added scopeById.has() cache checks
  • scope.test.ts — 2 new tests: cache reuse verification + sub-cubic scaling assertion
  • package.json — Version bump to 0.14.0

…licateScopeGeneration

# Conflicts:
#	package-lock.json
#	package.json
@JordanBoltonMN JordanBoltonMN changed the title Fix cache checks under scopeInspection.ts Add missing cache checks to scopeInspection.ts Mar 30, 2026
@JordanBoltonMN JordanBoltonMN merged commit 5b9b2dd into master Mar 30, 2026
5 checks passed
@JordanBoltonMN JordanBoltonMN deleted the dev/jobolton/fixDuplicateScopeGeneration branch March 30, 2026 04:15
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.

2 participants