Miguel Munoz created LANG-1493:
----------------------------------
Summary: Performance of Reflection for equals and hash code can be
improved over EqualsBuilder
Key: LANG-1493
URL: https://issues.apache.org/jira/browse/LANG-1493
Project: Commons Lang
Issue Type: New Feature
Components: lang.*
Reporter: Miguel Munoz
The EqualsBuilder class is very inefficient. It reflects through all the fields
of a class every time it needs to do a comparison. A faster approach would be
to do the reflection once when the class loads, and re-use it for each
instance. This change, however, needs a different API and couldn't be a drop-in
replacement for EqualsBuilder. Instead it would need to be a new class.
I have written a class that can do this. Performance tests show that it runs
up to 20 times faster, depending on how many fields it needs to compare before
finding a difference.
You can try the class out by going to [https://github.com/SwingGuy1024/DogTags].
The API currently works like this:
{code:java}
public class MyClass {
// fields and methods
private static final DogTag<MyClass> dogTag = DogTag.from(MyClass.class);
@Override
public boolean equals(Object that) {
return dogTag.doEqualsTest(this, that);
}
@Override
public int hashCode() {
return dogTag.doHashCode(this);
}
}{code}
I'm looking into changing the API, so it would work like this:
{code:java}
public class MyClass {
// fields and methods here ...
private final DogTag<MyClass> dogTag = DogTag.from(MyClass.class, this);
@Override
public boolean equals(Object other) {
return dogTag.doEqualsTest(other);
}
@Override
public int hashCode() {
return dogTag.doHashCode();
}
}
{code}
(I'm a new contributor, so I'm not sure if this is the best way to suggest a
whole new class. But I'd like to contribute to give people a faster way of
doing Reflection Equals.)
--
This message was sent by Atlassian Jira
(v8.3.4#803005)