Skip to content

perf(parser): append sorted intervals without scanning the set - #1302

Open
zahariash wants to merge 1 commit into
LadybugDB:mainfrom
zahariash:perf/antlr-interval-append
Open

zahariash wants to merge 1 commit into
LadybugDB:mainfrom
zahariash:perf/antlr-interval-append

Conversation

@zahariash

Copy link
Copy Markdown
Contributor

This work was produced with the help of language models.

Fixes #1301. Part of #1290.

The first query in a process deserialized the lexer's ATN, which builds large Unicode character sets one interval at a time, and IntervalSet::add walked the whole interval list for each one: about half of the first query's 9.39 ms compile time for RETURN 1 (query-summary time, median of 8 processes). The 0.18 ms in #1290 is a later query.

Fix. An interval that goes into an empty set, or starts after the last interval without touching it, is appended directly; any other interval takes the existing loop, so the resulting set is the same. This is a local patch to the vendored runtime (ANTLR 4.13.1, marked in the source); upstream 4.13.2 and the dev branch have the same loop.

Performance. First query compile 9.39 → 5.16 ms, 77.7M → 42.0M instructions; later queries unchanged. Mallocs are unchanged too: about 68.4K for the first query (within run-to-run noise) and 601 per later RETURN 1 at one thread. The rest of the first compile is ANTLR filling its prediction cache on first use.

Tests. AntlrIntervalSetTest.AddKeepsCanonicalForm: random and sorted inserts plus touching intervals, against brute-force membership and the canonical sorted, disjoint, non-adjacent form. EofMergesOnlyWithTouchingRange: EOF (-1) merges with a range starting at 0 and stays separate from one starting at 1. EmptyIntervalAndRangeLimits: an empty interval is ignored, and appends and merges near SSIZE_MAX give the same set. All three fail if the fast path appends an interval that touches the last one. Full ctest on this branch: 3103/3103.

Independent of #1297 and #1300; with all three the first query compiles in 5.1 ms and executes in 0.33 ms (query-summary median of 8 processes).

The first query in a process deserializes the lexer's ATN, which builds
large Unicode character sets one interval at a time. IntervalSet::add
walked the whole list for each one, which was about half of the first
query's compile time. An interval that goes into an empty set, or starts
after the last one without touching it, is now appended directly; the
result is the same. This is a local patch on the vendored ANTLR 4.13.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

First query in a process takes 9 ms to compile, half of it building ANTLR character sets

1 participant