Replace operation limit API with a clock-based timeout API #301
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
When I initially implemented the multi-threaded parsing support in Atom, I hacked in a feature called a "parsing operation limit" in order to avoid unnecessary threading overhead in the case of fast parsing. At the time, I avoided using actual time as the limiting "unit", because I wanted to avoid introducing any system call overhead during parsing.
Improvements
In this PR, I've replaced the
ts_parser_set_operation_limit
function with a new function calledts_parser_set_timeout_micros
. This new fuction allows you to provide a timeout value in microseconds instead of an abstract "operation limit".I've also added a
--timeout
flag to thetree-sitter parse
command so that we can easily test the behavior of timeouts at the command line.Implementation
The new timeout functionality relies on the
clock(3)
POSIX API and theCLOCKS_PER_SEC
constant. I initially found that callingclock()
on every parse action imposed a huge amount of overhead, so I added a counter to ensure thatclock()
is only called every 100 parse actions. This change eliminated any observable performance overhead.