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

Optimize field set for oneof case #385

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions protobuf/lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/// - <int, dynamic>{} 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;
Expand Down Expand Up @@ -98,7 +100,7 @@ class _FieldSet {

_FieldSet(this._message, BuilderInfo meta, this._eventPlugin)
: _meta = meta,
_values = _makeValueList(meta.byIndex.length),
_values = meta.oneofs.isNotEmpty ? <int, dynamic>{} : _makeValueList(meta.byIndex.length),
_oneofCases = meta.oneofs.isEmpty ? null : <int, int>{};

static List _makeValueList(int length) {
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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) {
Expand Down