Avoid megamorphic calls for Cell.timestamp/ttl/path/localDeletionTimeAsUnsignedInt methods

minDeletionTime is also added to Cell to avoid double invocation of localDeletionTime method

patch by Dmitry Konstantinov; reviewed by Francisco Guerrero for CASSANDRA-21526
This commit is contained in:
Dmitry Konstantinov 2026-07-16 23:17:45 +01:00
parent bdbdf8d710
commit 838691de1b
7 changed files with 89 additions and 63 deletions

View File

@ -61,7 +61,18 @@ public abstract class AbstractCell<V> extends Cell<V>
public boolean isTombstone()
{
return localDeletionTime() != NO_DELETION_TIME && ttl() == NO_TTL;
return isTombstone(localDeletionTime());
}
public long minDeletionTime()
{
long localDeletionTime = localDeletionTime();
return isTombstone(localDeletionTime) ? Long.MIN_VALUE : localDeletionTime;
}
private boolean isTombstone(long localDeletionTime)
{
return localDeletionTime != NO_DELETION_TIME && ttl() == NO_TTL;
}
public boolean isExpiring()

View File

@ -31,17 +31,12 @@ import org.apache.cassandra.utils.memory.ByteBufferCloner;
import static org.apache.cassandra.utils.ByteArrayUtil.EMPTY_BYTE_ARRAY;
public class ArrayCell extends AbstractCell<byte[]>
public class ArrayCell extends HeapAbstractCell<byte[]>
{
private static final long EMPTY_SIZE = ObjectSizes.measure(new ArrayCell(ColumnMetadata.regularColumn("", "", "", ByteType.instance, ColumnMetadata.NO_UNIQUE_ID), 0L, 0, 0, EMPTY_BYTE_ARRAY, null));
// Careful: Adding vars here has an impact on memtable size
private final long timestamp;
private final int ttl;
private final int localDeletionTimeUnsignedInteger;
private final byte[] value;
private final CellPath path;
// Please keep both int/long overloaded ctros public. Otherwise silent casts will mess timestamps when one is not
// available.
@ -52,12 +47,8 @@ public class ArrayCell extends AbstractCell<byte[]>
public ArrayCell(ColumnMetadata column, long timestamp, int ttl, int localDeletionTimeUnsignedInteger, byte[] value, CellPath path)
{
super(column);
this.timestamp = timestamp;
this.ttl = ttl;
this.localDeletionTimeUnsignedInteger = localDeletionTimeUnsignedInteger;
super(column, timestamp, ttl, localDeletionTimeUnsignedInteger, path);
this.value = value;
this.path = path;
}
public static ArrayCell live(ColumnMetadata column, long timestamp, byte[] value, CellPath path)
@ -71,16 +62,6 @@ public class ArrayCell extends AbstractCell<byte[]>
return new ArrayCell(column, timestamp, ttl, ExpirationDateOverflowHandling.computeLocalExpirationTime(nowInSec, ttl), value, path);
}
public long timestamp()
{
return timestamp;
}
public int ttl()
{
return ttl;
}
public byte[] value()
{
return value;
@ -91,10 +72,6 @@ public class ArrayCell extends AbstractCell<byte[]>
return ByteArrayAccessor.instance;
}
public CellPath path()
{
return path;
}
public Cell<?> withUpdatedColumn(ColumnMetadata newColumn)
{
@ -144,9 +121,4 @@ public class ArrayCell extends AbstractCell<byte[]>
return EMPTY_SIZE + ObjectSizes.sizeOfArray(value) - value.length + (path == null ? 0 : path.unsharedHeapSizeExcludingData());
}
@Override
protected int localDeletionTimeAsUnsignedInt()
{
return localDeletionTimeUnsignedInteger;
}
}

View File

@ -172,7 +172,7 @@ public class BTreeRow extends AbstractRow
private static long minDeletionTime(Cell<?> cell)
{
return cell.isTombstone() ? Long.MIN_VALUE : cell.localDeletionTime();
return cell.minDeletionTime();
}
private static long minDeletionTime(LivenessInfo info)

View File

