Skip to content

Add thread-safe TreeSitterPool for concurrent parsing#30

Merged
andreaTP merged 1 commit into
roastedroot:mainfrom
andreaTP:add-treesitter-pool
Jun 24, 2026
Merged

Add thread-safe TreeSitterPool for concurrent parsing#30
andreaTP merged 1 commit into
roastedroot:mainfrom
andreaTP:add-treesitter-pool

Conversation

@andreaTP

Copy link
Copy Markdown
Collaborator

Port the pool implementation from ch007m/java-tree-sitter#6. Uses Semaphore + ConcurrentLinkedDeque (no synchronized blocks) so it is safe with virtual threads. Includes tests for borrow/return, reuse, discard, concurrency limiting, and parallel parsing.

Supersede #13

Port the pool implementation from ch007m/java-tree-sitter#6. Uses
Semaphore + ConcurrentLinkedDeque (no synchronized blocks) so it is
safe with virtual threads. Includes tests for borrow/return, reuse,
discard, concurrency limiting, and parallel parsing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@andreaTP andreaTP requested a review from cmoulliard June 24, 2026 11:15

@cmoulliard cmoulliard left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe a silly question: What is finally the benefit to have in a pool x instances of treesitter wasm runtime vs having a pool of the parsers that we can use when we would like to parse in parallel string (java, xml, json, etc code) ?

Comment thread Readme.md
Comment on lines +62 to +69
try (TreeSitterPool pool = TreeSitterPool.create(4)) {
try (TreeSitterPool.Loan loan = pool.borrow()) {
try (TreeSitterParser parser = loan.instance().newParser(Language.JAVA);
TreeSitterTree tree = parser.parseString(source)) {
System.out.println(tree.rootNode().toSexp());
}
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we add a comment here to tell to the users that if they try to create 5 parsers: json, xml, yaml, properties, java and markdown, then the semaphore will reject ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good question! The pool size limits concurrent borrows, not languages. Each TreeSitter instance can parse any language, so a pool of 4 can serve all 6 grammars — borrow() only blocks when all instances are already in use. I've updated the docs to clarify this.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks to clarify

@andreaTP

Copy link
Copy Markdown
Collaborator Author

What is finally the benefit to have in a pool x instances of treesitter wasm runtime vs having a pool of the parsers that we can use when we would like to parse in parallel string (java, xml, json, etc code) ?

Just a different level of granularity, also the two options are not exclusive

@cmoulliard

Copy link
Copy Markdown
Collaborator

Just a different level of granularity, also the two options are not exclusive

Something to be implemented maybe in a separate PR ?

@andreaTP

Copy link
Copy Markdown
Collaborator Author

maybe in a separate PR ?

I think it's a "user"'s concern, but not fussy 🤷

@cmoulliard

Copy link
Copy Markdown
Collaborator

LGTM. Approved

@andreaTP andreaTP merged commit 6886664 into roastedroot:main Jun 24, 2026
2 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.

2 participants