Skip to content

Commit

Permalink
Add getAndSet to some BigArray implementations and make set return vo…
Browse files Browse the repository at this point in the history
…id (#109878)

The majority of callsites for these things don't look at the return.
Computing the return from bytes to numeric values is not free due to
bounds checks etc.
=> this PR adds a set that doesn't read the previous value and where
needed moves the existing code to a new getAndSet call
  • Loading branch information
original-brownbear authored Jun 19, 2024
1 parent c900743 commit 5ef4e81
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public long id(long index) {
}

protected final long id(long index, long id) {
return ids.set(index, id + 1) - 1;
return ids.getAndSet(index, id + 1) - 1;
}

@Override
Expand Down
28 changes: 17 additions & 11 deletions server/src/main/java/org/elasticsearch/common/util/BigArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,9 @@ public byte get(long index) {
}

@Override
public byte set(long index, byte value) {
public void set(long index, byte value) {
assert indexIsInt(index);
final byte ret = array[(int) index];
array[(int) index] = value;
return ret;
}

@Override
Expand Down Expand Up @@ -215,13 +213,19 @@ public int get(long index) {
}

@Override
public int set(long index, int value) {
public int getAndSet(long index, int value) {
assert index >= 0 && index < size();
final int ret = (int) VH_PLATFORM_NATIVE_INT.get(array, (int) index << 2);
VH_PLATFORM_NATIVE_INT.set(array, (int) index << 2, value);
return ret;
}

@Override
public void set(long index, int value) {
assert index >= 0 && index < size();
VH_PLATFORM_NATIVE_INT.set(array, (int) index << 2, value);
}

@Override
public int increment(long index, int inc) {
assert index >= 0 && index < size();
Expand Down Expand Up @@ -272,13 +276,19 @@ public long get(long index) {
}

@Override
public long set(long index, long value) {
public long getAndSet(long index, long value) {
assert index >= 0 && index < size();
final long ret = (long) VH_PLATFORM_NATIVE_LONG.get(array, (int) index << 3);
VH_PLATFORM_NATIVE_LONG.set(array, (int) index << 3, value);
return ret;
}

@Override
public void set(long index, long value) {
assert index >= 0 && index < size();
VH_PLATFORM_NATIVE_LONG.set(array, (int) index << 3, value);
}

@Override
public long increment(long index, long inc) {
assert index >= 0 && index < size();
Expand Down Expand Up @@ -336,11 +346,9 @@ public double get(long index) {
}

@Override
public double set(long index, double value) {
public void set(long index, double value) {
assert index >= 0 && index < size();
final double ret = (double) VH_PLATFORM_NATIVE_DOUBLE.get(array, (int) index << 3);
VH_PLATFORM_NATIVE_DOUBLE.set(array, (int) index << 3, value);
return ret;
}

@Override
Expand Down Expand Up @@ -400,11 +408,9 @@ public float get(long index) {
}

@Override
public float set(long index, float value) {
public void set(long index, float value) {
assert index >= 0 && index < size();
final float ret = (float) VH_PLATFORM_NATIVE_FLOAT.get(array, (int) index << 2);
VH_PLATFORM_NATIVE_FLOAT.set(array, (int) index << 2, value);
return ret;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ public byte get(long index) {
}

@Override
public byte set(long index, byte value) {
public void set(long index, byte value) {
final int pageIndex = pageIndex(index);
final int indexInPage = indexInPage(index);
final byte[] page = getPageForWriting(pageIndex);
final byte ret = page[indexInPage];
page[indexInPage] = value;
return ret;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ public double get(long index) {
}

@Override
public double set(long index, double value) {
public void set(long index, double value) {
final int pageIndex = pageIndex(index);
final int indexInPage = indexInPage(index);
final byte[] page = getPageForWriting(pageIndex);
final double ret = (double) VH_PLATFORM_NATIVE_DOUBLE.get(page, indexInPage << 3);
VH_PLATFORM_NATIVE_DOUBLE.set(page, indexInPage << 3, value);
return ret;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ final class BigFloatArray extends AbstractBigByteArray implements FloatArray {
}

@Override
public float set(long index, float value) {
public void set(long index, float value) {
final int pageIndex = pageIndex(index);
final int indexInPage = indexInPage(index);
final byte[] page = getPageForWriting(pageIndex);
final float ret = (float) VH_PLATFORM_NATIVE_FLOAT.get(page, indexInPage << 2);
VH_PLATFORM_NATIVE_FLOAT.set(page, indexInPage << 2, value);
return ret;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public int get(long index) {
}

@Override
public int set(long index, int value) {
public int getAndSet(long index, int value) {
final int pageIndex = pageIndex(index);
final int indexInPage = indexInPage(index);
final byte[] page = getPageForWriting(pageIndex);
Expand All @@ -55,6 +55,13 @@ public int set(long index, int value) {
return ret;
}

@Override
public void set(long index, int value) {
final int pageIndex = pageIndex(index);
final int indexInPage = indexInPage(index);
VH_PLATFORM_NATIVE_INT.set(getPageForWriting(pageIndex), indexInPage << 2, value);
}

@Override
public int increment(long index, int inc) {
final int pageIndex = pageIndex(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public long get(long index) {
}

@Override
public long set(long index, long value) {
public long getAndSet(long index, long value) {
final int pageIndex = pageIndex(index);
final int indexInPage = indexInPage(index);
final byte[] page = getPageForWriting(pageIndex);
Expand All @@ -50,6 +50,14 @@ public long set(long index, long value) {
return ret;
}

@Override
public void set(long index, long value) {
final int pageIndex = pageIndex(index);
final int indexInPage = indexInPage(index);
final byte[] page = getPageForWriting(pageIndex);
VH_PLATFORM_NATIVE_LONG.set(page, indexInPage << 3, value);
}

@Override
public long increment(long index, long inc) {
final int pageIndex = pageIndex(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ static ByteArray readFrom(StreamInput in) throws IOException {
byte get(long index);

/**
* Set a value at the given index and return the previous value.
* Set a value at the given index.
*/
byte set(long index, byte value);
void set(long index, byte value);

/**
* Get a reference to a slice.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ static DoubleArray readFrom(StreamInput in) throws IOException {
double get(long index);

/**
* Set a value at the given index and return the previous value.
* Set a value at the given index.
*/
double set(long index, double value);
void set(long index, double value);

/**
* Increment value at the given index by <code>inc</code> and return the value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public interface FloatArray extends BigArray {
float get(long index);

/**
* Set a value at the given index and return the previous value.
* Set a value at the given index.
*/
float set(long index, float value);
void set(long index, float value);

/**
* Fill slots between <code>fromIndex</code> inclusive to <code>toIndex</code> exclusive with <code>value</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ protected void removeAndAdd(long index) {
final long id = id(index, -1);
assert id >= 0;
long keyOffset = id * 3;
final int key1 = keys.set(keyOffset, 0);
final int key2 = keys.set(keyOffset + 1, 0);
final int key3 = keys.set(keyOffset + 2, 0);
final int key1 = keys.getAndSet(keyOffset, 0);
final int key2 = keys.getAndSet(keyOffset + 1, 0);
final int key3 = keys.getAndSet(keyOffset + 2, 0);
reset(key1, key2, key3, id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ static IntArray readFrom(StreamInput in) throws IOException {
/**
* Set a value at the given index and return the previous value.
*/
int set(long index, int value);
int getAndSet(long index, int value);

/**
* Set a value at the given index
*/
void set(long index, int value);

/**
* Increment value at the given index by <code>inc</code> and return the value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ static LongArray readFrom(StreamInput in) throws IOException {
/**
* Set a value at the given index and return the previous value.
*/
long set(long index, long value);
long getAndSet(long index, long value);

/**
* Set a value at the given index.
*/
void set(long index, long value);

/**
* Increment value at the given index by <code>inc</code> and return the value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public long add(long key) {
protected void removeAndAdd(long index) {
final long id = id(index, -1);
assert id >= 0;
final long key = keys.set(id, 0);
final long key = keys.getAndSet(id, 0);
reset(key, id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ protected void removeAndAdd(long index) {
final long id = id(index, -1);
assert id >= 0;
long keyOffset = id * 2;
final long key1 = keys.set(keyOffset, 0);
final long key2 = keys.set(keyOffset + 1, 0);
final long key1 = keys.getAndSet(keyOffset, 0);
final long key2 = keys.getAndSet(keyOffset + 1, 0);
reset(key1, key2, id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public boolean get(long index, int len, BytesRef ref) {
}

@Override
public byte set(long index, byte value) {
public void set(long index, byte value) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public double get(long index) {
}

@Override
public double set(long index, double value) {
public void set(long index, double value) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ public int get(long index) {
}

@Override
public int set(long index, int value) {
public int getAndSet(long index, int value) {
throw new UnsupportedOperationException();
}

@Override
public void set(long index, int value) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ public long get(long index) {
}

@Override
public long set(long index, long value) {
public long getAndSet(long index, long value) {
throw new UnsupportedOperationException();
}

@Override
public void set(long index, long value) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ private void mapSegmentCountsToGlobalCounts(LongUnaryOperator mapping) throws IO
for (long i = 1; i < segmentDocCounts.size(); i++) {
// We use set(...) here, because we need to reset the slow to 0.
// segmentDocCounts get reused over the segments and otherwise counts would be too high.
long inc = segmentDocCounts.set(i, 0);
long inc = segmentDocCounts.getAndSet(i, 0);
if (inc == 0) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ public byte get(long index) {
}

@Override
public byte set(long index, byte value) {
return in.set(index, value);
public void set(long index, byte value) {
in.set(index, value);
}

@Override
Expand Down Expand Up @@ -469,8 +469,13 @@ public int get(long index) {
}

@Override
public int set(long index, int value) {
return in.set(index, value);
public int getAndSet(long index, int value) {
return in.getAndSet(index, value);
}

@Override
public void set(long index, int value) {
in.set(index, value);
}

@Override
Expand Down Expand Up @@ -524,8 +529,13 @@ public long get(long index) {
}

@Override
public long set(long index, long value) {
return in.set(index, value);
public long getAndSet(long index, long value) {
return in.getAndSet(index, value);
}

@Override
public void set(long index, long value) {
in.set(index, value);
}

@Override
Expand Down Expand Up @@ -584,8 +594,8 @@ public float get(long index) {
}

@Override
public float set(long index, float value) {
return in.set(index, value);
public void set(long index, float value) {
in.set(index, value);
}

@Override
Expand Down Expand Up @@ -629,8 +639,8 @@ public double get(long index) {
}

@Override
public double set(long index, double value) {
return in.set(index, value);
public void set(long index, double value) {
in.set(index, value);
}

@Override
Expand Down

0 comments on commit 5ef4e81

Please sign in to comment.