From e7b768b138ad8f0ad3c7354fb5a8fc280f80c478 Mon Sep 17 00:00:00 2001 From: Yaniv Shaked Date: Mon, 13 Jul 2020 12:44:00 +0300 Subject: [PATCH 1/3] Optimize field set for oneof case: use Map instead of List to save memory --- protobuf/lib/src/protobuf/field_set.dart | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/protobuf/lib/src/protobuf/field_set.dart b/protobuf/lib/src/protobuf/field_set.dart index 11df0c3db..ad845b541 100644 --- a/protobuf/lib/src/protobuf/field_set.dart +++ b/protobuf/lib/src/protobuf/field_set.dart @@ -52,10 +52,12 @@ class _FieldSet { final BuilderInfo _meta; final EventPlugin _eventPlugin; - /// The value of each non-extension field in a fixed-length array. + /// The value of each non-extension field in a dynamic type: + /// - Map for the case for one-of, since no need to allocate such long list + /// - List (fixed-length array) for non one-of cases /// The index of a field can be found in [FieldInfo.index]. /// A null entry indicates that the field has no value. - final List _values; + final dynamic _values; /// Contains all the extension fields, or null if there aren't any. _ExtensionFieldSet _extensions; @@ -98,7 +100,7 @@ class _FieldSet { _FieldSet(this._message, BuilderInfo meta, this._eventPlugin) : _meta = meta, - _values = _makeValueList(meta.byIndex.length), + _values = meta.oneofs.isNotEmpty ? Map() : _makeValueList(meta.byIndex.length), _oneofCases = meta.oneofs.isEmpty ? null : {}; static List _makeValueList(int length) { @@ -576,7 +578,14 @@ class _FieldSet { } } } - if (_values.isNotEmpty) _values.fillRange(0, _values.length, null); + if (_values.isNotEmpty) { + if (_values is List) { + _values.fillRange(0, _values.length, null); + } else { + _values.values.fillRange(0, _values.length, null); + } + } + if (_hasExtensions) _extensions._clearValues(); } @@ -893,7 +902,12 @@ class _FieldSet { /// /// Map fields and repeated fields are copied. void _shallowCopyValues(_FieldSet original) { - _values.setRange(0, original._values.length, original._values); + if (_values is List) { + _values.setRange(0, original._values.length, original._values); + } else { + _values.clear(); + _values.addAll(original._values); + } for (var index = 0; index < _meta.byIndex.length; index++) { var fieldInfo = _meta.byIndex[index]; if (fieldInfo.isMapField) { From 57228c210e8a4f36f26f27153ed094bbb319e276 Mon Sep 17 00:00:00 2001 From: Yaniv Shaked Date: Mon, 13 Jul 2020 14:46:54 +0300 Subject: [PATCH 2/3] Prefer Collection Literals --- protobuf/lib/src/protobuf/field_set.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protobuf/lib/src/protobuf/field_set.dart b/protobuf/lib/src/protobuf/field_set.dart index ad845b541..bf6e03f5e 100644 --- a/protobuf/lib/src/protobuf/field_set.dart +++ b/protobuf/lib/src/protobuf/field_set.dart @@ -53,7 +53,7 @@ class _FieldSet { final EventPlugin _eventPlugin; /// The value of each non-extension field in a dynamic type: - /// - Map for the case for one-of, since no need to allocate such long list + /// - {} for the case for one-of, since no need to allocate such long list /// - List (fixed-length array) for non one-of cases /// The index of a field can be found in [FieldInfo.index]. /// A null entry indicates that the field has no value. @@ -100,7 +100,7 @@ class _FieldSet { _FieldSet(this._message, BuilderInfo meta, this._eventPlugin) : _meta = meta, - _values = meta.oneofs.isNotEmpty ? Map() : _makeValueList(meta.byIndex.length), + _values = meta.oneofs.isNotEmpty ? {} : _makeValueList(meta.byIndex.length), _oneofCases = meta.oneofs.isEmpty ? null : {}; static List _makeValueList(int length) { From 64362b5d696eed3e5a647e134d27b6ca2cd638a3 Mon Sep 17 00:00:00 2001 From: Yaniv Shaked Date: Mon, 13 Jul 2020 15:01:21 +0300 Subject: [PATCH 3/3] Run dartfmt --- protobuf/lib/src/protobuf/field_set.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/protobuf/lib/src/protobuf/field_set.dart b/protobuf/lib/src/protobuf/field_set.dart index bf6e03f5e..d78484883 100644 --- a/protobuf/lib/src/protobuf/field_set.dart +++ b/protobuf/lib/src/protobuf/field_set.dart @@ -100,7 +100,9 @@ class _FieldSet { _FieldSet(this._message, BuilderInfo meta, this._eventPlugin) : _meta = meta, - _values = meta.oneofs.isNotEmpty ? {} : _makeValueList(meta.byIndex.length), + _values = meta.oneofs.isNotEmpty + ? {} + : _makeValueList(meta.byIndex.length), _oneofCases = meta.oneofs.isEmpty ? null : {}; static List _makeValueList(int length) {