-
-
Notifications
You must be signed in to change notification settings - Fork 286
Cache hashCode computation in Freezed models to avoid expensive recomputation #1312
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
base: master
Are you sure you want to change the base?
Conversation
WalkthroughThe changes implement hashCode caching for Freezed models through a new Changes
Sequence DiagramsequenceDiagram
participant Code as Generated Code
participant Getter as hashCode Getter
participant Cache as _cachedHashCode Field
Note over Code: isConst = false
Code->>Getter: Access hashCode
Getter->>Cache: Check if cached
alt Cache exists
Cache-->>Getter: Return cached value
else Cache is null
Getter->>Getter: Compute Object.hash(...)
Getter->>Cache: Store result (_cachedHashCode ??=)
Cache-->>Getter: Return computed value
end
Getter-->>Code: Return hashCode
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes The changes introduce new logic density in hashCode generation with conditional caching behavior, require understanding parameter threading across multiple templates, and involve subtle semantic shifts in collection equality. The alterations are focused and consistent, but each file demands separate reasoning for correctness. Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (3)
🧰 Additional context used🪛 GitHub Actions: Buildpackages/freezed_annotation/lib/freezed_annotation.dart[error] 1-1: Command failed: dart format --set-exit-if-changed . — Dart format detected formatting changes (3 files formatted, 1 changed). Please run 'dart format --set-exit-if-changed .' to fix the code style issues. packages/freezed/lib/src/templates/concrete_template.dart[error] 1-1: Dart formatter changed 1 file: lib/src/templates/concrete_template.dart. Run 'dart format' to apply changes. 🔇 Additional comments (7)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This PR implements hashCode caching for Freezed models to improve performance for large, deeply nested objects. Currently,
hashCodeis recomputed every time it's accessed, which can be expensive for complex models.Changes
int? _cachedHashCode;field andint get hashCode => _cachedHashCode ??= $hashCodeExpression;patternhashCodeMethodto conditionally apply caching based onisConstandsourceparametersisConstparameter tohashCodeMethodfunction and updated all call sitesImplementation Details
The caching is applied to:
isConst: false)source: Source.syntheticClass)Object.hashAll)Const classes and mixins continue to use the non-cached version for performance reasons.
Fixes
Resolves #1293
Example Generated Code
Before:
After:
This change ensures that hashCode computation is only performed once per object instance, providing significant performance benefits for applications with large, deeply nested Freezed models.
Summary by CodeRabbit
Bug Fixes
Performance