-
-
Notifications
You must be signed in to change notification settings - Fork 166
optimize CompoundTag map compares #701
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
Open
hayanesuru
wants to merge
2
commits into
ver/1.21.11
Choose a base branch
from
opt/nbt
base: ver/1.21.11
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+45
−33
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,16 +3,14 @@ | |
| import com.google.common.collect.Interner; | ||
| import com.google.common.collect.Interners; | ||
| import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
| import it.unimi.dsi.fastutil.objects.ObjectIterator; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
|
|
||
| public class StringCanonizingOpenHashMap<T> extends Object2ObjectOpenHashMap<String, T> { | ||
|
|
||
| private static final Interner<String> KEY_INTERNER = Interners.newBuilder().weak().concurrencyLevel(16).<String>build(); | ||
| private static final Interner<String> KEY_INTERNER = Interners.newBuilder().weak().concurrencyLevel(16).build(); | ||
|
|
||
| private static String intern(String key) { | ||
| public static String intern(String key) { | ||
| return key != null ? KEY_INTERNER.intern(key) : null; | ||
| } | ||
|
|
||
|
|
@@ -29,32 +27,24 @@ public StringCanonizingOpenHashMap(int expectedSize, float loadFactor) { | |
| } | ||
|
|
||
| @Override | ||
| public T put(String key, T value) { | ||
| return super.put(intern(key), value); | ||
| } | ||
|
|
||
| @Override | ||
| public void putAll(Map<? extends String, ? extends T> m) { | ||
| if (m.isEmpty()) return; | ||
| ensureCapacity(size() + m.size()); | ||
| for (Map.Entry<? extends String, ? extends T> entry : m.entrySet()) { | ||
| super.put(intern(entry.getKey()), entry.getValue()); | ||
| public boolean equals(Object o) { | ||
| if (o == this) return true; | ||
| if (!(o instanceof Map<?, ?> m)) return false; | ||
| if (m.size() != size()) return false; | ||
| if (containsNullKey) { | ||
| if (!value[n].equals(m.get(key[n]))) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use |
||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void putWithoutInterning(String key, T value) { | ||
| super.put(key, value); | ||
| } | ||
|
|
||
| public static <T> StringCanonizingOpenHashMap<T> deepCopy(StringCanonizingOpenHashMap<T> incomingMap, Function<T, T> deepCopier) { | ||
| StringCanonizingOpenHashMap<T> newMap = new StringCanonizingOpenHashMap<>(incomingMap.size(), incomingMap.f); | ||
| ObjectIterator<Entry<String, T>> iterator = incomingMap.object2ObjectEntrySet().fastIterator(); | ||
|
|
||
| while (iterator.hasNext()) { | ||
| Map.Entry<String, T> entry = iterator.next(); | ||
| newMap.putWithoutInterning(entry.getKey(), deepCopier.apply(entry.getValue())); | ||
| final Object[] key = this.key; | ||
| for (int pos = n; pos-- != 0;) { | ||
| //noinspection ConstantValue | ||
| if (!((key[pos]) == null)) { | ||
| if (!value[pos].equals(m.get(key[pos]))) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return newMap; | ||
| return true; | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious about how much improvement this patch can bring, as we ignored the load factor of the original map when copying, rehashing cost is expensive.
I suggest add another method
getLoadFactorin theStringCanonizingOpenHashMapto reuse load factor