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

Remove fold() closures from FieldSet._hashCode. #554

Merged
merged 1 commit into from
Apr 23, 2022
Merged
Changes from all 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
33 changes: 17 additions & 16 deletions protobuf/lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -677,26 +677,27 @@ class _FieldSet {
return hash;
}

int hashEachField(int hash) {
//non-extension fields
hash = _infosSortedByTag.where((fi) => _values[fi.index!] != null).fold(
hash, (int h, FieldInfo fi) => hashField(h, fi, _values[fi.index!]));

if (!_hasExtensions) return hash;
// Hash with descriptor.
var hash = _HashUtils._combine(0, _meta.hashCode);

hash =
_sorted(_extensions!._tagNumbers).fold(hash, (int h, int tagNumber) {
var fi = _extensions!._getInfoOrNull(tagNumber)!;
return hashField(h, fi, _extensions!._getFieldOrNull(fi));
});
// Hash with non-extension fields.
final values = _values;
for (final fi in _infosSortedByTag) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_hashObjects, called at line 671, also uses fold

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a big difference between these two patterns. The function passed to fold() in _hashObjects doesn't capture any local variables. I tested modifying that one as well and it was a regression in both JS and VM cases.

final value = values[fi.index!];
if (value == null) continue;
hash = hashField(hash, fi, value);
}

return hash;
// Hash with extension fields.
if (_hasExtensions) {
final extensions = _extensions!;
final sortedByTagNumbers = _sorted(extensions._tagNumbers);
for (final tagNumber in sortedByTagNumbers) {
final fi = extensions._getInfoOrNull(tagNumber)!;
hash = hashField(hash, fi, extensions._getFieldOrNull(fi));
}
}

// Hash with descriptor.
var hash = _HashUtils._combine(0, _meta.hashCode);
// Hash with fields.
hash = hashEachField(hash);
// Hash with unknown fields.
if (_hasUnknownFields) {
hash = _HashUtils._combine(hash, _unknownFields.hashCode);
Expand Down