I'm creating a few rather big objects and many of them are duplicates. So I thought about using Guava's Interner
for them and always work with the interned object only (i.e., each object gets interned just after creation).
It occurred to me that equals
of those objects is rather slow (and gets used a lot) and that I actually never need it, as a.equals(b)
is equivalent to a == b
after interning. Unfortunately, the Interner
itself uses equals
so I have to override it for the single use.
I wonder if there's a simple way to have my equals and eat it?
Disclaimer: I know about the root of all evil and I'm not sure if optimization on this place is worth the effort. However, I'm interested if the above problem has a nice solution.
It depends on your use pattern as to where you want to take the performance hit.
If you're going to be doing a lot of comparisons after creating an object and you know you have a high occurrence of duplicates, then the flyweight pattern may make sense as all your post-creation comparisons can be done via reference equality.
The other thing to consider is space efficiency; you mention they're large objects. What's more important? If you're saving a ton of memory by pooling objects, that may be a win.
Profiling your code trying both approaches can also help you make that decision.
Edit to add: That said I'm surprised their interner is relying solely on
equals()
. In my head it would make much more sense to rely onhashcode()
and only useequals()
for collisions.