On Fri, 2009-09-25 at 16:41 +0300, Daniel Augustin Pavel wrote:
> I've worked a bit on improving the perfomance of equals() and
> hashCode() for the generated message clases.
> My implementation of equals() compares the two messages
> field-by-field. In tests, for equal objects it is about an order of
> magnitude faster than the default.
> The hashCode() value is cached (the objects are supposed to be
> immutable anyway), speeding up equals() in the not-equal case by
> another order of magnitude.
I think it would be safer to do this:
+ "if (otherObject == this) return true;\n"
+ "if (!(otherObject instanceof $classname$)) return false;\n"
+ "if (hashCode() != otherObject.hashCode()) return false;\n"
+ "$classname$ other = ($classname$) otherObject;\n",
+ "classname", descriptor_->name());
(I reversed the hashCode test and the instanceof test)
This way if I happen to pbObject.equals(null) it won't throw a
NullPointerException, but just say they're not equals (which they are,
> For LITE messages, which don't use the AbstractMessage
> implementations, the hashCode is computed by going through all the
> The only downside is that the size of the generated classes increases
> (additional bytecode), proportional to the number of fields in the
> Would love to hear feedback on the patch -- anyone finding it useful /
This is a good addition. I really like it.
It'd be really great if it would be merged in the next revision.
My Blog: http://www.masterzen.fr/
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To post to this group, send email to firstname.lastname@example.org
To unsubscribe from this group, send email to
For more options, visit this group at