Skip to content

optimize CompoundTag map compares#701

Open
hayanesuru wants to merge 2 commits intover/1.21.11from
opt/nbt
Open

optimize CompoundTag map compares#701
hayanesuru wants to merge 2 commits intover/1.21.11from
opt/nbt

Conversation

@hayanesuru
Copy link
Copy Markdown
Collaborator

@hayanesuru hayanesuru commented Apr 10, 2026

#646

avoid map entry and iterator overhead

inline StringCanonizingOpenHashMap#deepCopy

@hayanesuru hayanesuru requested a review from HaHaWTH April 10, 2026 15:23
@hayanesuru hayanesuru added the type: optimization Pull request for optimization label Apr 10, 2026

@Override
public CompoundTag copy() {
+ // Leaf start - Further reduce memory footprint of CompoundTag
+ if (this.tags instanceof org.dreeam.leaf.util.map.StringCanonizingOpenHashMap<Tag> stringCanonizingTags) {
+ return new CompoundTag(org.dreeam.leaf.util.map.StringCanonizingOpenHashMap.deepCopy(stringCanonizingTags, Tag::copy));
+ org.dreeam.leaf.util.map.StringCanonizingOpenHashMap<Tag> ret = new org.dreeam.leaf.util.map.StringCanonizingOpenHashMap<>(stringCanonizingTags.size(), 0.8f);
Copy link
Copy Markdown
Member

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 getLoadFactor in the StringCanonizingOpenHashMap to reuse load factor

final Map<?, ?> m = (Map<?, ?>)o;
if (m.size() != size()) return false;
if (containsNullKey) {
if (!value[n].equals(m.get(key[n]))) {
Copy link
Copy Markdown
Member

@HaHaWTH HaHaWTH Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use Objects.equals here to guard against null values? And use m.containsKey(key) to ensure the key exists, null == null is always true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: optimization Pull request for optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants