This is an automated email from the ASF dual-hosted git repository.

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git


The following commit(s) were added to refs/heads/main by this push:
     new d38534ae fix(java): ClassDef header calculation error (#2382)
d38534ae is described below

commit d38534ae3ae5005acf8c494410d2192e09c45fc9
Author: LiangliangSui <116876207+liangliang...@users.noreply.github.com>
AuthorDate: Sun Jul 6 18:58:22 2025 +0800

    fix(java): ClassDef header calculation error (#2382)
    
    <!--
    **Thanks for contributing to Fory.**
    
    **If this is your first time opening a PR on fory, you can refer to
    
[CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).**
    
    Contribution Checklist
    
    - The **Apache Fory (incubating)** community has restrictions on the
    naming of pr titles. You can also find instructions in
    [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).
    
    - Fory has a strong focus on performance. If the PR you submit will have
    an impact on performance, please benchmark it first and provide the
    benchmark result here.
    -->
    
    ## What does this PR do?
    
    <!-- Describe the purpose of this PR. -->
    
    1. The lower 12 bits of the header are metaSize.
    
    2. When `metaSize > META_SIZE_MASKS`, write `META_SIZE_MASKS`;
    otherwise, write `metaSize`.
    
    ## Related issues
    
    <!--
    Is there any related issue? Please attach here.
    
    - #xxxx0
    - #xxxx1
    - #xxxx2
    -->
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/fory/issues/new/choose) describing the
    need to do so and update the document if necessary.
    -->
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    ## Benchmark
    
    <!--
    When the PR has an impact on performance (if you don't know whether the
    PR will have an impact on performance, you can submit the PR first, and
    if it will have impact on performance, the code reviewer will explain
    it), be sure to attach a benchmark data here.
    -->
    
    Signed-off-by: Liangliang Sui <coolsui.cod...@gmail.com>
---
 .../src/main/java/org/apache/fory/meta/ClassDef.java         |  3 ++-
 .../src/main/java/org/apache/fory/meta/ClassDefEncoder.java  |  5 +----
 .../test/java/org/apache/fory/meta/ClassDefEncoderTest.java  | 12 ++++++++++++
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/java/fory-core/src/main/java/org/apache/fory/meta/ClassDef.java 
b/java/fory-core/src/main/java/org/apache/fory/meta/ClassDef.java
index ff3a46ff..1e5b14bc 100644
--- a/java/fory-core/src/main/java/org/apache/fory/meta/ClassDef.java
+++ b/java/fory-core/src/main/java/org/apache/fory/meta/ClassDef.java
@@ -85,7 +85,8 @@ import org.apache.fory.util.Preconditions;
 public class ClassDef implements Serializable {
   static final int COMPRESS_META_FLAG = 0b1 << 13;
   static final int HAS_FIELDS_META_FLAG = 0b1 << 12;
-  static final int META_SIZE_MASKS = 0b111_1111_1111;
+  // low 12 bits
+  static final int META_SIZE_MASKS = 0xfff;
   static final int NUM_HASH_BITS = 50;
   private static final Logger LOG = LoggerFactory.getLogger(ClassDef.class);
 
diff --git 
a/java/fory-core/src/main/java/org/apache/fory/meta/ClassDefEncoder.java 
b/java/fory-core/src/main/java/org/apache/fory/meta/ClassDefEncoder.java
index 6d516c12..53c6b046 100644
--- a/java/fory-core/src/main/java/org/apache/fory/meta/ClassDefEncoder.java
+++ b/java/fory-core/src/main/java/org/apache/fory/meta/ClassDefEncoder.java
@@ -195,10 +195,7 @@ class ClassDefEncoder {
     if (hasFieldsMeta) {
       header |= HAS_FIELDS_META_FLAG;
     }
-    if (metaSize > META_SIZE_MASKS) {
-      header |= META_SIZE_MASKS;
-    }
-    header |= metaSize;
+    header |= Math.min(metaSize, META_SIZE_MASKS);
     MemoryBuffer result = MemoryUtils.buffer(metaSize + 8);
     result.writeInt64(header);
     if (metaSize > META_SIZE_MASKS) {
diff --git 
a/java/fory-core/src/test/java/org/apache/fory/meta/ClassDefEncoderTest.java 
b/java/fory-core/src/test/java/org/apache/fory/meta/ClassDefEncoderTest.java
index 75911c77..eec203fd 100644
--- a/java/fory-core/src/test/java/org/apache/fory/meta/ClassDefEncoderTest.java
+++ b/java/fory-core/src/test/java/org/apache/fory/meta/ClassDefEncoderTest.java
@@ -107,4 +107,16 @@ public class ClassDefEncoderTest {
       private Long itemId;
     }
   }
+
+  @Test
+  public void testPrependHeader() {
+    MemoryBuffer inputBuffer = 
MemoryBuffer.newHeapBuffer(ClassDef.META_SIZE_MASKS + 1);
+    inputBuffer.writerIndex(ClassDef.META_SIZE_MASKS + 1);
+    MemoryBuffer outputBuffer = ClassDefEncoder.prependHeader(inputBuffer, 
true, false);
+
+    long header = outputBuffer.readInt64();
+    Assert.assertEquals(header & ClassDef.META_SIZE_MASKS, 
ClassDef.META_SIZE_MASKS);
+    Assert.assertEquals(header & ClassDef.COMPRESS_META_FLAG, 
ClassDef.COMPRESS_META_FLAG);
+    Assert.assertEquals(header & ClassDef.HAS_FIELDS_META_FLAG, 0);
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@fory.apache.org
For additional commands, e-mail: commits-h...@fory.apache.org

Reply via email to