-
-
Notifications
You must be signed in to change notification settings - Fork 246
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
conditions memoization #600
base: master
Are you sure you want to change the base?
conditions memoization #600
Conversation
This is pretty much what I had in mind as well, very nice 👍 The overhead of using memoization would be interesting, should be measurable by adding a bunch of snippets with the same trigger, but different conditions. |
Ah, we should consider passing the cursor-position through from |
Oh yes, sounds reasonable. Then we'll need some changes in he code after all (and think about if we want to make these args accessible for normal conditions as well (but I've got no real idea how to hide them tbh)) |
True, but as long as they don't explicitly accomodate the memoization, I'm happy :D It should be alright to just pass the cursor behind the other stuff, and it could actually come in useful for regular conditions, so there's no reason to hide it |
Also, remember that lua boolean operators don't do short-circuit, so this is basically doing all the calls all the time, I think it will be a good idea to do the short-circuit ourselves and advise the users to put the heavier conditions at the end of the chain. Forget my comment, it does shortcut, I'm just speaking nonsense. |
Are you sure they don't short-circuit? |
Forget my comment, I'm definitely mistaking languages features. |
Just collected some TODOs in the initial PR message (let me know if I'm missing something). What other logic operators do you think are usefull? ( What parameters for invalidation do you think are useful? (I still have to read about things like change ticks as my vim-lib background is pretty weak) Should we make the changes to the luaSnip core (add additional parameters to condition function) in another PR? EDIT: And we can already think of some useful conditions besides |
I'm using |
e182352
to
4e13374
Compare
All the |
And I just though about invalidation conditions like same line. Not quite sure if it's done by tracking the number of the line. In most cases one would have some condition like |
Can't think of any others :/
Mhmmmm would be a tad cleaner, but I won't complain if you tack it on here, it originates here after all
I started looking into treesitter-conditions in
Those would have to be invalidated on pretty much any change, unfortunately. |
line_start would also depend on the cursor-position inside the line :/ In general, having nvim push the invalidations to us (via We should really start measuring the impact of condition-evaluations before focusing this much on the memoization-options, it will most likely only be worth it if multiple snippets have the same condition and trigger (because only then will a condition be evaluated twice for a single expand-call) For example, via diff --git a/lua/luasnip/init.lua b/lua/luasnip/init.lua
index a79ddc5..390ba47 100644
--- a/lua/luasnip/init.lua
+++ b/lua/luasnip/init.lua
@@ -27,11 +27,15 @@ end
-- parameters(trigger and captures). params are returned here because there's
-- no need to recalculate them.
local function match_snippet(line, type)
- return snippet_collection.match_snippet(
+ require("jit.p").start("10,i1,s,m0,G", "match")
+ local match, expand_params = snippet_collection.match_snippet(
line,
util.get_snippet_filetypes(),
type
)
+ require("jit.p").stop()
+
+ return match, expand_params
end
-- ft: (I get no samples at all, but I also don't have snippets with conditions) |
I'd say pass them in a table, that would at least be comfortable. (maybe create one table per condition and reuse it (don't create a table each call), but I'd say we measure first) |
Can't find |
👍
I agree, why close this interface for the user (providing custom
Do you refer to switching trigger checking and condition checking?
I guess we should make a testing branch which every user can checkout if they want to provide us with data. Any ideas on how to get to the users? (making an announcement) |
Having all this discussion on memoization/invalidation, maybe we should outsource the implementation of condition objects and the logic operators to another (new) PR and handle memoization separately (still in this PR). |
No idea how you want to share a table with is simply used to pass arguments 🤷, but that's for later |
Just not sure about the vim stuff. But do you think there is any way a condition could change without setting another But indeed we should run some benchmarks to check if memoization even has the potential to give some performance benefit (tbh I doubt it based on our current discussion, memoization sounds great but I don't think it's worth it. Maybe it's just a think to keep in the back of our mind to come back if someday this becomes an issue). |
Ah, my bad, it's a branch that I thought I'd pushed already 😅 |
We could invalidate on "buffer" by actually clearing the value of conditions instead of checking the condition-function (otoh, this might also clean to often, maybe there won't be any expansions in the new buffere, and then we'd cleaned it for naught). But this is the only one where I can actually think of an optimization
Yeah, that makes sense, the logic-operators are pretty much complete, so no reason to wait with merging them.
:D ...
invalidate_args = {
line = ...,
cursor = ...
...
}
if invalidate(invalidate_args) then ... end so we'd create a new table for each of those calls, but we could instead store this table in the condition-object, and just set the new values ...
self.invalidate_args.line = ...
self.invalidate_args.cursor = ...
...
if invalidate(self.invalidate_args) then ... end Not sure if this has any performance-impact at all, I just thought of it and wanted to share :D
Well, maybe, depends on the condition. For example if
Yup :/ |
Me neither (basically the question is if allocate + init is faster than assigning elements)
Oh yes of course. |
I'd suggest we keep this in the back of our mind, if someone reports bad performance on conditions (I see no point in doing memoization if there is no real benefit (ofc would be cool to simply have it, but for that it's just too much work I think)). We'd need user support for benchmarking anyways. So I'd close this PR, but maybe we could add a label (maybe something like |
Agreed 👍 I'd close it just in spirit, leaving it as draft seems the perfect thing to do, since we'll need to do more investigation to do this properly (or, to find out if this needs doing properly :D) |
Fine with that. Just thought properly close this might avoid cluttering the PR section. But you're also right, closed PRs might get forgetten |
first draft of memoization and condition objects as suggested in #592.
Just a draft PR so we have a place for discussion. If you've another idea of implementing, we cen delete/overwrite this one, no problem ; )
Collection of TODOs:
xnor