Thanks, Jason. Any timeline for when these internal patches will make it to
protobuf 2.3.1?



From: Jason Hsueh 
Sent: Friday, September 10, 2010 12:31 PM
Subject: Re: [protobuf] Could Java initializers be elided for primitive
fields in many cases?

Indeed, this has actually been fixed internally but the changes have not
been released yet.

(Although, the code you referenced is the code to generate the 
clearFoo() method, which still must set things to the default)

On Wed, Sep 8, 2010 at 1:05 PM, David Dabbs wrote:

IIRC Java initializes these primitive types as

  private boolean foo_boolean;    // false
  private int     foo_int;        // 0
  private long    foo_long;       // 0L
  private float   foo_float;      // 0.0f
  private double  foo_double;     // 0.0d

But unconditionally emits initialization code even
when the proto -derived default value from is the same as the organic

Was there a performance reason for including the (seemingly) unnecessary
init code?
If not, would there be any harm is NOT explicitly initializing these
primitive field(s)?
Seems like not doing so would reduce code size.

// from

 JavaType type = GetJavaType(descriptor_);
 if (type == JAVATYPE_STRING || type == JAVATYPE_BYTES) {
   // The default value is not a simple literal so we want to avoid
   // it multiple times.  Instead, get the default out of the default
     "  result.$name$_ = getDefaultInstance().get$capitalized_name$();\n");
 } else {
     "  result.$name$_ = $default$;\n");

I didn't go as far as creating a patch in case there were specific reasons
for the init code.



You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

Reply via email to