Skip to content
Permalink
Browse files

[JENKINS-18377] Cache Role#hashCode to speed up RoleMap#getRolesHavin…

…gPermission

I have experienced severe slowdown caused by following stacktrace:

        at java.util.AbstractSet.hashCode(AbstractSet.java:126)
	at com.michelin.cio.hudson.plugins.rolestrategy.Role.hashCode(Role.java:149)
	at java.util.HashMap.hash(HashMap.java:338)
	at java.util.HashMap.put(HashMap.java:611)
	at java.util.HashSet.add(HashSet.java:219)
	at com.michelin.cio.hudson.plugins.rolestrategy.RoleMap$1.perform(RoleMap.java:310)
	at com.michelin.cio.hudson.plugins.rolestrategy.RoleMap$RoleWalker.walk(RoleMap.java:387)
	at com.michelin.cio.hudson.plugins.rolestrategy.RoleMap$RoleWalker.<init>(RoleMap.java:376)
	at com.michelin.cio.hudson.plugins.rolestrategy.RoleMap$1.<init>(RoleMap.java:307)
	at com.michelin.cio.hudson.plugins.rolestrategy.RoleMap.getRolesHavingPermission(RoleMap.java:307)
	at com.michelin.cio.hudson.plugins.rolestrategy.RoleMap.hasPermission(RoleMap.java:107)
	at com.michelin.cio.hudson.plugins.rolestrategy.RoleMap.access$000(RoleMap.java:75)
	at com.michelin.cio.hudson.plugins.rolestrategy.RoleMap$AclImpl.hasPermission(RoleMap.java:362)
  • Loading branch information...
olivergondza committed May 29, 2017
1 parent 8dbec9e commit 161c0358179a4c845d688b3e42db09f8aa91739d
Showing with 9 additions and 0 deletions.
  1. +9 −0 src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/Role.java
@@ -53,6 +53,8 @@
*/
private final Set < Permission > permissions = new HashSet < Permission > ();

private transient Integer cachedHashCode = null;

/**
* Constructor for a global role with no pattern (which is then defaulted to
* {@code .*}).
@@ -143,6 +145,13 @@ public int compareTo(Object o) {

@Override
public int hashCode() {
if (cachedHashCode == null) {
cachedHashCode = _hashCode();
}
return cachedHashCode;
}

private int _hashCode() {
int hash = 7;
hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 53 * hash + (this.pattern != null ? this.pattern.hashCode() : 0);

0 comments on commit 161c035

Please sign in to comment.
You can’t perform that action at this time.