Skip to content

DepMgr hashCode handling#749

Merged
cstamas merged 5 commits intoapache:masterfrom
cstamas:perf-issue
Jun 12, 2025
Merged

DepMgr hashCode handling#749
cstamas merged 5 commits intoapache:masterfrom
cstamas:perf-issue

Conversation

@cstamas
Copy link
Member

@cstamas cstamas commented Jun 12, 2025

As graph is growing, same instances of HashMaps are repeatedly asked for hashCode, that in default implementation goes over all keys and values over and over again.

Changes:

  • fix missing entry in equals as @mbien reported
  • "invent" a special construct that fits this very use case: map is modified once, and copied if next level is about to modify it
  • the construct once asked for hashCode, memoizes it (the "read only" check never happens in this code, is just there to be foolproof)

@cstamas cstamas self-assigned this Jun 12, 2025
@cstamas cstamas changed the title Collection hashCode DepMgr hashCode handling Jun 12, 2025
Comment on lines +97 to +104
if (hashCode == null) {
synchronized (delegate) {
if (hashCode == null) {
hashCode = delegate.hashCode();
}
}
}
return hashCode;
Copy link
Member

Choose a reason for hiding this comment

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

i would probably use the simple "racy" version of this similar like it used to be:

public int hashCode() {
if (hashCode == 0) {
hashCode = Objects.hash(depth, managedVersions, managedScopes, managedOptionals, managedExclusions);
}
return hashCode;
}

this should be safe since:

  • doc requires managers to be stateless, I assume this means all maps are immutable
  • this will be likely only used from one thread
  • race condition would not be a big deal since all it would to is to introduce a chance that the value is computed more than once under high contention

Copy link
Member Author

Choose a reason for hiding this comment

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

I have other idea, will add done() method....

@cstamas cstamas marked this pull request as ready for review June 12, 2025 20:32
@cstamas cstamas requested review from gnodet and mbien June 12, 2025 20:33
Copy link
Member

@mbien mbien left a comment

Choose a reason for hiding this comment

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

makes sense to me, left one comment.

Comment on lines +68 to +70
public MMap<K, V> done() {
return new DoneMMap<>(delegate);
}
Copy link
Member

Choose a reason for hiding this comment

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

    return this instanceof DoneMMap ? this : new DoneMMap<>(delegate);
}

otherwise this would keep copying the maps even if nothing changed.

Copy link
Member Author

Choose a reason for hiding this comment

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

but done method is overridden in DoneMMap? I don't get it

Copy link
Member

@mbien mbien Jun 12, 2025

Choose a reason for hiding this comment

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

wait maybe I am wrong here - this likely can't happen since done() of the frozen instance is a no-op

Copy link
Member

Choose a reason for hiding this comment

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

sorry!

@cstamas cstamas merged commit 93bd1cb into apache:master Jun 12, 2025
8 checks passed
@cstamas cstamas deleted the perf-issue branch June 12, 2025 21:16
@github-actions github-actions bot added this to the 2.0.10 milestone Jun 12, 2025
@mbien
Copy link
Member

mbien commented Jun 12, 2025

it will copy the map twice now though.

first:
MMap.copy(this.managedExclusions);

second later:
managedExclusions.done()

which will hit the same copy constructor again.

@cstamas
Copy link
Member Author

cstamas commented Jun 12, 2025

it will copy the map twice now though.

first: MMap.copy(this.managedExclusions);

second later: managedExclusions.done()

which will hit the same copy constructor again.

#750

@cstamas cstamas added enhancement New feature or request and removed maintenance labels Jun 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants