From 6bd067c41720d222080227e9b1ecd7282affcfb4 Mon Sep 17 00:00:00 2001 From: Shawn Date: Thu, 5 Oct 2023 23:38:10 +0800 Subject: [PATCH] [Java] fix varint writeindex grow for jit (#957) increase writeindex for varint in jit --- .../java/io/fury/builder/ObjectCodecBuilder.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/java/fury-core/src/main/java/io/fury/builder/ObjectCodecBuilder.java b/java/fury-core/src/main/java/io/fury/builder/ObjectCodecBuilder.java index 7756a5abf0..19f91e5abd 100644 --- a/java/fury-core/src/main/java/io/fury/builder/ObjectCodecBuilder.java +++ b/java/fury-core/src/main/java/io/fury/builder/ObjectCodecBuilder.java @@ -289,14 +289,25 @@ private List serializePrimitivesUnCompressed( private List serializePrimitivesCompressed( Expression bean, Expression buffer, List> primitiveGroups, int totalSize) { List expressions = new ArrayList<>(); - int numPrimitiveFields = getNumPrimitiveFields(primitiveGroups); // int/long may need extra one-byte for writing. - int growSize = (int) (totalSize + primitiveGroups.stream().mapToLong(Collection::size).sum()); + int extraSize = 0; + for (List group : primitiveGroups) { + for (Descriptor d : group) { + if (d.getRawType() == int.class) { + // varint may be written as 5bytes, use 8bytes for written as long to reduce cost. + extraSize += 4; + } else if (d.getRawType() == long.class) { + extraSize += 1; // long use 1~9 bytes. + } + } + } + int growSize = totalSize + extraSize; // After this grow, following writes can be unsafe without checks. expressions.add(new Invoke(buffer, "grow", Literal.ofInt(growSize))); // Must grow first, otherwise may get invalid address. Expression base = new Invoke(buffer, "getHeapMemory", "base", PRIMITIVE_BYTE_ARRAY_TYPE); expressions.add(base); + int numPrimitiveFields = getNumPrimitiveFields(primitiveGroups); for (List group : primitiveGroups) { ListExpression groupExpressions = new ListExpression(); Expression writerAddr =