leveldb/memdb: avoid repeated comparisons #384
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.
When the memory database does a search through the skiplist, it descends one level whenver it either finds that the list is ended, or that the comparison fails (we've passed the item).
In the latter case, we descend a level, and a non-insignificant amount of times, the search on that level will also end on the same exact item -- since if the item was at the upper level, it's guaranteed to be present on the lower level too.
By adding one local int, to keep track of, basically, "an item we found on the upper level, which is known to be past the item we're searching for", we can avoid doing that comparison again.
Using this fix, plus a global comparison counter, I made these charts. It imports
30M
items, each with a32
byte key and a1
byte value. At 100 points during this import, I dump out the average comparison ops performed in that range.This is
master
It executed it in
175.91 seconds
, doing1416736538
comparison calls in total.This is this PR:
It executed it in
171.51 seconds
, doing1333783947
comparison calls in total.