Repository navigation
Implement Query API - #62
Conversation
|
Hi, sorry for the delay in response. Nice work on this so far. I would definitely be open to merging this. I think that I would prefer for the API to match the the query API in the WASM binding. Unfortunately, it's not really documented right now; the best way to understand it is to read the test file. Summary:
Thoughts? |
|
Also, I'm just curious if you tried using the wasm-based bindings ( |
I haven't tried the wasm version. I remember seeing somewhere that the performance of the wasm version was slower than the native one, thus why I'm interested in this one. If that claim is not applicable anymore I could try the wasm bindings. I'll reply to the other points, I should have more time in a few days. |
|
Hello, I'm also very interested in the query API, and I will be happy to help! |
I'm actually not sure that comment is still true (or ever was true for most use cases). It might be worth doing a quick performance comparison in your application. |
|
Good to know! I will give a try. |
|
Unfortunately, I was unable to load the Ruby Language using wasm. |
|
Hey @maxbrunsfeld, sorry for the delay, there is never enough time to do everything :/ So about your comments, here are my thoughts: QueryCursor
Sounds good to me, nothing is gained by exposing the query cursor to the users. .matches and .captures
Sounds good as well, though I would still keep Query API
On this one I think I'd prefer to keep Question: predicatesIf it's ok with you I'd also bump the tree-sitter version to include tree-sitter/tree-sitter#615 Let me know what you think on the above comments and I'll try to finish the PR in the next week. |
Yes, you are absolutely right! And 👍 to all of your suggestions.
That's right. Ideally, we would handle the |
|
So the way the node marshalling is setup, when sending a list of nodes from C++ to JS, C++ for each nodes returns either the nodeTypeID or the cached node object. However the queries API does something that doesn't happen otherwise: it can return the same node multiple times (if multiple pattern match). So C++ sends multiple times the same node (serialized in assert(!tree->cached_nodes_.count(key)); // tree.cc : 230 -_-There were a couple ways to solve it, I'm quite hesitant about my implementation, I'd like your input. In JS's A few other solutions:
Could you let me know what you think of it? I'll gladly rework this. Other questions/comments:
|
|
@maxbrunsfeld I really hope this will get merged soon, it will open so many new doors! |
I think your solution is pretty good. It looks like
I am also open to this solution if you think it would be cleaner.
I don't want to do this because instantiating objects in C++ is much slower than doing so in JavaScript. It essentially always goes through the slowest-possible path (a hash-table like interface, instead of using hidden classes).
But how would you prevent the
This means that Tree-sitter API failed to return a node. For example, if you call
That sounds great! |
|
Ok, well if the
Good you mention it, I had no idea. And there's no way to hint V8 to use a hidden class? Eg class Hint {
field1 = 0;
field2 = 0;
}
bindings.setHintFunction(Hint);auto instance = Nan::NewInstance(hintClass);
Nan::Set(instance, Nan::New("field1").ToLocalChecked(), Nan::New(42));I'm asking because currently
Any preference? I've added the predicates matching by copying most of the code from the WASM implementation, everything seems to be working fine. Remaining tasks:
|
|
After getting a feel for the API, I've removed Everything else has been implemented, note that I took the tests from the WASM bindings so everything is as similar as possible. The only remaining issue is the matches & captures marshalling. I've removed the object creation in C++ and replaced it with arrays as it was the easiest solution. I've done some testing and it does shave off some time. Obviously the cost of creating and GC'ing a lot of temporary arrays is always there (and not visible in my tests), so let me know if you'd rather have me transfer everything in a buffer I'll make time to implement it. Edit: I've taken the liberty to include a fix for #66 which is a leak I think. |
maxbrunsfeld
left a comment
There was a problem hiding this comment.
This is a really awesome PR. Thanks for taking on this very non-trivial task! I just left a couple of very minor suggested changes, and one optimization idea which I think you could take or leave.
| }; | ||
|
|
||
|
|
||
| /* |
There was a problem hiding this comment.
Could you 🔥 the second blank line here, so there's only one blank line between declarations?
|
Ok, so I've done the changes, everything should be good now. I've kept the transfer in a flattened JS array instead of a C buffer. Two minor optimizations, first when I've ran some tests (parsing Second optimization is that I don't pass // first match
5, // pattern index
"foo",
"bar,
// second match
7, // pattern index
"baz,
]And the captures are accumulated with while (typeof returnedMatches[i] === 'string') // ...Didn't expect it to affect the result that much but this gives a ~9% gain (for |
|
I've been waiting for this to be merged for about a month now, is there anything holding it up? In the meantime I've tried using @romgrk 's fork directly, but run into some difficulties building (not too familiar with node-gyp, and |
|
Ah, sorry about the delay. I missed that last comment going by. Thanks for the PR @romgrk. |
|
This is published in 0.16.2. |
|
Can I use this from web-tree-sitter somehow? Doesn't seem like I can update that one, might be missing something here. |
|
This is for the Node tree-sitter binding. The query API already exists in web-tree-sitter. |
|
Thanks for the feedback & help @maxbrunsfeld! |
* queries: initial implementation * queries: add predicates * test: setup query tests * package: add build script * query: display TSQueryError name instead of value * tree-sitter: update to latest * query_cursor: remove & move .exec to query.cc * query: continue implementation of .exec * test: adjust query_test.js to match current API * lint: remove unused module * node_methods: expose GetMarshalNodes * lint: add section comments * query: implement Query.matches * query: implement Query.captures * query: implement predicates filtering * query: make Query::GetPredicates warmer * query: fix predicates initialization * tests: unskip query.captures * query: implement range searching * query: remove Query.exec * query: use arrays for marshalling * node: fix tree-sitter#66 free transfer_buffer * tests: fix query_test labels * query: flatten intermediate array * query: dont transfer capture_count * unmarshalNodes: pass nodes as array * lint * query: remove unused values * query: avoid out-of-bound access
Hey,
I need access to the query API for a project, here is a draft implementation of it. Are you interesting in including it in the main repo?
Thanks.
Fixes #56