Skip to content

Allow caching of hashCode on Freezed models #1293

@Matthiee

Description

@Matthiee

Is your feature request related to a problem? Please describe.
During the profiling of the app I work on, I noticed that hashCode call for larger, deeply nested Freezed models is rather expensive. Upon further inspection, the hashCode is recomputed every time it is accessed.

Describe the solution you'd like
Repeated calls to .hashCode should return the cached value instead of recomputing.

Additional context
A similar request was made in built_value google/built_value.dart#760

Currently Freezed generates

@override
int get hashCode => Object.hash(runtimeType,propertyA,const DeepCollectionEquality().hash(someCollection),...

With cached hashCode

int? _cachedHashCode;

@override
int get hashCode => _cachedHashCode ??= Object.hash(runtimeType,propertyA,const DeepCollectionEquality().hash(someCollection),...

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions