This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fury.git
The following commit(s) were added to refs/heads/main by this push:
new c9d1f468 fix(python): inconsistent struct hash calculation between
Java and Python (#2108)
c9d1f468 is described below
commit c9d1f46842957c72666586dfe9d0e2e3b80fcb7a
Author: LouShaokun <[email protected]>
AuthorDate: Sat Mar 22 15:50:07 2025 +0800
fix(python): inconsistent struct hash calculation between Java and Python
(#2108)
<!--
**Thanks for contributing to Fury.**
**If this is your first time opening a PR on fury, you can refer to
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).**
Contribution Checklist
- The **Apache Fury (incubating)** community has restrictions on the
naming of pr titles. You can also find instructions in
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).
- Fury has a strong focus on performance. If the PR you submit will have
an impact on performance, please benchmark it first and provide the
benchmark result here.
-->
## What does this PR do?
### Problem
The struct hash calculation implementations in Java and Python are
inconsistent. When `classinfo` is `None` in the Python implementation,
the method returns early without updating the hash value, whereas the
Java implementation continues with a default hash value of 0.
### Solution
Modified the `visit_customized` method in the Python implementation to
initialize `hash_value` to 0 before checking if `classinfo` is `None`,
ensuring the hash computation continues regardless of the `classinfo`
status. This approach aligns with the Java implementation behavior.
Changes:
- Initialize `hash_value` to 0 at the beginning of the method
- Replace the early return with a conditional assignment to `hash_value`
- Always call `_compute_field_hash` at the end of the method
### Testing
Verified the fix by running the `CrossLanguageTest.java` tests that were
previously failing due to this issue. The struct hash values now match
between Java and Python implementations.
## Related issues
- #2107
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/issues/new/choose) describing the
need to do so and update the document if necessary.
-->
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
---
python/pyfury/_struct.py | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/python/pyfury/_struct.py b/python/pyfury/_struct.py
index bd00f950..9b639315 100644
--- a/python/pyfury/_struct.py
+++ b/python/pyfury/_struct.py
@@ -177,11 +177,13 @@ class StructHashVisitor(TypeVisitor):
def visit_customized(self, field_name, type_, types_path=None):
classinfo = self.fury.class_resolver.get_classinfo(type_, create=False)
- if classinfo is None:
- return
- hash_value = classinfo.type_id
- if TypeId.is_namespaced_type(classinfo.type_id):
- hash_value = compute_string_hash(classinfo.namespace +
classinfo.typename)
+ hash_value = 0
+ if classinfo is not None:
+ hash_value = classinfo.type_id
+ if TypeId.is_namespaced_type(classinfo.type_id):
+ hash_value = compute_string_hash(
+ classinfo.namespace + classinfo.typename
+ )
self._hash = self._compute_field_hash(self._hash, hash_value)
def visit_other(self, field_name, type_, types_path=None):
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]