Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make wrapped lists in PbList monomorphic #902

Merged
merged 6 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions protobuf/lib/src/protobuf/pb_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ typedef CheckFunc<E> = void Function(E? x);

/// A [ListBase] implementation used for protobuf `repeated` fields.
class PbList<E> extends ListBase<E> {
/// The actual list storing the elements.
///
/// We want only one [List] implementation class to be stored here to make
/// sure the list operations are monomorphic and can be inlined. To make this
/// explicit we use the `<E>[]` syntax when initializing this field (instead
/// of factory methods like `List.from`).
final List<E> _wrappedList;

final CheckFunc<E> _check;

bool _isReadOnly = false;
Expand All @@ -23,12 +30,14 @@ class PbList<E> extends ListBase<E> {
_check = check;

PbList.unmodifiable()
: _wrappedList = const [],
: _wrappedList = <E>[],
osa1 marked this conversation as resolved.
Show resolved Hide resolved
_check = _checkNotNull,
_isReadOnly = true;

PbList.from(List from)
: _wrappedList = List<E>.from(from),
PbList.from(List<E> from)
// Use `<E>[]` to allocate the list, see [_wrappedList] documentation.
// ignore: prefer_spread_collections
osa1 marked this conversation as resolved.
Show resolved Hide resolved
: _wrappedList = <E>[]..addAll(from),
_check = _checkNotNull;

@override
Expand Down
10 changes: 8 additions & 2 deletions protobuf/lib/src/protobuf/pb_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ class PbMap<K, V> extends MapBase<K, V> {
static const int _keyFieldNumber = 1;
static const int _valueFieldNumber = 2;

/// The actual map storing the elements.
///
/// We want only one [Map] implementation class to be stored here to make
/// sure the map operations are monomorphic and can be inlined. To make this
/// explicit we use the `<K, V>{}` syntax when initializing this field
/// (instead of factory methods like `Map.unmodifiable`).
final Map<K, V> _wrappedMap;

bool _isReadonly = false;

PbMap(this.keyFieldType, this.valueFieldType) : _wrappedMap = <K, V>{};

PbMap.unmodifiable(PbMap other)
PbMap.unmodifiable(PbMap<K, V> other)
: keyFieldType = other.keyFieldType,
valueFieldType = other.valueFieldType,
_wrappedMap = Map.unmodifiable(other._wrappedMap),
_wrappedMap = <K, V>{}..addAll(other._wrappedMap),
_isReadonly = true;

@override
Expand Down