Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CASSANDRA-20162][5.0] Avoid memory allocation in NativeCell.valueSize() and NativeClustering.dataSize() #3758

Open
wants to merge 1 commit into
base: cassandra-5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/java/org/apache/cassandra/db/NativeClustering.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/java/org/apache/cassandra/db/rows/NativeCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public ValueAccessor<ByteBuffer> accessor()
return ByteBufferAccessor.instance; // FIXME: add native accessor
}

public int valueSize()
{
return MemoryUtil.getInt(peer + LENGTH);
}

public CellPath path()
{
if (!hasPath())
Expand Down
21 changes: 21 additions & 0 deletions test/unit/org/apache/cassandra/db/NativeCellTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Cell<?>> row1Iterator = row1.cells().iterator();
Iterator<Cell<?>> row2Iterator = row2.cells().iterator();
while (row1Iterator.hasNext())
{
Cell cell1 = row1Iterator.next();
Cell cell2 = row2Iterator.next();
Assert.assertEquals(cell1.dataSize(), cell2.dataSize());
}
}

}