Skip to content
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

Curious performance issue when keyword matches potential variable #5

Open
nighthawk opened this issue Aug 22, 2019 · 2 comments
Open

Comments

@nighthawk
Copy link

I built an expression library using Eval with about 50 operators (most of them taken from sample code – thanks a lot for that!) and noticed a curious drop in performance when defining a prefix function like not X and then having an expression like nothing == true where nothing could potentially be a variable. The performance drop is quite significant in my case, where by having adding that not prefix function slows down the overall evaluation by a factor of 5-6 compared to just having a ! prefix function.

I wonder if the evaluate method of the interpreter could be improved by introducing at the appropriate place a check that a function name shouldn't be evaluated against a substring. Though that might be done on purpose for some use case. In that case an alternative would be to provide a PatternOptions which allows telling the parser that this specific function keyword can't be a substring.

I can try to do those adjustments, though I'm not sure where to do them in the code. Any hints would be appreciated.

Some sample code which reproduces the slowdown (less than in my big library, but still by a factor of 3): https://gist.github.com/nighthawk/c7daa27285da406e5b2a71f8b789bf3f

@tevelee
Copy link
Owner

tevelee commented Aug 22, 2019

Thanks a lot for your feedback and detailed bug report! It’s a really interesting issue indeed, I’ll look into it as soon as possible

@nighthawk
Copy link
Author

Great, thanks. The behaviour makes sense when thinking about a negation operator and doing something like !foo but would be good to disable this for word-only operators. Or maybe the parser can detect word boundaries so that it knows !foo can really be treated like ! foo.

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

No branches or pull requests

2 participants