Skip to content

fix(query): overflow in capture pool ids + some perf#5555

Merged
WillLillis merged 3 commits into
release-0.26from
backport-5548-to-release-0.26
Apr 27, 2026
Merged

fix(query): overflow in capture pool ids + some perf#5555
WillLillis merged 3 commits into
release-0.26from
backport-5548-to-release-0.26

Conversation

@tree-sitter-ci-bot

Copy link
Copy Markdown

Description

Backport of #5548 to release-0.26.

WillLillis and others added 3 commits April 27, 2026 07:10
Commit 1f6eac5 ("query: Use uint32_t for capture list IDs") widened
QueryState.capture_list_id to uint32_t and removed the 65536 pool cap,
but left the pool function signatures as uint16_t. This caused silent
truncation when the pool exceeded 65535 entries, leading to a segfault.

(cherry picked from commit 361f293)
`ts_query_cursor_next_capture` linearly scanned all finished states to
find the one with the earliest next capture byte offset. With deeply
nested code, this O(n) scan per capture caused the highlight crate to
hang for minutes on large files.

Replace the linear scan with a min-heap over the finished_states array,
keyed by (next_capture_byte_offset, pattern_index, id). The heap is
maintained lazily: ts_query_cursor__advance uses plain array_push
(preserving FIFO insertion order), and next_capture sifts new elements
into the heap on entry via a tracked heap_size boundary. This preserves
the documented "order found" guarantee for next_match while giving
next_capture O(log n) per call.

(cherry picked from commit 123fb1c)
@WillLillis WillLillis merged commit b7964b9 into release-0.26 Apr 27, 2026
14 checks passed
@WillLillis WillLillis deleted the backport-5548-to-release-0.26 branch April 27, 2026 07:37
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