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.
> [snipped]
> 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:
+  printer->Print(
+    "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
> fields.
> The only downside is that the size of the generated classes increases
> (additional bytecode), proportional to the number of fields in the
> message.
> Would love to hear feedback on the patch -- anyone finding it useful /
> appropiate?

This is a good addition. I really like it.
It'd be really great if it would be merged in the next revision.
Brice Figureau
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 protobuf@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to