-
Couldn't load subscription status.
- Fork 312
feat: Align object array to collection serialization protocol v2 #2218
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
Draft
david1437
wants to merge
5
commits into
apache:main
Choose a base branch
from
david1437:#1229
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−66
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
561c780
feat: Align object array to collection serialization protocol v2
david1437 56002e2
fix: build collection type dynamically with TypeUtils
david1437 3cdf8ef
fix: cache ArrayAsList and use collectionm serializer for 1D objects …
david1437 7e2d793
fix: handle depth during write serialization
david1437 24ee77c
fix: Handle primitive casting in read
david1437 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,12 +26,17 @@ | |
| import org.apache.fury.config.CompatibleMode; | ||
| import org.apache.fury.memory.MemoryBuffer; | ||
| import org.apache.fury.memory.Platform; | ||
| import org.apache.fury.reflect.TypeRef; | ||
| import org.apache.fury.resolver.ClassInfo; | ||
| import org.apache.fury.resolver.ClassInfoHolder; | ||
| import org.apache.fury.resolver.ClassResolver; | ||
| import org.apache.fury.resolver.RefResolver; | ||
| import org.apache.fury.serializer.NonexistentClass.NonexistentSkip; | ||
| import org.apache.fury.serializer.NonexistentClassSerializers.NonexistentEnumClassSerializer; | ||
| import org.apache.fury.serializer.Serializers.CrossLanguageCompatibleSerializer; | ||
| import org.apache.fury.serializer.collection.CollectionFlags; | ||
| import org.apache.fury.serializer.collection.FuryArrayAsListSerializer; | ||
| import org.apache.fury.serializer.collection.FuryArrayAsListSerializer.ArrayAsList; | ||
| import org.apache.fury.type.GenericType; | ||
| import org.apache.fury.type.TypeUtils; | ||
| import org.apache.fury.type.Types; | ||
|
|
@@ -45,6 +50,9 @@ public class ArraySerializers { | |
| public static final class ObjectArraySerializer<T> extends Serializer<T[]> { | ||
| private final Class<T> innerType; | ||
| private final Serializer componentTypeSerializer; | ||
| private final FuryArrayAsListSerializer collectionSerializer; | ||
| private ArrayAsList list; | ||
| private final GenericType collectionGenericType; | ||
| private final ClassInfoHolder classInfoHolder; | ||
| private final int[] stubDims; | ||
| private final GenericType componentGenericType; | ||
|
|
@@ -69,14 +77,21 @@ public ObjectArraySerializer(Fury fury, Class<T[]> cls) { | |
| if (fury.getClassResolver().isMonomorphic(componentType)) { | ||
| if (fury.isCrossLanguage()) { | ||
| this.componentTypeSerializer = null; | ||
| this.collectionSerializer = null; | ||
| this.collectionGenericType = null; | ||
| } else { | ||
| this.componentTypeSerializer = fury.getClassResolver().getSerializer(componentType); | ||
| this.collectionSerializer = new FuryArrayAsListSerializer(fury); | ||
| this.collectionGenericType = buildCollectionGenericType(dimension); | ||
| } | ||
| } else { | ||
| // TODO add ClassInfo cache for non-final component type. | ||
| this.componentTypeSerializer = null; | ||
| this.collectionSerializer = null; | ||
| this.collectionGenericType = null; | ||
| } | ||
| this.stubDims = new int[dimension]; | ||
| this.list = null; | ||
| classInfoHolder = fury.getClassResolver().nilClassInfoHolder(); | ||
| } | ||
|
|
||
|
|
@@ -85,52 +100,81 @@ public void write(MemoryBuffer buffer, T[] arr) { | |
| int len = arr.length; | ||
| RefResolver refResolver = fury.getRefResolver(); | ||
| Serializer componentSerializer = this.componentTypeSerializer; | ||
| int header = componentSerializer != null ? 0b1 : 0b0; | ||
| buffer.writeVarUint32Small7(len << 1 | header); | ||
| if (componentSerializer != null) { | ||
| for (T t : arr) { | ||
| if (!refResolver.writeRefOrNull(buffer, t)) { | ||
| componentSerializer.write(buffer, t); | ||
| } | ||
| if (this.collectionSerializer != null) { | ||
| buffer.writeVarUint32Small7(len); | ||
| if (len == 0) { | ||
| return; | ||
| } | ||
| ArrayAsList list = this.list; | ||
| if (list == null) { | ||
| list = new ArrayAsList(0); | ||
| } else { | ||
| this.list = null; | ||
| } | ||
| list.setArray(arr); | ||
| fury.incDepth(1); | ||
| fury.getGenerics().pushGenericType(this.collectionGenericType); | ||
| this.collectionSerializer.write(buffer, list); | ||
| fury.getGenerics().popGenericType(); | ||
| fury.incDepth(-1); | ||
| this.list = list; | ||
| } else { | ||
| Fury fury = this.fury; | ||
| ClassResolver classResolver = fury.getClassResolver(); | ||
| ClassInfo classInfo = null; | ||
| Class<?> elemClass = null; | ||
| for (T t : arr) { | ||
| if (!refResolver.writeRefOrNull(buffer, t)) { | ||
| Class<?> clz = t.getClass(); | ||
| if (clz != elemClass) { | ||
| elemClass = clz; | ||
| classInfo = classResolver.getClassInfo(clz); | ||
| int header = componentSerializer != null ? 0b1 : 0b0; | ||
| buffer.writeVarUint32Small7(len << 1 | header); | ||
| if (componentSerializer != null) { | ||
| for (T t : arr) { | ||
| if (!refResolver.writeRefOrNull(buffer, t)) { | ||
| componentSerializer.write(buffer, t); | ||
| } | ||
| } | ||
| } else { | ||
| Fury fury = this.fury; | ||
| ClassResolver classResolver = fury.getClassResolver(); | ||
| ClassInfo classInfo = null; | ||
| Class<?> elemClass = null; | ||
| for (T t : arr) { | ||
| if (!refResolver.writeRefOrNull(buffer, t)) { | ||
| Class<?> clz = t.getClass(); | ||
| if (clz != elemClass) { | ||
| elemClass = clz; | ||
| classInfo = classResolver.getClassInfo(clz); | ||
| } | ||
| fury.writeNonRef(buffer, t, classInfo); | ||
| } | ||
| fury.writeNonRef(buffer, t, classInfo); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public T[] copy(T[] originArray) { | ||
| int length = originArray.length; | ||
| Object[] newArray = newArray(length); | ||
| if (needToCopyRef) { | ||
| fury.reference(originArray, newArray); | ||
| } | ||
| Object[] newArray; | ||
| Serializer componentSerializer = this.componentTypeSerializer; | ||
| if (componentSerializer != null) { | ||
| if (componentSerializer.isImmutable()) { | ||
| System.arraycopy(originArray, 0, newArray, 0, length); | ||
| if (this.collectionSerializer != null) { | ||
| fury.getGenerics().pushGenericType(this.collectionGenericType); | ||
| ArrayAsList list = new ArrayAsList(originArray.length); | ||
| list.setArray(originArray); | ||
| newArray = this.collectionSerializer.copy(list).toArray(); | ||
| fury.getGenerics().popGenericType(); | ||
| } else { | ||
| int length = originArray.length; | ||
| newArray = newArray(length); | ||
| if (needToCopyRef) { | ||
| fury.reference(originArray, newArray); | ||
| } | ||
| if (componentSerializer != null) { | ||
| if (componentSerializer.isImmutable()) { | ||
| System.arraycopy(originArray, 0, newArray, 0, length); | ||
| } else { | ||
| for (int i = 0; i < length; i++) { | ||
| newArray[i] = componentSerializer.copy(originArray[i]); | ||
| } | ||
| } | ||
| } else { | ||
| for (int i = 0; i < length; i++) { | ||
| newArray[i] = componentSerializer.copy(originArray[i]); | ||
| newArray[i] = fury.copyObject(originArray[i]); | ||
| } | ||
| } | ||
| } else { | ||
| for (int i = 0; i < length; i++) { | ||
| newArray[i] = fury.copyObject(originArray[i]); | ||
| } | ||
| } | ||
| return (T[]) newArray; | ||
| } | ||
|
|
@@ -147,39 +191,49 @@ public void xwrite(MemoryBuffer buffer, T[] arr) { | |
|
|
||
| @Override | ||
| public T[] read(MemoryBuffer buffer) { | ||
| Object[] value; | ||
| int numElements = buffer.readVarUint32Small7(); | ||
| boolean isFinal = (numElements & 0b1) != 0; | ||
| numElements >>>= 1; | ||
| Object[] value = newArray(numElements); | ||
| RefResolver refResolver = fury.getRefResolver(); | ||
| refResolver.reference(value); | ||
| if (isFinal) { | ||
| final Serializer componentTypeSerializer = this.componentTypeSerializer; | ||
| for (int i = 0; i < numElements; i++) { | ||
| Object elem; | ||
| int nextReadRefId = refResolver.tryPreserveRefId(buffer); | ||
| if (nextReadRefId >= Fury.NOT_NULL_VALUE_FLAG) { | ||
| elem = componentTypeSerializer.read(buffer); | ||
| refResolver.setReadObject(nextReadRefId, elem); | ||
| } else { | ||
| elem = refResolver.getReadObject(); | ||
| } | ||
| value[i] = elem; | ||
| if (this.collectionSerializer != null) { | ||
| fury.getGenerics().pushGenericType(this.collectionGenericType); | ||
| value = this.collectionSerializer.read(buffer).toArray(); | ||
| fury.getGenerics().popGenericType(); | ||
| if (this.innerType.isPrimitive() || TypeUtils.isBoxed(this.innerType)) { | ||
| return Arrays.copyOf(value, value.length, this.type); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Considering this does a copy it does not feel good is there a better approach for handling this? without this i get a class cast exception? |
||
| } | ||
| } else { | ||
| Fury fury = this.fury; | ||
| ClassInfoHolder classInfoHolder = this.classInfoHolder; | ||
| for (int i = 0; i < numElements; i++) { | ||
| int nextReadRefId = refResolver.tryPreserveRefId(buffer); | ||
| Object o; | ||
| if (nextReadRefId >= Fury.NOT_NULL_VALUE_FLAG) { | ||
| // ref value or not-null value | ||
| o = fury.readNonRef(buffer, classInfoHolder); | ||
| refResolver.setReadObject(nextReadRefId, o); | ||
| } else { | ||
| o = refResolver.getReadObject(); | ||
| boolean isFinal = (numElements & 0b1) != 0; | ||
| numElements >>>= 1; | ||
| value = newArray(numElements); | ||
| RefResolver refResolver = fury.getRefResolver(); | ||
| refResolver.reference(value); | ||
| if (isFinal) { | ||
| final Serializer componentTypeSerializer = this.componentTypeSerializer; | ||
| for (int i = 0; i < numElements; i++) { | ||
| Object elem; | ||
| int nextReadRefId = refResolver.tryPreserveRefId(buffer); | ||
| if (nextReadRefId >= Fury.NOT_NULL_VALUE_FLAG) { | ||
| elem = componentTypeSerializer.read(buffer); | ||
| refResolver.setReadObject(nextReadRefId, elem); | ||
| } else { | ||
| elem = refResolver.getReadObject(); | ||
| } | ||
| value[i] = elem; | ||
| } | ||
| } else { | ||
| Fury fury = this.fury; | ||
| ClassInfoHolder classInfoHolder = this.classInfoHolder; | ||
| for (int i = 0; i < numElements; i++) { | ||
| int nextReadRefId = refResolver.tryPreserveRefId(buffer); | ||
| Object o; | ||
| if (nextReadRefId >= Fury.NOT_NULL_VALUE_FLAG) { | ||
| // ref value or not-null value | ||
| o = fury.readNonRef(buffer, classInfoHolder); | ||
| refResolver.setReadObject(nextReadRefId, o); | ||
| } else { | ||
| o = refResolver.getReadObject(); | ||
| } | ||
| value[i] = o; | ||
| } | ||
| value[i] = o; | ||
| } | ||
| } | ||
| return (T[]) value; | ||
|
|
@@ -208,6 +262,14 @@ private Object[] newArray(int numElements) { | |
| } | ||
| return value; | ||
| } | ||
|
|
||
| private GenericType buildCollectionGenericType(int dims) { | ||
| TypeRef arrayType = TypeRef.of(this.innerType); | ||
| for (int i = 0; i < dims; i++) { | ||
| arrayType = TypeUtils.collectionOf(arrayType); | ||
| } | ||
| return GenericType.build(arrayType); | ||
| } | ||
| } | ||
|
|
||
| public static final class PrimitiveArrayBufferObject implements BufferObject { | ||
|
|
@@ -249,7 +311,7 @@ public MemoryBuffer toBuffer() { | |
| // Implement all read/write methods in subclasses to avoid | ||
| // virtual method call cost. | ||
| public abstract static class PrimitiveArraySerializer<T> | ||
| extends Serializers.CrossLanguageCompatibleSerializer<T> { | ||
| extends CrossLanguageCompatibleSerializer<T> { | ||
| protected final int offset; | ||
| protected final int elemSize; | ||
|
|
||
|
|
@@ -610,14 +672,14 @@ public double[] read(MemoryBuffer buffer) { | |
| public static final class StringArraySerializer extends Serializer<String[]> { | ||
| private final StringSerializer stringSerializer; | ||
| private final FuryArrayAsListSerializer collectionSerializer; | ||
| private final FuryArrayAsListSerializer.ArrayAsList list; | ||
| private final ArrayAsList list; | ||
|
|
||
| public StringArraySerializer(Fury fury) { | ||
| super(fury, String[].class); | ||
| stringSerializer = new StringSerializer(fury); | ||
| collectionSerializer = new FuryArrayAsListSerializer(fury); | ||
| collectionSerializer.setElementSerializer(stringSerializer); | ||
| list = new FuryArrayAsListSerializer.ArrayAsList(0); | ||
| list = new ArrayAsList(0); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -890,11 +952,10 @@ public NonexistentArrayClassSerializer(Fury fury, Class<?> cls) { | |
| public NonexistentArrayClassSerializer(Fury fury, String className, Class<?> cls) { | ||
| super(fury, className, cls); | ||
| if (TypeUtils.getArrayComponent(cls).isEnum()) { | ||
| componentSerializer = new NonexistentClassSerializers.NonexistentEnumClassSerializer(fury); | ||
| componentSerializer = new NonexistentEnumClassSerializer(fury); | ||
| } else { | ||
| if (fury.getConfig().getCompatibleMode() == CompatibleMode.COMPATIBLE) { | ||
| componentSerializer = | ||
| new CompatibleSerializer<>(fury, NonexistentClass.NonexistentSkip.class); | ||
| componentSerializer = new CompatibleSerializer<>(fury, NonexistentSkip.class); | ||
| } else { | ||
| componentSerializer = null; | ||
| } | ||
|
|
||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.