Skip to content

Commit

Permalink
Avoid null checks if we can
Browse files Browse the repository at this point in the history
We know that the myElements entries will be non-null, so we don't need
to check for that.
  • Loading branch information
iamsrp-deshaw committed Sep 27, 2024
1 parent 1b84a7c commit 61f74b9
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 319 deletions.
83 changes: 19 additions & 64 deletions java/src/main/java/com/deshaw/hypercube/DoubleArrayHypercube.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,7 @@ public DoubleArrayHypercube(final Dimension<?>[] dimensions,
public void fill(final double v)
{
for (int i=0; i < myElements.length(); i++) {
double[] elements = myElements.get(i);
if (elements == null) {
elements = allocForIndex(i);
myElements.set(i, elements);
}
Arrays.fill(elements, v);
Arrays.fill(myElements.get(i), v);
}
}

Expand Down Expand Up @@ -146,8 +141,7 @@ public void toFlattenedObjs(final long srcPos,
for (int i=0; i < length; i++) {
final long pos = srcPos + i;
final double[] array = myElements.get((int)(pos >>> MAX_ARRAY_SHIFT));
final double d =
(array == null) ? Double.NaN : array[(int)(pos & MAX_ARRAY_MASK)];
final double d = array[(int)(pos & MAX_ARRAY_MASK)];
dst[dstPos + i] = Double.valueOf(d);
}
}
Expand Down Expand Up @@ -190,10 +184,6 @@ public void fromFlattenedObjs(final Double[] src,
final long pos = dstPos + i;
final int idx = (int)(pos >>> MAX_ARRAY_SHIFT);
double[] array = myElements.get(idx);
if (array == null) {
array = allocForIndex(idx);
myElements.set(idx, array);
}
final Double value = src[srcPos + i];
array[(int)(pos & MAX_ARRAY_MASK)] =
(value == null) ? Double.NaN : value.doubleValue();
Expand Down Expand Up @@ -240,42 +230,29 @@ public void toFlattened(final long srcPos,

case 1:
// Just the one element
dst[dstPos] =
(array == null) ? Double.NaN : array[(int)(srcPos & MAX_ARRAY_MASK)];
dst[dstPos] = array[(int)(srcPos & MAX_ARRAY_MASK)];
break;

default:
// Standard copy within the same sub-array
if (array != null) {
System.arraycopy(
array, (int)(srcPos & MAX_ARRAY_MASK),
dst, dstPos,
length
);
}
else {
Arrays.fill(dst, dstPos, dstPos + length, Double.NaN);
}
System.arraycopy(array, (int)(srcPos & MAX_ARRAY_MASK),
dst, dstPos,
length);
}
}
else {
// Split into two copies
final double[] startArray = myElements.get(startIdx);
final double[] endArray = myElements.get( endIdx);
if (startArray != null && endArray != null) {
final int startPos = (int)(srcPos & MAX_ARRAY_MASK);
final int startLength = length - (startArray.length - startPos);
final int endLength = length - startLength;
System.arraycopy(startArray, startPos,
dst, dstPos,
startLength);
System.arraycopy(endArray, 0,
dst, dstPos + startLength,
endLength);
}
else {
Arrays.fill(dst, dstPos, dstPos + length, Double.NaN);
}
final int startPos = (int)(srcPos & MAX_ARRAY_MASK);
final int startLength = length - (startArray.length - startPos);
final int endLength = length - startLength;
System.arraycopy(startArray, startPos,
dst, dstPos,
startLength);
System.arraycopy(endArray, 0,
dst, dstPos + startLength,
endLength);
}
}

