Skip to content

Commit

Permalink
Update getList and getMap return types (#903)
Browse files Browse the repository at this point in the history
This improves iteration performance by making `moveNext` and `current` direct
calls in kernel. #902 does not have the same effect on `moveNext` and `current`
calls, this change is needed even with #902.

This change was not possible before #880 as users could override the list and
map types to types that are not `PbList`s or `PbMap`s.

Note: changes in the message field types (the plugin changes in this PR) are
not necessary for the kernel improvements, but it doesn't hurt to generate more
precise types everywhere.
  • Loading branch information
osa1 authored Nov 23, 2023
1 parent 4e0bdff commit cf43230
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
6 changes: 3 additions & 3 deletions protobuf/lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class _FieldSet {
}

/// The implementation of a generated getter for repeated fields.
List<T> _$getList<T>(int index) {
PbList<T> _$getList<T>(int index) {
final value = _values[index];
if (value != null) return value;

Expand All @@ -387,9 +387,9 @@ class _FieldSet {
}

/// The implementation of a generated getter for map fields.
Map<K, V> _$getMap<K, V>(GeneratedMessage parentMessage, int index) {
PbMap<K, V> _$getMap<K, V>(GeneratedMessage parentMessage, int index) {
final value = _values[index];
if (value != null) return value as Map<K, V>;
if (value != null) return value;

final fi = _nonExtensionInfoByIndex(index) as MapFieldInfo<K, V>;
assert(fi.isMapField);
Expand Down
5 changes: 3 additions & 2 deletions protobuf/lib/src/protobuf/generated_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,12 @@ abstract class GeneratedMessage {

/// For generated code only.
/// @nodoc
List<T> $_getList<T>(int index) => _fieldSet._$getList<T>(index);
PbList<T> $_getList<T>(int index) => _fieldSet._$getList<T>(index);

/// For generated code only.
/// @nodoc
Map<K, V> $_getMap<K, V>(int index) => _fieldSet._$getMap<K, V>(this, index);
PbMap<K, V> $_getMap<K, V>(int index) =>
_fieldSet._$getMap<K, V>(this, index);

/// For generated code only.
/// @nodoc
Expand Down
5 changes: 5 additions & 0 deletions protoc_plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
## 22.0.0-dev

* Remove `PbEventMixin` mixin. ([#738])
* Type of repeated fields is now `PbList` (instead of `List`), type of map
fields is now `PbMap` (instead of `Map`). ([#903])

This change requires protobuf-4.0.0.

[#738]: https://github.com/google/protobuf.dart/issues/738
[#903]: https://github.com/google/protobuf.dart/pull/903

## 21.1.2

Expand Down
2 changes: 1 addition & 1 deletion protoc_plugin/lib/src/base_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BaseType {
: prefixed;

String getRepeatedDartType(FileGenerator fileGen) =>
'$coreImportPrefix.List<${getDartType(fileGen)}>';
'$protobufImportPrefix.PbList<${getDartType(fileGen)}>';

String getRepeatedDartTypeIterable(FileGenerator fileGen) =>
'$coreImportPrefix.Iterable<${getDartType(fileGen)}>';
Expand Down
2 changes: 1 addition & 1 deletion protoc_plugin/lib/src/protobuf_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class ProtobufField {
final d = baseType.generator as MessageGenerator;
final keyType = d._fieldList[0].baseType.getDartType(parent.fileGen!);
final valueType = d._fieldList[1].baseType.getDartType(parent.fileGen!);
return '$coreImportPrefix.Map<$keyType, $valueType>';
return '$protobufImportPrefix.PbMap<$keyType, $valueType>';
}
if (isRepeated) return baseType.getRepeatedDartType(parent.fileGen!);
return baseType.getDartType(parent.fileGen!);
Expand Down

0 comments on commit cf43230

Please sign in to comment.