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 a closure in _FieldSet.hashCode #633

Merged
merged 3 commits into from
Apr 26, 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
51 changes: 26 additions & 25 deletions protobuf/lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
@@ -654,29 +654,6 @@ class _FieldSet {
if (_hashCodesCanBeMemoized && _memoizedHashCode != null) {
return _memoizedHashCode!;
}
// Hashes the value of one field (recursively).
int hashField(int hash, FieldInfo fi, value) {
if (value is List && value.isEmpty) {
return hash; // It's either repeated or an empty byte array.
}

hash = _HashUtils._combine(hash, fi.tagNumber);
if (_isBytes(fi.type)) {
// Bytes are represented as a List<int> (Usually with byte-data).
// We special case that to match our equality semantics.
hash = _HashUtils._combine(hash, _HashUtils._hashObjects(value));
} else if (!_isEnum(fi.type)) {
hash = _HashUtils._combine(hash, value.hashCode);
} else if (fi.isRepeated) {
hash = _HashUtils._combine(
hash, _HashUtils._hashObjects(value.map((enm) => enm.value)));
} else {
ProtobufEnum enm = value;
hash = _HashUtils._combine(hash, enm.value);
}

return hash;
}

// Hash with descriptor.
var hash = _HashUtils._combine(0, _meta.hashCode);
@@ -686,7 +663,7 @@ class _FieldSet {
for (final fi in _infosSortedByTag) {
final value = values[fi.index!];
if (value == null) continue;
hash = hashField(hash, fi, value);
hash = _hashField(hash, fi, value);
}

// Hash with extension fields.
@@ -695,7 +672,7 @@ class _FieldSet {
final sortedByTagNumbers = _sorted(extensions._tagNumbers);
for (final tagNumber in sortedByTagNumbers) {
final fi = extensions._getInfoOrNull(tagNumber)!;
hash = hashField(hash, fi, extensions._getFieldOrNull(fi));
hash = _hashField(hash, fi, extensions._getFieldOrNull(fi));
}
}

@@ -710,6 +687,30 @@ class _FieldSet {
return hash;
}

// Hashes the value of one field (recursively).
static int _hashField(int hash, FieldInfo fi, value) {
if (value is List && value.isEmpty) {
return hash; // It's either repeated or an empty byte array.
}

hash = _HashUtils._combine(hash, fi.tagNumber);
if (_isBytes(fi.type)) {
// Bytes are represented as a List<int> (Usually with byte-data).
// We special case that to match our equality semantics.
hash = _HashUtils._combine(hash, _HashUtils._hashObjects(value));
} else if (!_isEnum(fi.type)) {
hash = _HashUtils._combine(hash, value.hashCode);
} else if (fi.isRepeated) {
hash = _HashUtils._combine(
hash, _HashUtils._hashObjects(value.map((enm) => enm.value)));
} else {
ProtobufEnum enm = value;
hash = _HashUtils._combine(hash, enm.value);
}

return hash;
}

void writeString(StringBuffer out, String indent) {
void renderValue(key, value) {
if (value is GeneratedMessage) {