Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
Protobuf changes for smaller dart2js code, Int64 fixes
Browse files Browse the repository at this point in the history
This change is paired with https://chromiumcodereview.appspot.com/814213003

Reduces code size for one app by 0.9%

1. Allow constants for the default value to avoid many trivial closures.
2. Generate and use static M.create() and M.createRepeated() methods on message classes M to ensure there is a shared copy of these closures rather than one copy per use.
3. Parse Int64 values rather than generate from 'int' to ensure no truncation errors in JavaScript.

R=sigmund@google.com

Review URL: https://chromiumcodereview.appspot.com//813373003
  • Loading branch information
rakudrama committed Dec 19, 2014
1 parent e94fb50 commit fec382b
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 79 deletions.
12 changes: 10 additions & 2 deletions lib/message_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ class MessageGenerator extends ProtobufContainer {
makeDefault = field.initializationForPackage(package);
}
String subBuilder = null;
String subBuilderRepeated = null;
if (field.message || field.group) {
subBuilder = '()${SP}=>${SP}new ${fieldType}()';
subBuilder = '${fieldType}.create';
subBuilderRepeated = '${fieldType}.createRepeated';
}
String valueOf = null;
if (field.enm) {
Expand All @@ -116,7 +118,7 @@ class MessageGenerator extends ProtobufContainer {
// Repeated message: default is an empty list
out.println('..m(${field.number},${SP}'
'\'${field.externalFieldName}\',${SP}$subBuilder,'
'${SP}()${SP}=>${SP}new PbList<${fieldType}>())');
'${SP}$subBuilderRepeated)');
} else if (type[0] == 'P' && type != 'PG' && type != 'PE') {
// Repeated, not a message or enum: default is an empty list,
// subBuilder is null, valueOf is null.
Expand Down Expand Up @@ -172,6 +174,12 @@ class MessageGenerator extends ProtobufContainer {

out.println('BuilderInfo get info_${SP}=>${SP}_i;');

// Factory functions which can be used as default value closures.
out.println('static ${classname}${SP}create()${SP}=>'
'${SP}new ${classname}();');
out.println('static PbList<${classname}>${SP}createRepeated()${SP}=>'
'${SP}new PbList<${classname}>();');

generateFieldsAccessorsMutators(out);
});
out.println();
Expand Down
43 changes: 22 additions & 21 deletions lib/protobuf_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class ProtobufField {
codedStreamType = 'Bool';
if (!repeats) {
if (field.hasDefaultValue() && 'false' != field.defaultValue) {
initialization = '()${SP}=>${SP}${field.defaultValue}';
initialization = '${field.defaultValue}';
}
}
break;
Expand All @@ -155,19 +155,18 @@ class ProtobufField {
if (field.hasDefaultValue() &&
('0.0' != field.defaultValue || '0' != field.defaultValue)) {
if (field.defaultValue == 'inf') {
initialization = '()${SP}=>${SP}double.INFINITY';
initialization = 'double.INFINITY';
} else if (field.defaultValue == '-inf') {
initialization = '()${SP}=>${SP}double.NEGATIVE_INFINITY';
initialization = 'double.NEGATIVE_INFINITY';
} else if (field.defaultValue == 'nan') {
initialization = '()${SP}=>${SP}double.NAN';
initialization = 'double.NAN';
} else if (HEX_LITERAL_REGEX.hasMatch(field.defaultValue)) {
initialization = '()${SP}=>${SP}(${field.defaultValue})'
'.toDouble()';
initialization = '(${field.defaultValue}).toDouble()';
} else if (INTEGER_LITERAL_REGEX.hasMatch(field.defaultValue)) {
initialization = '()${SP}=>${SP}${field.defaultValue}.0';
initialization = '${field.defaultValue}.0';
} else if (DECIMAL_LITERAL_REGEX_A.hasMatch(field.defaultValue)
|| DECIMAL_LITERAL_REGEX_B.hasMatch(field.defaultValue)) {
initialization = '()${SP}=>${SP}${field.defaultValue}';
initialization = '${field.defaultValue}';
} else {
throw new InvalidDefaultValue.double(
field.name, field.defaultValue);
Expand Down Expand Up @@ -202,7 +201,7 @@ class ProtobufField {
}
if (!repeats) {
if (field.hasDefaultValue() && '0' != field.defaultValue) {
initialization = '()${SP}=>${SP}${field.defaultValue}';
initialization = '${field.defaultValue}';
}
}
break;
Expand Down Expand Up @@ -234,7 +233,11 @@ class ProtobufField {
if (!repeats) {
final defaultValue = field.hasDefaultValue() ?
field.defaultValue : '0';
initialization = '()${SP}=>${SP}makeLongInt($defaultValue)';
if (defaultValue == '0') {
initialization = 'Int64.ZERO';
} else {
initialization = "parseLongInt('$defaultValue')";
}
}
break;
case FieldDescriptorProto_Type.TYPE_STRING:
Expand All @@ -244,7 +247,7 @@ class ProtobufField {
if (!repeats) {
if (field.hasDefaultValue() && !field.defaultValue.isEmpty) {
String defaultValue = field.defaultValue.replaceAll(r'$', r'\$');
initialization = '()${SP}=>${SP}\'$defaultValue\'';
initialization = '\'$defaultValue\'';
}
}
break;
Expand Down Expand Up @@ -277,8 +280,8 @@ class ProtobufField {
} else {
throw 'FAILURE: Unknown group type reference ${field.typeName}';
}
initialization = '()${SP}=>${SP}new ${baseType}()';
prefixedInitialization = '()${SP}=>${SP}new ${prefixedBaseType}()';
initialization = '${baseType}.create';
prefixedInitialization = '${prefixedBaseType}.create';
break;
case FieldDescriptorProto_Type.TYPE_MESSAGE:
ProtobufContainer messageType = context[field.typeName];
Expand All @@ -296,8 +299,8 @@ class ProtobufField {
} else {
throw 'FAILURE: Unknown message type reference ${field.typeName}';
}
initialization = '()${SP}=>${SP}new ${baseType}()';
prefixedInitialization = '()${SP}=>${SP}new ${prefixedBaseType}()';
initialization = '${baseType}.create';
prefixedInitialization = '${prefixedBaseType}.create';
break;
case FieldDescriptorProto_Type.TYPE_ENUM:
EnumGenerator enumType = context[field.typeName];
Expand All @@ -316,16 +319,14 @@ class ProtobufField {
if (!repeats) {
if (field.hasDefaultValue() && !field.defaultValue.isEmpty) {
initialization =
'()${SP}=>${SP}${baseType}.${field.defaultValue}';
'${baseType}.${field.defaultValue}';
prefixedInitialization =
'()${SP}=>${SP}${prefixedBaseType}.${field.defaultValue}';
'${prefixedBaseType}.${field.defaultValue}';
} else if (!enumType._canonicalValues.isEmpty) {
initialization =
'()${SP}=>${SP}${baseType}.'
'${enumType._canonicalValues[0].name}';
'${baseType}.${enumType._canonicalValues[0].name}';
prefixedInitialization =
'()${SP}=>${SP}${prefixedBaseType}.'
'${enumType._canonicalValues[0].name}';
'${prefixedBaseType}.${enumType._canonicalValues[0].name}';
}
}
} else {
Expand Down
Loading

0 comments on commit fec382b

Please sign in to comment.