@ -30,17 +30,12 @@ import org.apache.cassandra.utils.memory.ByteBufferCloner;
import static java.lang.String.format;
public class BufferCell extends AbstractCell<ByteBuffer>
public class BufferCell extends HeapAbstractCell<ByteBuffer>
{
private static final long EMPTY_SIZE = ObjectSizes.measure(new BufferCell(ColumnMetadata.regularColumn("", "", "", ByteType.instance, ColumnMetadata.NO_UNIQUE_ID), 0L, 0, 0, ByteBufferUtil.EMPTY_BYTE_BUFFER, null));
// Careful: Adding vars here has an impact on memtable size
private final long timestamp;
private final int ttl;
private final int localDeletionTimeUnsignedInteger;
private final ByteBuffer value;
private final CellPath path;
// Please keep both int/long overloaded ctros public. Otherwise silent casts will mess timestamps when one is not
// available.
@ -51,14 +46,10 @@ public class BufferCell extends AbstractCell<ByteBuffer>
public BufferCell(ColumnMetadata column, long timestamp, int ttl, int localDeletionTimeUnsignedInteger, ByteBuffer value, CellPath path)
{
super(column);
super(column, timestamp, ttl, localDeletionTimeUnsignedInteger, path);
assert !column.isPrimaryKeyColumn();
assert column.isComplex() == (path != null) : format("Column %s.%s(%s: %s) isComplex: %b with cellpath: %s", column.ksName, column.cfName, column.name, column.type.toString(), column.isComplex(), path);
this.timestamp = timestamp;
this.ttl = ttl;
this.localDeletionTimeUnsignedInteger = localDeletionTimeUnsignedInteger;
this.value = value;
this.path = path;
}
public static BufferCell live(ColumnMetadata column, long timestamp, ByteBuffer value)
@ -92,16 +83,6 @@ public class BufferCell extends AbstractCell<ByteBuffer>
return new BufferCell(column, timestamp, NO_TTL, nowInSec, ByteBufferUtil.EMPTY_BYTE_BUFFER, path);
}
public long timestamp()
{
return timestamp;
}
public int ttl()
{
return ttl;
}
public ByteBuffer value()
{
return value;
@ -112,10 +93,6 @@ public class BufferCell extends AbstractCell<ByteBuffer>
return ByteBufferAccessor.instance;
}
public CellPath path()
{
return path;
}
public Cell<?> withUpdatedColumn(ColumnMetadata newColumn)
{
@ -163,10 +140,4 @@ public class BufferCell extends AbstractCell<ByteBuffer>
{
return EMPTY_SIZE + ObjectSizes.sizeOnHeapExcludingDataOf(value) + (path == null ? 0 : path.unsharedHeapSizeExcludingData());
}
@Override
protected int localDeletionTimeAsUnsignedInt()
{
return localDeletionTimeUnsignedInteger;
}
}

View File

@ -150,6 +150,8 @@ public abstract class Cell<V> extends ColumnData
return deletionTimeUnsignedIntegerToLong(localDeletionTimeAsUnsignedInt());
}
public abstract long minDeletionTime();
/**
* Whether the cell is a tombstone or not.
*

View File

@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.db.rows;
import org.apache.cassandra.schema.ColumnMetadata;
public abstract class HeapAbstractCell<V> extends AbstractCell<V>
{
// Careful: Adding vars here has an impact on memtable size
protected final long timestamp;
protected final int ttl;
protected final int localDeletionTimeUnsignedInteger;
protected final CellPath path;
protected HeapAbstractCell(ColumnMetadata column, long timestamp, int ttl, int localDeletionTimeUnsignedInteger, CellPath path)
{
super(column);
this.timestamp = timestamp;
this.ttl = ttl;
this.localDeletionTimeUnsignedInteger = localDeletionTimeUnsignedInteger;
this.path = path;
}
@Override
public long timestamp()
{
return timestamp;
}
@Override
public int ttl()
{
return ttl;
}
@Override
protected int localDeletionTimeAsUnsignedInt()
{
return localDeletionTimeUnsignedInteger;
}
@Override
public CellPath path()
{
return path;
}
}

View File

@ -103,6 +103,12 @@ public class CellWithSource<T> extends Cell<T>
return cell.localDeletionTime();
}
@Override
public long minDeletionTime()
{
return cell.minDeletionTime();
}
@Override
public boolean isTombstone()
{