# language: Fix slow Tree-sitter parsing \(\#52674\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/6cdf954e2ce0e1a6b83ce116b81258d3b512adf1)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 6cdf954e2ce0e1a6b83ce116b81258d3b512adf1

Requested commit: 6cdf954e2ce0e1a6b83ce116b81258d3b512adf1

Commit: 6cdf954e2ce0e1a6b83ce116b81258d3b512adf1

Tree: ee439b1a5bfc34d01401337212eb3bc685a40ed1

Author: Om Chillure

Committer: GitHub

## Message

```
language: Fix slow Tree-sitter parsing (#52674)

#### Context

When tree-sitter parses a file with broken syntax (e.g. a large partial
SQL `VALUES` clause, or any language where a large chunk becomes
invalid), it can produce a single `ERROR` node spanning thousands of
lines. On every render frame, Zed queries this tree for syntax
highlights via `SyntaxMapCaptures`. Previously, only `set_byte_range`
was applied to the query cursor - this limits which captures are
*returned*, but tree-sitter still had to *traverse* the entire ERROR
subtree to find them, causing O(file size) work per frame and making
scrolling/editing visibly laggy.

The fix applies `set_containing_byte_range` to the highlight query
cursor, mirroring what `SyntaxMapMatches` already does for indentation
and bracket queries. This tells tree-sitter to skip subtrees that extend
far beyond the visible window, reducing traversal to the visible range
only.

**Note:** This fix eliminates the main freeze/stall caused by full-tree
traversal. A small amount of lag may still occur on very large broken
files, as tree-sitter still needs to parse the error-recovery structure.
Further improvements would require deeper changes to tree-sitter's query
execution or incremental parsing.


#### Closes #52390

#### How to Review

Small change — focus on
[syntax_map.rs:1119-1123](crates/language/src/syntax_map.rs#L1119) (the
fix) and the `containing_byte_range_for_captures` helper below it.
Compare with the existing `SyntaxMapMatches::new` path (line ~1255)
which uses the same pattern.

#### Self-Review Checklist

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

#### Video 
[Screencast from 2026-03-26
14-19-19.webm](https://github.com/user-attachments/assets/6628492a-f013-438a-836a-2740f6e2f266)


#### Note : Reopens previous work from closed PR #52475 (fork was
deleted)

Release Notes:

- Fixed laggy scrolling and editing in files with large broken syntax
regions (e.g. incomplete SQL `VALUES` clauses or large invalid blocks in
any language)

---------

Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
```

## Parents

- [bcaa1b5231fc804c8b02517ada62fd48cccfa371](https://git.cafe/gitcafe/zed/commit/bcaa1b5231fc804c8b02517ada62fd48cccfa371?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/6cdf954e2ce0e1a6b83ce116b81258d3b512adf1?format=markdown)
