Skip to content

Commit 99e99e2

Browse files
committed
Add benchmark for hashing. Improve hashing performance.
1 parent b9d9bbe commit 99e99e2

17 files changed

+169
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Generate empty constructor with semicolon instead of {}.
77
- Use ArgumentError.notNull for null errors.
88
- Reject dynamic fields.
9+
- Add simple benchmark for hashing. Improve hashing performance.
910

1011
## 0.1.5
1112

built_value/lib/built_value.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
library built_value;
66

7-
export 'package:quiver/core.dart' show hashObjects;
8-
97
/// Implement this for a Built Value.
108
///
119
/// Then use built_value_generator.dart code generation functionality to
@@ -59,3 +57,19 @@ abstract class Builder<V extends Built<V, B>, B extends Builder<V, B>> {
5957
//
6058
// Fields marked with this annotation are allowed to be null.
6159
const String nullable = 'nullable';
60+
61+
/// For use by generated code in calculating hash codes. Do not use directly.
62+
int $jc(int hash, int value) {
63+
// Jenkins hash "combine".
64+
hash = 0x1fffffff & (hash + value);
65+
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
66+
return hash ^ (hash >> 6);
67+
}
68+
69+
/// For use by generated code in calculating hash codes. Do not use directly.
70+
int $jf(int hash) {
71+
// Jenkins hash "finish".
72+
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
73+
hash = hash ^ (hash >> 11);
74+
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
75+
}

built_value/pubspec.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,3 @@ homepage: https://github.com/google/built_value.dart
88

99
environment:
1010
sdk: '>=1.8.0 <2.0.0'
11-
12-
dependencies:
13-
quiver: '>=0.21.0 <0.24.0'

built_value_generator/lib/src/source_class.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,11 @@ abstract class SourceClass implements Built<SourceClass, SourceClassBuilder> {
259259
if (fields.length == 0) {
260260
result.writeln('return ${name.hashCode};');
261261
} else {
262-
result.writeln('return hashObjects([');
263-
result.write(fields.map((field) => field.name).join(', '));
264-
result.writeln(']);');
262+
result.writeln(r'return $jf(');
263+
result.writeln(r'$jc(' * fields.length);
264+
result.writeln('0, ');
265+
result.write(fields.map((field) => '${field.name}.hashCode').join('), '));
266+
result.writeln('));');
265267
}
266268
result.writeln('}');
267269
result.writeln();

built_value_generator/lib/src/source_class.g.dart

Lines changed: 24 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

built_value_generator/lib/src/source_field.g.dart

Lines changed: 12 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/collections.g.dart

Lines changed: 21 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/compound_value.g.dart

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/simple_value.g.dart

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/validated_value.g.dart

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)