Skip to content

Commit

Permalink
Merge pull request #123 from apache/GetCapacityDirect
Browse files Browse the repository at this point in the history
Get capacity direct
  • Loading branch information
leerho committed Dec 1, 2020
2 parents 7d0278d + d866a0d commit a3b3c7e
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions src/main/java/org/apache/datasketches/memory/BaseState.java
Expand Up @@ -38,7 +38,7 @@ abstract class BaseState {

//Byte Order related
static final ByteOrder nativeByteOrder = ByteOrder.nativeOrder();
static final ByteOrder nonNativeByteOrder = (nativeByteOrder == ByteOrder.LITTLE_ENDIAN)
static final ByteOrder nonNativeByteOrder = nativeByteOrder == ByteOrder.LITTLE_ENDIAN
? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;

//Monitoring
Expand Down Expand Up @@ -92,7 +92,7 @@ abstract class BaseState {
BaseState(final Object unsafeObj, final long nativeBaseOffset, final long regionOffset,
final long capacityBytes) {
capacityBytes_ = capacityBytes;
cumBaseOffset_ = regionOffset + ((unsafeObj == null)
cumBaseOffset_ = regionOffset + (unsafeObj == null
? nativeBaseOffset
: UnsafeUtil.getArrayBaseOffset(unsafeObj.getClass()));
}
Expand Down Expand Up @@ -125,7 +125,7 @@ static boolean isNativeByteOrder(final ByteOrder byteOrder) {
if (byteOrder == null) {
throw new IllegalArgumentException("ByteOrder parameter cannot be null.");
}
return (BaseState.nativeByteOrder == byteOrder);
return BaseState.nativeByteOrder == byteOrder;
}

/**
Expand All @@ -137,7 +137,7 @@ static boolean isNativeByteOrder(final ByteOrder byteOrder) {
*/
public final boolean isByteOrderCompatible(final ByteOrder byteOrder) {
final ByteOrder typeBO = getTypeByteOrder();
return ((typeBO == getNativeByteOrder()) && (typeBO == byteOrder));
return typeBO == getNativeByteOrder() && typeBO == byteOrder;
}

/**
Expand All @@ -148,8 +148,8 @@ public final boolean isByteOrderCompatible(final ByteOrder byteOrder) {
@Override
public final boolean equals(final Object that) {
if (this == that) { return true; }
return (that instanceof BaseState)
? CompareAndCopy.equals(this, ((BaseState) that))
return that instanceof BaseState
? CompareAndCopy.equals(this, (BaseState) that)
: false;
}

Expand All @@ -166,7 +166,7 @@ public final boolean equals(final Object that) {
*/
public final boolean equalTo(final long thisOffsetBytes, final Object that,
final long thatOffsetBytes, final long lengthBytes) {
return (that instanceof BaseState)
return that instanceof BaseState
? CompareAndCopy.equals(this, thisOffsetBytes, (BaseState) that, thatOffsetBytes, lengthBytes)
: false;
}
Expand Down Expand Up @@ -231,7 +231,7 @@ long getNativeBaseOffset() {
*/
public final long getRegionOffset() {
final Object unsafeObj = getUnsafeObject();
return (unsafeObj == null)
return unsafeObj == null
? cumBaseOffset_ - getNativeBaseOffset()
: cumBaseOffset_ - UnsafeUtil.getArrayBaseOffset(unsafeObj.getClass());
}
Expand Down Expand Up @@ -281,7 +281,7 @@ public final boolean hasArray() {
*/
@Override
public final int hashCode() {
return (int) xxHash64(0, getCapacity(), 0);
return (int) xxHash64(0, capacityBytes_, 0); //xxHash64() calls checkValid()
}

/**
Expand All @@ -297,7 +297,7 @@ public final int hashCode() {
*/
public final long xxHash64(final long offsetBytes, final long lengthBytes, final long seed) {
checkValid();
return XxHash64.hash(getUnsafeObject(), getCumulativeOffset() + offsetBytes, lengthBytes, seed);
return XxHash64.hash(getUnsafeObject(), cumBaseOffset_ + offsetBytes, lengthBytes, seed);
}

/**
Expand Down Expand Up @@ -342,10 +342,10 @@ public final boolean isSameResource(final Object that) {
that1.checkValid();
if (this == that1) { return true; }

return (getCumulativeOffset() == that1.getCumulativeOffset())
&& (getCapacity() == that1.getCapacity())
&& (getUnsafeObject() == that1.getUnsafeObject())
&& (getByteBuffer() == that1.getByteBuffer());
return cumBaseOffset_ == that1.cumBaseOffset_
&& capacityBytes_ == that1.capacityBytes_
&& getUnsafeObject() == that1.getUnsafeObject()
&& getByteBuffer() == that1.getByteBuffer();
}

/**
Expand All @@ -371,12 +371,18 @@ final void checkValid() {

final void assertValidAndBoundsForRead(final long offsetBytes, final long lengthBytes) {
assertValid();
assertBounds(offsetBytes, lengthBytes, getCapacity());
// capacityBytes_ is intentionally read directly instead of calling getCapacity()
// because the later can make JVM to not inline the assert code path (and entirely remove it)
// even though it does nothing in production code path.
assertBounds(offsetBytes, lengthBytes, capacityBytes_);
}

final void assertValidAndBoundsForWrite(final long offsetBytes, final long lengthBytes) {
assertValid();
assertBounds(offsetBytes, lengthBytes, getCapacity());
// capacityBytes_ is intentionally read directly instead of calling getCapacity()
// because the later can make JVM to not inline the assert code path (and entirely remove it)
// even though it does nothing in production code path.
assertBounds(offsetBytes, lengthBytes, capacityBytes_);
assert !isReadOnly() : "Memory is read-only.";
}

Expand All @@ -389,12 +395,14 @@ final void assertValidAndBoundsForWrite(final long offsetBytes, final long lengt
*/
public final void checkValidAndBounds(final long offsetBytes, final long lengthBytes) {
checkValid();
checkBounds(offsetBytes, lengthBytes, getCapacity());
//read capacityBytes_ directly to eliminate extra checkValid() call
checkBounds(offsetBytes, lengthBytes, capacityBytes_);
}

final void checkValidAndBoundsForWrite(final long offsetBytes, final long lengthBytes) {
checkValid();
checkBounds(offsetBytes, lengthBytes, getCapacity());
//read capacityBytes_ directly to eliminate extra checkValid() call
checkBounds(offsetBytes, lengthBytes, capacityBytes_);
if (isReadOnly()) {
throw new ReadOnlyException("Memory is read-only.");
}
Expand Down Expand Up @@ -422,19 +430,19 @@ final boolean isNonNativeType() {
}

final boolean isHeapType() {
return ((getTypeId() >>> 3) & 3) == 0;
return (getTypeId() >>> 3 & 3) == 0;
}

final boolean isDirectType() {
return ((getTypeId() >>> 3) & 3) == 1;
return (getTypeId() >>> 3 & 3) == 1;
}

final boolean isMapType() {
return ((getTypeId() >>> 3) & 3) == 2;
return (getTypeId() >>> 3 & 3) == 2;
}

final boolean isBBType() {
return ((getTypeId() >>> 3) & 3) == 3;
return (getTypeId() >>> 3 & 3) == 3;
}

//MONITORING
Expand Down Expand Up @@ -521,10 +529,10 @@ static final String toHex(final BaseState state, final String preamble, final lo
uObjHeader = UnsafeUtil.getArrayBaseOffset(uObj.getClass());
}
final ByteBuffer bb = state.getByteBuffer();
final String bbStr = (bb == null) ? "null"
final String bbStr = bb == null ? "null"
: bb.getClass().getSimpleName() + ", " + (bb.hashCode() & 0XFFFFFFFFL);
final MemoryRequestServer memReqSvr = state.getMemoryRequestServer();
final String memReqStr = (memReqSvr != null)
final String memReqStr = memReqSvr != null
? memReqSvr.getClass().getSimpleName() + ", " + (memReqSvr.hashCode() & 0XFFFFFFFFL)
: "null";
final long cumBaseOffset = state.getCumulativeOffset();
Expand All @@ -546,7 +554,7 @@ static final String toHex(final BaseState state, final String preamble, final lo

for (long i = 0; i < lengthBytes; i++) {
final int b = unsafe.getByte(uObj, cumBaseOffset + offsetBytes + i) & 0XFF;
if ((i % 8) == 0) { //row header
if (i % 8 == 0) { //row header
sb.append(String.format("%n%20s: ", offsetBytes + i));
}
sb.append(String.format("%02x ", b));
Expand Down

0 comments on commit a3b3c7e

Please sign in to comment.