Skip to content

Commit f16c048

Browse files
committed
Merge remote-tracking branch 'origin/master' into refactor_field_type
2 parents ccdf937 + 146b186 commit f16c048

File tree

7 files changed

+50
-65
lines changed

7 files changed

+50
-65
lines changed

protobuf/lib/meta.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ const GeneratedMessage_reservedNames = <String>[
2626
'eventPlugin',
2727
'extensionsAreInitialized',
2828
'freeze',
29-
'fromBuffer',
30-
'fromJson',
3129
'getDefaultForField',
3230
'getExtension',
3331
'getField',

protobuf/lib/src/protobuf/extension_registry.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ExtensionRegistry {
4949
///
5050
/// Using this method to retrieve extensions is more expensive overall than
5151
/// using an [ExtensionRegistry] with all the needed extensions when doing
52-
/// [GeneratedMessage.fromBuffer].
52+
/// [GeneratedMessage.mergeFromBuffer].
5353
///
5454
/// Example:
5555
///

protobuf/lib/src/protobuf/field_set.dart

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -654,50 +654,28 @@ class _FieldSet {
654654
if (_hashCodesCanBeMemoized && _memoizedHashCode != null) {
655655
return _memoizedHashCode!;
656656
}
657-
// Hashes the value of one field (recursively).
658-
int hashField(int hash, FieldInfo fi, value) {
659-
if (value is List && value.isEmpty) {
660-
return hash; // It's either repeated or an empty byte array.
661-
}
662657

663-
hash = _HashUtils._combine(hash, fi.tagNumber);
664-
if (fi.type.baseType == FieldBaseType.bytes) {
665-
// Bytes are represented as a List<int> (Usually with byte-data).
666-
// We special case that to match our equality semantics.
667-
hash = _HashUtils._combine(hash, _HashUtils._hashObjects(value));
668-
} else if (fi.type.baseType != FieldBaseType.enum_) {
669-
hash = _HashUtils._combine(hash, value.hashCode);
670-
} else if (fi.isRepeated) {
671-
hash = _HashUtils._combine(
672-
hash, _HashUtils._hashObjects(value.map((enm) => enm.value)));
673-
} else {
674-
ProtobufEnum enm = value;
675-
hash = _HashUtils._combine(hash, enm.value);
676-
}
658+
// Hash with descriptor.
659+
var hash = _HashUtils._combine(0, _meta.hashCode);
677660

678-
return hash;
661+
// Hash with non-extension fields.
662+
final values = _values;
663+
for (final fi in _infosSortedByTag) {
664+
final value = values[fi.index!];
665+
if (value == null) continue;
666+
hash = _hashField(hash, fi, value);
679667
}
680668

681-
int hashEachField(int hash) {
682-
//non-extension fields
683-
hash = _infosSortedByTag.where((fi) => _values[fi.index!] != null).fold(
684-
hash, (int h, FieldInfo fi) => hashField(h, fi, _values[fi.index!]));
685-
686-
if (!_hasExtensions) return hash;
687-
688-
hash =
689-
_sorted(_extensions!._tagNumbers).fold(hash, (int h, int tagNumber) {
690-
var fi = _extensions!._getInfoOrNull(tagNumber)!;
691-
return hashField(h, fi, _extensions!._getFieldOrNull(fi));
692-
});
693-
694-
return hash;
669+
// Hash with extension fields.
670+
if (_hasExtensions) {
671+
final extensions = _extensions!;
672+
final sortedByTagNumbers = _sorted(extensions._tagNumbers);
673+
for (final tagNumber in sortedByTagNumbers) {
674+
final fi = extensions._getInfoOrNull(tagNumber)!;
675+
hash = _hashField(hash, fi, extensions._getFieldOrNull(fi));
676+
}
695677
}
696678

697-
// Hash with descriptor.
698-
var hash = _HashUtils._combine(0, _meta.hashCode);
699-
// Hash with fields.
700-
hash = hashEachField(hash);
701679
// Hash with unknown fields.
702680
if (_hasUnknownFields) {
703681
hash = _HashUtils._combine(hash, _unknownFields.hashCode);
@@ -709,6 +687,30 @@ class _FieldSet {
709687
return hash;
710688
}
711689

690+
// Hashes the value of one field (recursively).
691+
static int _hashField(int hash, FieldInfo fi, value) {
692+
if (value is List && value.isEmpty) {
693+
return hash; // It's either repeated or an empty byte array.
694+
}
695+
696+
hash = _HashUtils._combine(hash, fi.tagNumber);
697+
if (fi.type.baseType == FieldBaseType.bytes) {
698+
// Bytes are represented as a List<int> (Usually with byte-data).
699+
// We special case that to match our equality semantics.
700+
hash = _HashUtils._combine(hash, _HashUtils._hashObjects(value));
701+
} else if (!fi.type.isEnum) {
702+
hash = _HashUtils._combine(hash, value.hashCode);
703+
} else if (fi.type.isRepeated) {
704+
hash = _HashUtils._combine(
705+
hash, _HashUtils._hashObjects(value.map((enm) => enm.value)));
706+
} else {
707+
ProtobufEnum enm = value;
708+
hash = _HashUtils._combine(hash, enm.value);
709+
}
710+
711+
return hash;
712+
}
713+
712714
void writeString(StringBuffer out, String indent) {
713715
void renderValue(key, value) {
714716
if (value is GeneratedMessage) {

protobuf/lib/src/protobuf/generated_message.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,6 @@ abstract class GeneratedMessage {
2929
if (eventPlugin != null) eventPlugin!.attach(this);
3030
}
3131

32-
GeneratedMessage.fromBuffer(
33-
List<int> input, ExtensionRegistry extensionRegistry) {
34-
__fieldSet = _FieldSet(this, info_, eventPlugin);
35-
if (eventPlugin != null) eventPlugin!.attach(this);
36-
mergeFromBuffer(input, extensionRegistry);
37-
}
38-
39-
GeneratedMessage.fromJson(String input, ExtensionRegistry extensionRegistry) {
40-
__fieldSet = _FieldSet(this, info_, eventPlugin);
41-
if (eventPlugin != null) eventPlugin!.attach(this);
42-
mergeFromJson(input, extensionRegistry);
43-
}
44-
4532
// Overridden by subclasses.
4633
BuilderInfo get info_;
4734

protoc_plugin/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ your `PATH` with name `protoc-gen-dart`, or pass the path to it to `protoc`'s
6464
## How to use
6565

6666
Once you have `protoc-gen-dart` in the `PATH` the protocol buffer compiler can
67-
be invoked like this:
67+
be invoked like this to generate Dart for the proto file `test.proto`:
6868

6969
$ protoc --dart_out=. test.proto
7070

@@ -106,21 +106,25 @@ re-export the relevant protocol buffer classes.
106106
Say we have the file `m1.proto` with the following content
107107

108108
```proto
109+
syntax = "proto3";
110+
109111
message M1 {
110-
optional string a;
112+
string a = 1;
111113
}
112114
```
113115

114116
and `m2.proto` containing
115117

116118
```proto
119+
syntax = "proto3";
120+
117121
message M2 {
118-
optional string b;
122+
string b = 1;
119123
}
120124
```
121125

122126
Compiling these to Dart will produce two libraries in `m1.pb.dart` and
123-
`m2.pb.dart`. The following code shows a library M which combines
127+
`m2.pb.dart`. The following code shows a library `M` which combines
124128
these two protocol buffer libraries, exposes the classes `M1` and `M2` and
125129
adds some additional methods.
126130

protoc_plugin/test/generated_message_test.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,6 @@ void main() {
468468
message.noSuchMethod_2 = 1;
469469
message.runtimeType_3 = 1;
470470
message.toString_4 = 1;
471-
message.fromBuffer_10 = 1;
472-
message.fromJson_11 = 1;
473471
message.hasRequiredFields_12 = 1;
474472
message.isInitialized_13 = 1;
475473
message.clear_14 = 1;
@@ -507,8 +505,6 @@ void main() {
507505
message.noSuchMethod_2.clear();
508506
message.runtimeType_3.clear();
509507
message.toString_4.clear();
510-
message.fromBuffer_10.clear();
511-
message.fromJson_11.clear();
512508
message.hasRequiredFields_12.clear();
513509
message.isInitialized_13.clear();
514510
message.clear_14.clear();
@@ -550,8 +546,6 @@ void main() {
550546
message.noSuchMethod_2 = 1;
551547
message.runtimeType_3 = 1;
552548
message.toString_4 = 1;
553-
message.fromBuffer_10 = 1;
554-
message.fromJson_11 = 1;
555549
message.hasRequiredFields_12 = 1;
556550
message.isInitialized_13 = 1;
557551
message.clear_14 = 1;

query_benchmark/bin/set_nested_value.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ main() {
2020
title: 'protobuf_decode',
2121
duration: measure(() => a.rebuild((f0.A0 a0Builder) {
2222
a0Builder.a4.last = a0Builder.a4.last.rebuild((f2.A1 a1builder) {
23-
a1builder.a378.rebuild(
23+
a1builder.a378 = a1builder.a378.rebuild(
2424
(f19.A220 a220builder) => a220builder.a234 = 'new_value');
2525
});
2626
})),

0 commit comments

Comments
 (0)