-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Second round of performance profiling #691
Comments
JSON direct syntactic analyzer (layer that translates tree-sitter CST into generic ApiDOM). Here are some numbers how ApiDOM parsing speed increased after this perf optimization. Before
After
|
JSON indirecty syntactic analysis optimization results: Before
After
|
YAML syntactic analysis optimization results: Before
After
|
During the weekend I did extensive optimizations on all our syntactic analyzers - YAML and JSON. I managed to increase the syntactic analysis phase by another factor of 2 - parsing is now two times faster again. It's not much, but it's something at least... During the optimizations I've managed to identify the biggest performance problem that we currently have. It's unfortunately not in our code, but in tree-sitter binding code. It takes extreme amount of time to access tree-sitter CST nodes and it's attributes. On relatively small CST trees (300 lines of YAML), the traversal of CST takes more than 20 ms. Access time of tree sitter CST seems to be linear, so for 3000 lines of YAML we get around 200 ms of just traversal time. I've created an issue with the tree-sitter, to find out If I'm doing something wrong or this is something expected. Just for comparison:
|
tree-sitter cursor traversal needs to be used to get the performance we need. Full CST traversal is around 10x faster when used. POC of cursor traversal: const cursor = cst.walk();
let reached_root = false;
while (!reached_root) {
//github.com/tree-sitter/py-tree-sitter/issues/33
https: const type = cursor.nodeType;
console.dir(cursor.nodeText);
if (cursor.gotoFirstChild()) {
continue;
}
if (cursor.gotoNextSibling()) {
continue;
}
let retracting = true;
while (retracting) {
if (!cursor.gotoParent()) {
retracting = false;
reached_root = true;
}
if (cursor.gotoNextSibling()) {
retracting = false;
}
}
} |
In #933 I've implemented two specialized iterators that provide optimized access to tree-sitter CST. Accessing the tree-sitter CST via cursor mechanism is 10 times faster. As cursor traversal is not compatible with our own ApiDOM traversal, adapter in form of two (2) iterators have been created. PreOrderCursorChildrenIteratorThis iterator uses Preorder Depth First traversal to create tree structure compatible with tree-sitter Tree using tree-sitter cursor traversal mechanism which provides cached and optimized access to CST. PreOrderCursorIteratorThis iterator uses Preorder Depth First traversal to create list of tree-sitter SyntaxNode like structures using tree-sitter cursor traversal mechanism which provides cached and optimized access to CST. Currently these iterators are not yet utilized as their utilization is blocked by the following PR I've issued for tree-sitter Node.js binding. If we utilized them now without the PR we'll see performance degradation in parsing speed for Node.js env, but increased performance for Browser env (which might be acceptable for now). Performance improvementAccording to benchmarks and profiling I've made, by using iterators the performance increased by around 400% (4 times), from 45 ops/s to 171 ops/s. |
Current status: waiting for tree-sitter/node-tree-sitter#96 to be merged to utilize the cursor traversal iterators and increase the parsing speed. |
tree-sitter/node-tree-sitter#96 has been finally merged. We need to wait for new release of |
|
[email protected] has been released and tree-sitter cursor traversal has been utilized in |
This issue is specific to
apidom-parser-adapter-json
, but the optimization that comes out of this issue could be possibly used in every package. It seems that we spend a lot of time in syntactic analysis phase, specifically in building minim objects. This needs to be inspected and profiled; it seems that the building time is unreasonably long.Node internal profiler mechanism is used to identify code which needs to be optimized.
Refs #385
The text was updated successfully, but these errors were encountered: