Rob Hitchens patch -1. Performance and readability. #1
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.
Removed some unnecessary crawling and redundant comparisons for gas optimization.
Added "exists()" and explicit checks to prevent duplicate keys. Replaced z,y,z variables with more descriptive labels to help prepare the rotation and fixup sections for an audit. Removed family-tree items (siblings, grandparents, uncle, etc.) because these are not used by the algo and are of little interest except for debugging (reduces complexity for audit).
This makes it more like a black box with a minimal surface.
Removed the counter because it can be safely externalized. A client app can track the count if it needs to. That squeezes a little more gas out if no one needs the count.
Set up minimalist event logs. The tree itself emits no event, for gas-optimization. A log is implemented at the library client level and reports only the coming and going of key/values which is sufficient to reconstruct an ordered list. This approach is consistent with a philosophy of emitting an event for every important state change. Logs could, arguably, be implemented inside the library to force log emissions but that could be too opinionated and would probably be redundant when clients emit application-level events. We could say the library itself emits no events but the client should, as shown in the example.
Hope it helps.