From cd35a2d7cdba75e7fda22be78eb630887739ea4a Mon Sep 17 00:00:00 2001 From: Dmitry Konstantinov Date: Mon, 23 Dec 2024 10:34:16 +0000 Subject: [PATCH] Avoid memory allocation in NativeCell.valueSize() and NativeClustering.dataSize() Patch by Dmitry Konstantinov; reviewed by TBD for CASSANDRA-20162 --- .../apache/cassandra/db/NativeClustering.java | 6 ++++++ .../apache/cassandra/db/rows/NativeCell.java | 5 +++++ .../apache/cassandra/db/NativeCellTest.java | 21 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/java/org/apache/cassandra/db/NativeClustering.java b/src/java/org/apache/cassandra/db/NativeClustering.java index f3a838047036..e7c7e8893a17 100644 --- a/src/java/org/apache/cassandra/db/NativeClustering.java +++ b/src/java/org/apache/cassandra/db/NativeClustering.java @@ -93,6 +93,12 @@ public int size() return MemoryUtil.getShort(peer); } + public int dataSize() + { + int dataSizeOffset = (size() * 2) + 2; // metadataSize - 2 + return MemoryUtil.getShort(peer + dataSizeOffset); + } + public ByteBuffer get(int i) { // offset at which we store the dataOffset diff --git a/src/java/org/apache/cassandra/db/rows/NativeCell.java b/src/java/org/apache/cassandra/db/rows/NativeCell.java index a876e7d59577..b0613f33f6da 100644 --- a/src/java/org/apache/cassandra/db/rows/NativeCell.java +++ b/src/java/org/apache/cassandra/db/rows/NativeCell.java @@ -145,6 +145,11 @@ public ValueAccessor accessor() return ByteBufferAccessor.instance; // FIXME: add native accessor } + public int valueSize() + { + return MemoryUtil.getInt(peer + LENGTH); + } + public CellPath path() { if (!hasPath()) diff --git a/test/unit/org/apache/cassandra/db/NativeCellTest.java b/test/unit/org/apache/cassandra/db/NativeCellTest.java index d93dea42fecc..cf2ba4ed0dd1 100644 --- a/test/unit/org/apache/cassandra/db/NativeCellTest.java +++ b/test/unit/org/apache/cassandra/db/NativeCellTest.java @@ -18,6 +18,7 @@ package org.apache.cassandra.db; import java.nio.ByteBuffer; +import java.util.Iterator; import java.util.Random; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; @@ -163,9 +164,29 @@ private static void test(Row row) Assert.assertEquals(row.clustering(), brow.clustering()); Assert.assertEquals(nrow.clustering(), brow.clustering()); + Assert.assertEquals(row.clustering().dataSize(), nrow.clustering().dataSize()); + Assert.assertEquals(row.clustering().dataSize(), brow.clustering().dataSize()); + ClusteringComparator comparator = new ClusteringComparator(UTF8Type.instance); Assert.assertEquals(0, comparator.compare(row.clustering(), nrow.clustering())); Assert.assertEquals(0, comparator.compare(row.clustering(), brow.clustering())); Assert.assertEquals(0, comparator.compare(nrow.clustering(), brow.clustering())); + + assertCellsDataSize(row, nrow); + assertCellsDataSize(row, brow); + } + + private static void assertCellsDataSize(Row row1, Row row2) + { + Iterator> row1Iterator = row1.cells().iterator(); + Iterator> row2Iterator = row2.cells().iterator(); + while (row1Iterator.hasNext()) + { + Cell cell1 = row1Iterator.next(); + Cell cell2 = row2Iterator.next(); + Assert.assertEquals(cell1.dataSize(), cell2.dataSize()); + } + } + }