LANG-1230: Remove unnecessary synchronization from registry lookup in EqualsBuilder and HashCodeBuilder (closes #143)

This commit is contained in:
Philippe Marschall 2016-05-16 10:30:02 +02:00 committed by pascalschumacher
parent a06c99b9c0
commit b2c1afce7e
2 changed files with 17 additions and 27 deletions

View File

@ -171,14 +171,12 @@ static boolean isRegistered(final Object lhs, final Object rhs) {
* @param lhs <code>this</code> object to register
* @param rhs the other object to register
*/
static void register(final Object lhs, final Object rhs) {
synchronized (EqualsBuilder.class) {
if (getRegistry() == null) {
REGISTRY.set(new HashSet<Pair<IDKey, IDKey>>());
}
private static void register(final Object lhs, final Object rhs) {
Set<Pair<IDKey, IDKey>> registry = getRegistry();
if (registry == null) {
registry = new HashSet<Pair<IDKey, IDKey>>();
REGISTRY.set(registry);
}
final Set<Pair<IDKey, IDKey>> registry = getRegistry();
final Pair<IDKey, IDKey> pair = getRegisterPair(lhs, rhs);
registry.add(pair);
}
@ -195,17 +193,13 @@ static void register(final Object lhs, final Object rhs) {
* @param rhs the other object to unregister
* @since 3.0
*/
static void unregister(final Object lhs, final Object rhs) {
private static void unregister(final Object lhs, final Object rhs) {
Set<Pair<IDKey, IDKey>> registry = getRegistry();
if (registry != null) {
final Pair<IDKey, IDKey> pair = getRegisterPair(lhs, rhs);
registry.remove(pair);
synchronized (EqualsBuilder.class) {
//read again
registry = getRegistry();
if (registry != null && registry.isEmpty()) {
REGISTRY.remove();
}
if (registry.isEmpty()) {
REGISTRY.remove();
}
}
}

View File

@ -497,13 +497,13 @@ public static int reflectionHashCode(final Object object, final String... exclud
* @param value
* The object to register.
*/
static void register(final Object value) {
synchronized (HashCodeBuilder.class) {
if (getRegistry() == null) {
REGISTRY.set(new HashSet<IDKey>());
}
private static void register(final Object value) {
Set<IDKey> registry = getRegistry();
if (registry == null) {
registry = new HashSet<IDKey>();
REGISTRY.set(registry);
}
getRegistry().add(new IDKey(value));
registry.add(new IDKey(value));
}
/**
@ -518,16 +518,12 @@ static void register(final Object value) {
* The object to unregister.
* @since 2.3
*/
static void unregister(final Object value) {
private static void unregister(final Object value) {
Set<IDKey> registry = getRegistry();
if (registry != null) {
registry.remove(new IDKey(value));
synchronized (HashCodeBuilder.class) {
//read again
registry = getRegistry();
if (registry != null && registry.isEmpty()) {
REGISTRY.remove();
}
if (registry.isEmpty()) {
REGISTRY.remove();
}
}
}