Skip to content

Commit

Permalink
perf: improve tuple store performance (#1329)
Browse files Browse the repository at this point in the history
We were being too clever and the clever approach doesn't seem justified.
This improves performance in the benchmarks by 3 to 6 percent.
  • Loading branch information
triceo authored Jan 16, 2025
1 parent 39b7ab6 commit 3bf5d40
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private void updateOutTupleLeft(OutTuple_ outTuple, LeftTuple_ leftTuple) {
private void doUpdateOutTuple(OutTuple_ outTuple) {
var state = outTuple.state;
if (!state.isActive()) { // Impossible because they shouldn't linger in the indexes.
throw new IllegalStateException("Impossible state: The tuple (" + outTuple.state + ") in node (" +
this + ") is in an unexpected state (" + outTuple.state + ").");
throw new IllegalStateException("Impossible state: The tuple (%s) in node (%s) is in an unexpected state (%s)."
.formatted(outTuple, this, outTuple.state));
} else if (state != TupleState.OK) { // Already in the queue in the correct state.
return;
}
Expand Down Expand Up @@ -155,10 +155,9 @@ protected final void retractOutTuple(OutTuple_ outTuple) {
ElementAwareListEntry<OutTuple_> outEntryRight = outTuple.removeStore(outputStoreIndexRightOutEntry);
outEntryRight.remove();
var state = outTuple.state;
if (!state.isActive()) {
// Impossible because they shouldn't linger in the indexes.
throw new IllegalStateException("Impossible state: The tuple (" + outTuple.state + ") in node (" + this
+ ") is in an unexpected state (" + outTuple.state + ").");
if (!state.isActive()) { // Impossible because they shouldn't linger in the indexes.
throw new IllegalStateException("Impossible state: The tuple (%s) in node (%s) is in an unexpected state (%s)."
.formatted(outTuple, this, outTuple.state));
}
propagationQueue.retract(outTuple, state == TupleState.CREATING ? TupleState.ABORTING : TupleState.DYING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,26 @@
*/
public abstract sealed class AbstractTuple permits UniTuple, BiTuple, TriTuple, QuadTuple {

/*
* We create a lot of tuples, many of them having store size of 1.
* If an array of size 1 was created for each such tuple, memory would be wasted and indirection created.
* This trade-off of increased memory efficiency for marginally slower access time is proven beneficial.
*/
private final boolean storeIsArray;

private Object store;
private static final Object[] EMPTY_STORE = new Object[0];

private final Object[] store;
public TupleState state = TupleState.DEAD; // It's the node's job to mark a new tuple as CREATING.

protected AbstractTuple(int storeSize) {
this.store = (storeSize < 2) ? null : new Object[storeSize];
this.storeIsArray = store != null;
this.store = storeSize == 0 ? EMPTY_STORE : new Object[storeSize];
}

public final <Value_> Value_ getStore(int index) {
return (Value_) (storeIsArray ? ((Object[]) store)[index] : store);
return (Value_) store[index];
}

public final void setStore(int index, Object value) {
if (storeIsArray) {
((Object[]) store)[index] = value;
} else {
store = value;
}
store[index] = value;
}

public <Value_> Value_ removeStore(int index) {
Value_ value;
if (storeIsArray) {
Object[] array = (Object[]) store;
value = (Value_) array[index];
array[index] = null;
} else {
value = (Value_) store;
store = null;
}
public final <Value_> Value_ removeStore(int index) {
Value_ value = getStore(index);
setStore(index, null);
return value;
}

Expand Down

0 comments on commit 3bf5d40

Please sign in to comment.