adriacabeza opened a new issue, #2459: URL: https://github.com/apache/fory/issues/2459
### Question I have a Java personal project that I believe that could be benefit from Apache Fory and I am considering using it. It uses [Netty's ByteBuf](https://netty.io/4.1/api/io/netty/buffer/ByteBuf.html) so my current interface for the Serializers looks something like this: ```java public interface Serializer<I, O> { void write(I value, ByteBuf dos) throws IOException; O read(ByteBuf dis) throws IOException; } ``` to deal with serialization and I have handcrafted a way very clunky solution that serializes to a heap buffer just to measure its size, and then copies it into the real ByteBuf which is very clunky and expensive: ```java private static final ThreadLocal<MemoryBuffer> PROBE = ThreadLocal.withInitial(() -> MemoryBuffer.newHeapBuffer(1024)); ... /* This method runs serialization first in a cheap, thread-local heap buffer to just get the size, and then reserve exactly that much in the real, off-heap ByteBuf. */ private void serializeDirectly(Object object, ByteBuf buf) { MemoryBuffer probe = PROBE.get(); probe.readerIndex(0); probe.writerIndex(0); fory.serialize(probe, object); int len = probe.writerIndex(); // TODO: I HATE THIS, WE SHOULD NOT NEED TO SERIALIZE TWICE buf.writeInt(len); buf.ensureWritable(len); int dataStart = buf.writerIndex(); ByteBuffer bb = buf.nioBuffer(dataStart, len); MemoryBuffer mb = MemoryBuffer.fromByteBuffer(bb); fory.serialize(mb, projection); buf.writerIndex(dataStart + len); } private Object deserializeDirectly(ByteBuf buf) { int length = buf.readInt(); ByteBuffer bb = buf.nioBuffer(buf.readerIndex(), length); MemoryBuffer mb = MemoryBuffer.fromByteBuffer(bb); buf.skipBytes(length); return fory.deserialize(mb); } ``` Given that the class `MemoryBuffer` is final I cannot just extend the class and delegate every write directly into a Netty's ByteBuf. Is there any thing that you might recommend here? The current hack is slower than my current handcrafted serializer that writes what is necessary to the `ByteBuf`. I also thought about grabbing a ByteBuffer view from my `ByteBuf` and wrap that in a `MemoryBuffer` but it ends up being ugly as well and can easily `IndexOutOfBoundsException`. Also I would like to avoid changing the interface as I use it in a lot more other places. Thanks! -- 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]
