Skip to content

Commit 42151e3

Browse files
committed
perf(python): perf and fix metashare mode in crosslanguage
1 parent 6c3f4c1 commit 42151e3

2 files changed

Lines changed: 155 additions & 0 deletions

File tree

java/fory-core/src/test/java/org/apache/fory/CrossLanguageTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,4 +831,63 @@ public void testEnumField() throws java.io.IOException {
831831
Assert.assertEquals(xserDe(fory, a), a);
832832
structRoundBack(fory, a, "test_enum_field");
833833
}
834+
835+
@Test
836+
public void testCrossLanguageMetaShare() throws Exception {
837+
Fory fory =
838+
Fory.builder()
839+
.withLanguage(Language.XLANG)
840+
.withRefTracking(true)
841+
.withCompatibleMode(CompatibleMode.COMPATIBLE)
842+
.requireClassRegistration(false)
843+
.build();
844+
fory.register(ComplexObject2.class, "test.ComplexObject2");
845+
846+
ComplexObject2 obj = new ComplexObject2();
847+
obj.f1 = true;
848+
obj.f2 = new HashMap<>(ImmutableMap.of((byte) -1, 2));
849+
850+
// Test with meta share enabled
851+
byte[] serialized = fory.serialize(obj);
852+
Assert.assertEquals(fory.deserialize(serialized), obj);
853+
854+
structRoundBack(fory, obj, "test_cross_language_meta_share");
855+
}
856+
857+
@Test
858+
public void testCrossLanguageMetaShareComplex() throws Exception {
859+
Fory fory =
860+
Fory.builder()
861+
.withLanguage(Language.XLANG)
862+
.withRefTracking(true)
863+
.withCompatibleMode(CompatibleMode.COMPATIBLE)
864+
.requireClassRegistration(false)
865+
.build();
866+
fory.register(ComplexObject1.class, "test.ComplexObject1");
867+
fory.register(ComplexObject2.class, "test.ComplexObject2");
868+
869+
ComplexObject2 obj2 = new ComplexObject2();
870+
obj2.f1 = true;
871+
obj2.f2 = ImmutableMap.of((byte) -1, 2);
872+
873+
ComplexObject1 obj = new ComplexObject1();
874+
obj.f1 = obj2;
875+
obj.f2 = "meta_share_test";
876+
obj.f3 = Arrays.asList("compatible", "mode");
877+
obj.f4 = ImmutableMap.of((byte) 1, 2);
878+
obj.f5 = Byte.MAX_VALUE;
879+
obj.f6 = Short.MAX_VALUE;
880+
obj.f7 = Integer.MAX_VALUE;
881+
obj.f8 = Long.MAX_VALUE;
882+
obj.f9 = 1.0f / 2;
883+
obj.f10 = 1 / 3.0;
884+
obj.f11 = new short[] {(short) 1, (short) 2};
885+
obj.f12 = ImmutableList.of((short) -1, (short) 4);
886+
887+
// Test with meta share enabled
888+
byte[] serialized = fory.serialize(obj);
889+
Assert.assertEquals(fory.deserialize(serialized), obj);
890+
891+
structRoundBack(fory, obj, "test_cross_language_meta_share_complex");
892+
}
834893
}

python/pyfory/tests/test_cross_language.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,102 @@ def buffer_callback(binary_object):
674674
f.write(out_of_band_buffer.to_bytes(0, out_of_band_buffer.writer_index))
675675

676676

677+
@cross_language_test
678+
def test_cross_language_meta_share(data_file_path):
679+
"""Test cross-language meta sharing with ComplexObject2."""
680+
fory = pyfory.Fory(language=pyfory.Language.XLANG, compatible=True, ref_tracking=True)
681+
682+
@dataclass
683+
class ComplexObject2:
684+
f1: Any
685+
f2: Dict[int, int] # Use plain int types to avoid type annotation issues
686+
687+
fory.register_type(ComplexObject2, namespace="test", typename="ComplexObject2")
688+
689+
with open(data_file_path, "rb") as f:
690+
data_bytes = f.read()
691+
692+
debug_print(f"Reading data of length {len(data_bytes)} from {data_file_path}")
693+
694+
# Deserialize Java-generated data with meta share
695+
obj = fory.deserialize(data_bytes)
696+
debug_print(f"Deserialized object: {obj}")
697+
698+
# Verify the object structure
699+
assert obj.f1 is True # Boolean value
700+
assert isinstance(obj.f2, dict)
701+
assert obj.f2[-1] == 2 # Dict[Int8, Int32] with key=-1, value=2
702+
703+
# Serialize back with meta share
704+
new_serialized = fory.serialize(obj)
705+
debug_print(f"Re-serialized data length: {len(new_serialized)}")
706+
707+
# Verify round-trip
708+
round_trip_obj = fory.deserialize(new_serialized)
709+
assert round_trip_obj == obj
710+
711+
# Write back for Java to verify
712+
with open(data_file_path, "wb") as f:
713+
f.write(new_serialized)
714+
715+
716+
@cross_language_test
717+
def test_cross_language_meta_share_complex(data_file_path):
718+
"""Test cross-language meta sharing with complex nested objects."""
719+
fory = pyfory.Fory(language=pyfory.Language.XLANG, compatible=True, ref_tracking=True)
720+
721+
@dataclass
722+
class ComplexObject2:
723+
f1: Any
724+
f2: Dict[int, int]
725+
726+
@dataclass
727+
class ComplexObject1:
728+
f1: Any
729+
f2: str
730+
f3: List[str]
731+
f4: Dict[int, int]
732+
f5: int
733+
f6: int
734+
f7: int
735+
f8: int
736+
f9: float
737+
f10: float
738+
f11: List[int] # Simplified from array
739+
f12: List[int]
740+
741+
fory.register_type(ComplexObject1, namespace="test", typename="ComplexObject1")
742+
fory.register_type(ComplexObject2, namespace="test", typename="ComplexObject2")
743+
744+
with open(data_file_path, "rb") as f:
745+
data_bytes = f.read()
746+
747+
debug_print(f"Reading complex data of length {len(data_bytes)} from {data_file_path}")
748+
749+
# Deserialize Java-generated complex object with meta share
750+
obj = fory.deserialize(data_bytes)
751+
debug_print(f"Deserialized complex object: {obj}")
752+
753+
# Verify the nested object structure
754+
assert hasattr(obj, 'f1') and hasattr(obj.f1, 'f1') and hasattr(obj.f1, 'f2')
755+
assert obj.f1.f1 is True
756+
assert isinstance(obj.f1.f2, dict)
757+
assert obj.f2 == "meta_share_test"
758+
assert obj.f3 == ["compatible", "mode"]
759+
760+
# Serialize back with meta share
761+
new_serialized = fory.serialize(obj)
762+
debug_print(f"Re-serialized complex data length: {len(new_serialized)}")
763+
764+
# Verify round-trip
765+
round_trip_obj = fory.deserialize(new_serialized)
766+
assert round_trip_obj == obj
767+
768+
# Write back for Java to verify
769+
with open(data_file_path, "wb") as f:
770+
f.write(new_serialized)
771+
772+
677773
if __name__ == "__main__":
678774
"""This file will be executed by CrossLanguageTest.java in fory-core/fory-format module and
679775
fory_xlang_test.go in go/fory module

0 commit comments

Comments
 (0)