Reuse objects when inserting identical keys/values in PathCopyingPersistentTreeMap - #66
Open
PhilippWendler wants to merge 3 commits into
Open
PhilippWendler wants to merge 3 commits into
PhilippWendler wants to merge 3 commits into
Conversation
In this case we can reuse the same object instances as the input. This has no visible behavior change except that the map object sometimes remains the same instance after putAndCopy(). But this is not what anyone would rely on, as it is common for persistent data structures to do things like this. For example, removeAndCopy() also returns the same map object if there are no changes.
This affects cases where a new object that compares equal to an existing key object is used for insertion. Previously PathCopyingPersistentTreeMap would use the new key object, now we use the old key object. There are no strict rules on what to use, but this is what HashMap and TreeMap do (as seen in some new tests), and so it is not a bad idea to be consistent. Also it allows us to reuse the same object a little bit more often (if identical value with new key object is inserted).
Contributor
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.
This PR affects cases where objects are inserted into a
PathCopyingPersistentTreeMapand the value is identical (as defined by==) as the old value for the same key. Now we reuse the old key object (even if it is not the same instance as the newly given key object) and then can reuse also the sameNodeand map objects, saving a little bit of memory for the cost of a single==comparison on re-inserting with the same key.Map keys need to have well-defined equality (here actually
compareTo()because it is a sorted map), so code that would rely on getting a specific instance of a key and breaks with other instances of an equal key is broken anyway and relies on assumptions that were never guaranteed.Note that we do not pose any assumptions on value equality, as it is legal to put arbitrary objects as values into maps, even if they have broken/non-standard equals, and expect to get the same instance back as put last. This is for example necessary for safely putting mutable data structures like lists as values into a map.
The new behavior is the same as for JDK's
HashMapandTreeMap, and there are tests for confirming this.@lembergerth @baierd Might one of you want to review this?