Repository navigation
Simplify and optimize Util.falsey? - #24
Merged
Merged
Conversation
The two first cases (nil and false) are equivalent to !value, the three following (String, Hash, Array) are covered by #empty? and the last case (Struct) can use #any? These optimizations aren't huge in terms of operations per second, but they avoid allocating strings, arrays and hashes, which means that the GC will have to work less, leading to better overall performance.
Contributor
|
Thanks for the tuning work. This should be part of the 1.3 release shortly. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I noticed that there was a new utility function that encapsulated the JMESPath rules for false. It's great that an important definition like that is not repeated over and over again.
However, I think the implementation for an often used function like this needs to take performance into account. Using literals in Ruby is problematic since they're allocated on each call, leading to unnecessary garbage being created.
I rewrote the code to avoid allocations, and also simplified it slightly.
These are benchmarks for a few different inputs before these changes (the full benchmark code is below).
And these are the numbers with the changes applied:
Notice especially the difference for the non-empty array hash cases. Checking that a hash is empty is much better than checking whether or not it is equal to another hash.
The benchmarks of course don't capture the most important aspect of this, GC pressure.
This is the full benchmark code:
We're have systems that are doing thousands, of not tens of thousands of JMESPath searches per second, so every little bit of performance counts.