Skip to content

[pigeon] Optimize data class equality and hashing in Dart, Kotlin, and Swift#11140

Open
tarrinneal wants to merge 2 commits intoflutter:mainfrom
tarrinneal:effic
Open

[pigeon] Optimize data class equality and hashing in Dart, Kotlin, and Swift#11140
tarrinneal wants to merge 2 commits intoflutter:mainfrom
tarrinneal:effic

Conversation

@tarrinneal
Copy link
Contributor

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.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant