Skip to content

Perf: Convert keyword/operator arrays to Sets in Monarch tokenizer (fixes #582)#718

Merged
tnaum-ms merged 2 commits into
microsoft:mainfrom
hanhan761:fix/582-monarch-set-lookup
Jun 23, 2026
Merged

Perf: Convert keyword/operator arrays to Sets in Monarch tokenizer (fixes #582)#718
tnaum-ms merged 2 commits into
microsoft:mainfrom
hanhan761:fix/582-monarch-set-lookup

Conversation

@hanhan761

Copy link
Copy Markdown
Contributor

Fixes #582

Summary

Converts keyword/operator array lookups in the Monarch tokenizer from O(n) Array.includes() to O(1) Set.has() using a lazy WeakMap-based cache.

Changes in src/documentdb/shell/highlighting/monarchRunner.ts:

  • Added rulesSetCache — a WeakMap<MonarchLanguageRules, Map<string, Set<string>>> that lazily converts named arrays (keywords, bsonConstructors, shellCommands, operators) to Sets on first access
  • resolveCases() now uses set?.has(matchedText) instead of (array as string[]).includes(matchedText)
  • WeakMap keyed by the rules object ensures cached Sets are garbage-collected when the rules reference is released

Testing

  • All 45 monarchRunner tests pass
  • TypeScript compilation passes with no errors

Copilot AI review requested due to automatic review settings June 1, 2026 14:01
@hanhan761 hanhan761 requested a review from a team as a code owner June 1, 2026 14:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates autocomplete and UI/higlighting helpers to improve correctness and performance by generating valid aggregation field references, showing index counts in the tree, and speeding up Monarch cases lookups.

Changes:

  • Emit $getField aggregation references for field paths that contain non-identifier segments and add tests for unsafe names/escaping.
  • Cache index counts and surface them as the tree item description for “Indexes”.
  • Add a lazy Set-based cache for Monarch named-array lookups to avoid repeated linear .includes() scans.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/utils/json/data-api/autocomplete/toFieldCompletionItems.ts Adds escaping + safe/unsafe detection and emits $getField for unsafe aggregation field references.
src/utils/json/data-api/autocomplete/toFieldCompletionItems.test.ts Extends tests to cover $getField output and quote escaping.
src/tree/documentdb/IndexesItem.ts Caches index count after loading and displays it in the tree item description.
src/documentdb/shell/highlighting/monarchRunner.ts Introduces lazy Set caching for faster keyword/operator lookups in cases.

Comment thread src/utils/json/data-api/autocomplete/toFieldCompletionItems.ts Outdated
Comment thread src/utils/json/data-api/autocomplete/toFieldCompletionItems.ts Outdated
Comment thread src/utils/json/data-api/autocomplete/toFieldCompletionItems.test.ts
@tnaum-ms tnaum-ms added the in-triage-queue We've seen your input and will triage it label Jun 4, 2026
@tnaum-ms tnaum-ms added this to the 0.9.1 milestone Jun 23, 2026
…ixes microsoft#582)

- Added lazy WeakMap<MonarchLanguageRules, Map<string, Set<string>>> cache
- resolveCases now uses Set.has() (O(1)) instead of Array.includes() (O(n))
- Sets are created lazily on first access per rules object

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread src/documentdb/shell/highlighting/monarchRunner.ts Outdated
@tnaum-ms tnaum-ms removed the in-triage-queue We've seen your input and will triage it label Jun 23, 2026
@tnaum-ms tnaum-ms enabled auto-merge June 23, 2026 13:28
@tnaum-ms tnaum-ms merged commit 8fda157 into microsoft:main Jun 23, 2026
5 checks passed
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.

Perf: Convert keyword/operator arrays to Sets in Monarch tokenizer

4 participants