LouisLou2 commented on issue #2459:
URL: https://github.com/apache/fory/issues/2459#issuecomment-3179071569
Yes, the current limitations of Fury make the integration with Netty's
`ByteBuf` not as smooth as it could be. A current workaround (which still feels
a bit clunky) is to compare the internal buffer fields of the `MemoryBuffer` to
detect if a reallocation has occurred. If a reallocation happened, it means the
original remaining buffer space was insufficient, and a copy is unavoidable.
Here is the implementation of this pattern:
```java
private static void serializeDirectly(Fury fury, Object object, ByteBuf buf)
{
// 1. Reserve space for the length prefix.
int lenIndex = buf.writerIndex();
buf.writeInt(0); // Placeholder for length
// 2. Create a ByteBuffer view from the ByteBuf for Fury to write into.
// We save a reference to it to detect potential reallocations.
final ByteBuffer originalBuffer = buf.nioBuffer(buf.writerIndex(),
buf.writableBytes());
MemoryBuffer memBuffer = MemoryBuffer.fromByteBuffer(originalBuffer);
// 3. Perform the serialization.
fury.serialize(memBuffer, object);
int serializedBytes = memBuffer.writerIndex();
// 4. After serialization, check if Fury reallocated its internal buffer.
byte[] heapMemory = memBuffer.getHeapMemory();
if (heapMemory != null && (!originalBuffer.hasArray() ||
originalBuffer.array() != heapMemory)) {
// --- SLOW PATH (Reallocation Occurred) ---
// Fury allocated a new heap buffer. We need to copy the data.
// a. Reset the writer index to just after the length prefix.
buf.writerIndex(lenIndex + 4);
// b. Write the data from Fury's new heap buffer.
buf.writeBytes(heapMemory, 0, serializedBytes);
// c. Update the length prefix with the correct size.
buf.setInt(lenIndex, serializedBytes);
} else {
// --- FAST PATH (No Reallocation) ---
// Data was written directly into the original ByteBuf view.
// a. Just update the writer index to its final position.
buf.writerIndex(lenIndex + 4 + serializedBytes);
// b. Update the length prefix.
buf.setInt(lenIndex, serializedBytes);
}
System.out.println("Serialized " + object.getClass().getName());
}
```
*Note: I've slightly restructured and commented on your provided code to
make the logic clearer in English while preserving the core mechanism.*
cc @chaokunyang. Should we consider providing first-class support for
`ByteBuf`? Or perhaps another mechanism to allow for the extension of
`MemoryBuffer`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]