Skip to content

Commit 5f914e3

Browse files
committed
implement cached hashcode for Freezed models
1 parent c038bc5 commit 5f914e3

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

packages/freezed/lib/src/templates/abstract_template.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mixin _\$${data.name.public}${data.genericsDefinitionTemplate}$interfaces {
4646
4747
$abstractProperties
4848
${copyWith?.copyWithGetter(needsCast: true) ?? ''}
49-
${methods(data, globalData, properties: commonProperties, name: data.name, escapedName: data.escapedName, source: Source.mixin)}
49+
${methods(data, globalData, properties: commonProperties, name: data.name, escapedName: data.escapedName, source: Source.mixin, isConst: false)}
5050
}
5151
5252
${copyWith?.commonInterface ?? ''}

packages/freezed/lib/src/templates/concrete_template.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ${constructor.redirectedName}${data.genericsDefinitionTemplate} $_concrete
5050
$_properties
5151
5252
${copyWith?.copyWithGetter(needsCast: false) ?? ''}
53-
${methods(data, globalData, properties: constructor.properties, name: constructor.redirectedName, escapedName: constructor.escapedName, source: Source.syntheticClass)}
53+
${methods(data, globalData, properties: constructor.properties, name: constructor.redirectedName, escapedName: constructor.escapedName, source: Source.syntheticClass, isConst: constructor.isConst)}
5454
}
5555
5656
${copyWith?.interface ?? ''}
@@ -325,12 +325,13 @@ String methods(
325325
required String name,
326326
required String escapedName,
327327
required Source source,
328+
required bool isConst,
328329
}) {
329330
return '''
330331
${toJson(data, name: name, source: source)}
331332
${debugFillProperties(data, globalData, properties, escapedClassName: escapedName)}
332333
${operatorEqualMethod(data, properties, className: name, source: source)}
333-
${hashCodeMethod(data, properties, source: source)}
334+
${hashCodeMethod(data, properties, source: source, isConst: isConst)}
334335
${toStringMethod(data, globalData, escapedClassName: escapedName, properties: properties)}
335336
''';
336337
}
@@ -467,6 +468,7 @@ String hashCodeMethod(
467468
Class data,
468469
List<Property> properties, {
469470
required Source source,
471+
required bool isConst,
470472
}) {
471473
if (!data.options.equal) return '';
472474

@@ -491,25 +493,26 @@ String hashCodeMethod(
491493
property.name,
492494
];
493495

494-
if (hashedProperties.length == 1) {
495-
return '''
496-
$jsonKey
497-
@override
498-
int get hashCode => ${hashedProperties.first}.hashCode;
499-
''';
500-
}
501-
if (hashedProperties.length >= 20) {
496+
final hashCodeExpression = hashedProperties.length == 1
497+
? '${hashedProperties.first}.hashCode'
498+
: hashedProperties.length >= 20
499+
? 'Object.hashAll([${hashedProperties.join(',')}])'
500+
: 'Object.hash(${hashedProperties.join(',')})';
501+
502+
if (isConst || source == Source.mixin) {
502503
return '''
503504
$jsonKey
504505
@override
505-
int get hashCode => Object.hashAll([${hashedProperties.join(',')}]);
506+
int get hashCode => $hashCodeExpression;
506507
''';
507508
}
508509

509510
return '''
510511
$jsonKey
512+
int? _cachedHashCode;
513+
511514
@override
512-
int get hashCode => Object.hash(${hashedProperties.join(',')});
515+
int get hashCode => _cachedHashCode ??= $hashCodeExpression;
513516
''';
514517
}
515518

0 commit comments

Comments
 (0)