Expand Down Expand Up @@ -314,10 +291,6 @@ public void fromFlattened(final double[] src,
if (startIdx == endIdx) {
// Get the array, creating if needbe
double[] array = myElements.get(startIdx);
if (array == null) {
array = allocForIndex(startIdx);
myElements.set(startIdx, array);
}

// And handle it
switch (length) {
Expand Down Expand Up @@ -358,14 +331,6 @@ public void fromFlattened(final double[] src,
// Split into two copies
double[] startArray = myElements.get(startIdx);
double[] endArray = myElements.get( endIdx);
if (startArray == null) {
startArray = allocForIndex(startIdx);
myElements.set(startIdx, startArray);
}
if (endArray == null) {
endArray = allocForIndex(endIdx);
myElements.set(endIdx, endArray);
}

// And do the copy
final int startPos = (int)(dstPos & MAX_ARRAY_MASK);
Expand Down Expand Up @@ -403,12 +368,7 @@ public void copyFrom(final DoubleArrayHypercube that)
if (myElements.length() == that.myElements.length()) {
for (int i=0; i < myElements.length(); i++) {
final double[] els = that.myElements.get(i);
if (els == null) {
myElements.set(i, null);
}
else {
myElements.set(i, Arrays.copyOf(els, els.length));
}
myElements.set(i, Arrays.copyOf(els, els.length));
}
}
else {
Expand Down Expand Up @@ -468,7 +428,7 @@ public double getAt(final long index)
{
preRead();
final double[] array = myElements.get((int)(index >>> MAX_ARRAY_SHIFT));
return (array == null) ? Double.NaN : array[(int)(index & MAX_ARRAY_MASK)];
return array[(int)(index & MAX_ARRAY_MASK)];
}

/**
Expand All @@ -478,12 +438,7 @@ public double getAt(final long index)
public void setAt(final long index, final double value)
throws IndexOutOfBoundsException
{
final int idx = (int)(index >>> MAX_ARRAY_SHIFT);
double[] array = myElements.get(idx);
if (array == null) {
array = allocForIndex(idx);
myElements.set(idx, array);
}
double[] array = myElements.get((int)(index >>> MAX_ARRAY_SHIFT));
array[(int)(index & MAX_ARRAY_MASK)] = value;
postWrite();
}
Expand Down Expand Up @@ -519,4 +474,4 @@ private double[] allocForIndex(final int index)
}
}

// [[[end]]] (checksum: 042be63cbafcc5a5e69e75a1b321ffce)
// [[[end]]] (checksum: b88a57b3feb95aaffba5dd3124475ddf)
83 changes: 19 additions & 64 deletions java/src/main/java/com/deshaw/hypercube/FloatArrayHypercube.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,7 @@ public FloatArrayHypercube(final Dimension<?>[] dimensions,
public void fill(final float v)
{
for (int i=0; i < myElements.length(); i++) {
float[] elements = myElements.get(i);
if (elements == null) {
elements = allocForIndex(i);
myElements.set(i, elements);
}
Arrays.fill(elements, v);
Arrays.fill(myElements.get(i), v);
}
}

Expand Down Expand Up @@ -146,8 +141,7 @@ public void toFlattenedObjs(final long srcPos,
for (int i=0; i < length; i++) {
final long pos = srcPos + i;
final float[] array = myElements.get((int)(pos >>> MAX_ARRAY_SHIFT));
final float d =
(array == null) ? Float.NaN : array[(int)(pos & MAX_ARRAY_MASK)];
final float d = array[(int)(pos & MAX_ARRAY_MASK)];
dst[dstPos + i] = Float.valueOf(d);
}
}
Expand Down Expand Up @@ -190,10 +184,6 @@ public void fromFlattenedObjs(final Float[] src,
final long pos = dstPos + i;
final int idx = (int)(pos >>> MAX_ARRAY_SHIFT);
float[] array = myElements.get(idx);
if (array == null) {
array = allocForIndex(idx);
myElements.set(idx, array);
}
final Float value = src[srcPos + i];
array[(int)(pos & MAX_ARRAY_MASK)] =
(value == null) ? Float.NaN : value.floatValue();
Expand Down Expand Up @@ -240,42 +230,29 @@ public void toFlattened(final long srcPos,

case 1:
// Just the one element
dst[dstPos] =
(array == null) ? Float.NaN : array[(int)(srcPos & MAX_ARRAY_MASK)];
dst[dstPos] = array[(int)(srcPos & MAX_ARRAY_MASK)];
break;

default:
// Standard copy within the same sub-array
if (array != null) {
System.arraycopy(
array, (int)(srcPos & MAX_ARRAY_MASK),
dst, dstPos,
length
);
}
else {
Arrays.fill(dst, dstPos, dstPos + length, Float.NaN);
}
System.arraycopy(array, (int)(srcPos & MAX_ARRAY_MASK),
dst, dstPos,
length);
}
}
else {
// Split into two copies
final float[] startArray = myElements.get(startIdx);
final float[] endArray = myElements.get( endIdx);
if (startArray != null && endArray != null) {
final int startPos = (int)(srcPos & MAX_ARRAY_MASK);
final int startLength = length - (startArray.length - startPos);
final int endLength = length - startLength;
System.arraycopy(startArray, startPos,
dst, dstPos,
startLength);
System.arraycopy(endArray, 0,
dst, dstPos + startLength,
endLength);
}
else {
Arrays.fill(dst, dstPos, dstPos + length, Float.NaN);
}
final int startPos = (int)(srcPos & MAX_ARRAY_MASK);
final int startLength = length - (startArray.length - startPos);
final int endLength = length - startLength;
System.arraycopy(startArray, startPos,
dst, dstPos,
startLength);
System.arraycopy(endArray, 0,
dst, dstPos + startLength,
endLength);
}
}

Expand Down Expand Up @@ -314,10 +291,6 @@ public void fromFlattened(final float[] src,
if (startIdx == endIdx) {
// Get the array, creating if needbe
float[] array = myElements.get(startIdx);
if (array == null) {
array = allocForIndex(startIdx);
myElements.set(startIdx, array);
}

// And handle it
switch (length) {
Expand Down Expand Up @@ -358,14 +331,6 @@ public void fromFlattened(final float[] src,
// Split into two copies
float[] startArray = myElements.get(startIdx);
float[] endArray = myElements.get( endIdx);
if (startArray == null) {
startArray = allocForIndex(startIdx);
myElements.set(startIdx, startArray);
}
if (endArray == null) {
endArray = allocForIndex(endIdx);
myElements.set(endIdx, endArray);
}

// And do the copy
final int startPos = (int)(dstPos & MAX_ARRAY_MASK);
Expand Down Expand Up @@ -403,12 +368,7 @@ public void copyFrom(final FloatArrayHypercube that)
if (myElements.length() == that.myElements.length()) {
for (int i=0; i < myElements.length(); i++) {
final float[] els = that.myElements.get(i);
if (els == null) {
myElements.set(i, null);
}
else {
myElements.set(i, Arrays.copyOf(els, els.length));
}
myElements.set(i, Arrays.copyOf(els, els.length));
}
}
else {
Expand Down Expand Up @@ -468,7 +428,7 @@ public float getAt(final long index)
{
preRead();
final float[] array = myElements.get((int)(index >>> MAX_ARRAY_SHIFT));
return (array == null) ? Float.NaN : array[(int)(index & MAX_ARRAY_MASK)];
return array[(int)(index & MAX_ARRAY_MASK)];
}

/**
Expand All @@ -478,12 +438,7 @@ public float getAt(final long index)
public void setAt(final long index, final float value)
throws IndexOutOfBoundsException
{
final int idx = (int)(index >>> MAX_ARRAY_SHIFT);
float[] array = myElements.get(idx);
if (array == null) {
array = allocForIndex(idx);
myElements.set(idx, array);
}
float[] array = myElements.get((int)(index >>> MAX_ARRAY_SHIFT));
array[(int)(index & MAX_ARRAY_MASK)] = value;
postWrite();
}
Expand Down Expand Up @@ -519,4 +474,4 @@ private float[] allocForIndex(final int index)
}
}

// [[[end]]] (checksum: cd3ee8e9023a952a48d3944ab5aedb16)
// [[[end]]] (checksum: e659210397168702c118c03d4d146103)
Loading

0 comments on commit 61f74b9

Please sign in to comment.