[pigeon] Optimize data class equality and hashing in Dart, Kotlin, and Swift#11140
Open
tarrinneal wants to merge 2 commits intoflutter:mainfrom
Open
[pigeon] Optimize data class equality and hashing in Dart, Kotlin, and Swift#11140tarrinneal wants to merge 2 commits intoflutter:mainfrom
tarrinneal wants to merge 2 commits intoflutter:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces significant performance optimizations for data class equality and hashing in Dart, Kotlin, and Swift by avoiding the creation of intermediate lists. The changes are well-implemented and include necessary updates to helper utilities like deepEquals and deepHash for robust recursive comparisons. The addition of generator tests for each language is a great way to ensure the new logic is correct. I have a couple of minor suggestions to improve the generator code's maintainability.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR optimizes the generated equality (==) and hashing (hashCode/hash) logic for data classes across Dart, Kotlin, and Swift.
Previously, Pigeon relied on converting objects to lists (via toList() or encode()) to perform comparisons. This approach was inefficient as it required new object allocations for every equality check or hash operation.
Key Improvements:
Dart:
Directly compares fields in "operator ==".
Uses Object.hash (for multiple fields) or field hashCode (for single fields) instead of hashing a generated list.
Includes a deepEquals utility for robust recursive comparison of nested collections.
Kotlin:
Generates equals and hashCode that utilize a new PigeonUtils helper.
Implements deepEquals and deepHash to correctly handle ByteArray, IntArray, LongArray, DoubleArray, FloatArray, List, and Map types recursively by value.
Swift:
Implements "static func ==" and "func hash(into:)" without calling toList().
Adds deepEquals and deepHash internal utilities to handle nested arrays and dictionaries by value, ensuring parity with Dart's equality semantics.
Fixes:
[Dart] Fixed a bug where classes with exactly one field would cause an error in Object.hash (which requires at least two arguments).
[Swift] Resolved compiler warnings related to let bindings in switch statements and type inference in deepHash.
Tests
Added comprehensive generator tests in dart_generator_test.dart, kotlin_generator_test.dart, and swift_generator_test.dart to verify the new code patterns.
Verified that all android_kotlin_integration_tests and ios_swift_integration_tests pass with the new implementation.