Skip to content

Commit

Permalink
Revert "perf: decrease lambda creation on the hot path during tuple u…
Browse files Browse the repository at this point in the history
…pdate (#1124)"

This reverts commit 9ec757a.
  • Loading branch information
triceo committed Oct 2, 2024
1 parent 9ec757a commit 459bfb6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public final void updateLeft(LeftTuple_ leftTuple) {
if (oldIndexProperties.equals(newIndexProperties)) {
// No need for re-indexing because the index properties didn't change
// Prefer an update over retract-insert if possible
indexerRight.forEach(oldIndexProperties,
rightTuple -> rightTupleUpdater.accept(leftTuple, rightTuple));
innerUpdateLeft(leftTuple, consumer -> indexerRight.forEach(oldIndexProperties, consumer));
} else {
ElementAwareListEntry<LeftTuple_> leftEntry = leftTuple.getStore(inputStoreIndexLeftEntry);
ElementAwareList<OutTuple_> outTupleListLeft = leftTuple.getStore(inputStoreIndexLeftOutTupleList);
Expand All @@ -92,7 +91,7 @@ private void indexAndPropagateLeft(LeftTuple_ leftTuple, IndexProperties indexPr
leftTuple.setStore(inputStoreIndexLeftProperties, indexProperties);
ElementAwareListEntry<LeftTuple_> leftEntry = indexerLeft.put(indexProperties, leftTuple);
leftTuple.setStore(inputStoreIndexLeftEntry, leftEntry);
indexerRight.forEach(indexProperties, rightTuple -> outTupleInserter.accept(leftTuple, rightTuple));
indexerRight.forEach(indexProperties, rightTuple -> insertOutTupleFiltered(leftTuple, rightTuple));
}

@Override
Expand Down Expand Up @@ -133,8 +132,7 @@ public final void updateRight(UniTuple<Right_> rightTuple) {
if (oldIndexProperties.equals(newIndexProperties)) {
// No need for re-indexing because the index properties didn't change
// Prefer an update over retract-insert if possible
indexerLeft.forEach(oldIndexProperties,
leftTuple -> leftTupleUpdater.accept(leftTuple, rightTuple));
innerUpdateRight(rightTuple, consumer -> indexerLeft.forEach(oldIndexProperties, consumer));
} else {
ElementAwareListEntry<UniTuple<Right_>> rightEntry = rightTuple.getStore(inputStoreIndexRightEntry);
ElementAwareList<OutTuple_> outTupleListRight = rightTuple.getStore(inputStoreIndexRightOutTupleList);
Expand All @@ -150,7 +148,7 @@ private void indexAndPropagateRight(UniTuple<Right_> rightTuple, IndexProperties
rightTuple.setStore(inputStoreIndexRightProperties, indexProperties);
ElementAwareListEntry<UniTuple<Right_>> rightEntry = indexerRight.put(indexProperties, rightTuple);
rightTuple.setStore(inputStoreIndexRightEntry, rightEntry);
indexerLeft.forEach(indexProperties, leftTuple -> outTupleInserter.accept(leftTuple, rightTuple));
indexerLeft.forEach(indexProperties, leftTuple -> insertOutTupleFiltered(leftTuple, rightTuple));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ai.timefold.solver.core.impl.score.stream.bavet.common;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

import ai.timefold.solver.core.impl.score.stream.bavet.common.tuple.AbstractTuple;
import ai.timefold.solver.core.impl.score.stream.bavet.common.tuple.LeftTupleLifecycle;
Expand All @@ -26,25 +26,19 @@ public abstract class AbstractJoinNode<LeftTuple_ extends AbstractTuple, Right_,

protected final int inputStoreIndexLeftOutTupleList;
protected final int inputStoreIndexRightOutTupleList;
private final boolean isFiltering;
private final int outputStoreIndexLeftOutEntry;
private final int outputStoreIndexRightOutEntry;

protected final TupleConsumer<LeftTuple_, Right_> leftTupleUpdater;
protected final TupleConsumer<LeftTuple_, Right_> rightTupleUpdater;
protected final TupleConsumer<LeftTuple_, Right_> outTupleInserter;
private final StaticPropagationQueue<OutTuple_> propagationQueue;

protected AbstractJoinNode(int inputStoreIndexLeftOutTupleList, int inputStoreIndexRightOutTupleList,
TupleLifecycle<OutTuple_> nextNodesTupleLifecycle, boolean isFiltering,
int outputStoreIndexLeftOutEntry, int outputStoreIndexRightOutEntry) {
this.inputStoreIndexLeftOutTupleList = inputStoreIndexLeftOutTupleList;
this.inputStoreIndexRightOutTupleList = inputStoreIndexRightOutTupleList;
this.isFiltering = isFiltering;
this.outputStoreIndexLeftOutEntry = outputStoreIndexLeftOutEntry;
this.outputStoreIndexRightOutEntry = outputStoreIndexRightOutEntry;

this.leftTupleUpdater = isFiltering ? this::updateLeftTupleWithFiltering : this::updateLeftTupleWithoutFiltering;
this.rightTupleUpdater = isFiltering ? this::updateRightTupleWithFiltering : this::updateRightTupleWithoutFiltering;
this.outTupleInserter = isFiltering ? this::insertOutTupleWithFiltering : this::insertOutTupleWithoutFiltering;
this.propagationQueue = new StaticPropagationQueue<>(nextNodesTupleLifecycle);
}

Expand All @@ -56,13 +50,7 @@ protected AbstractJoinNode(int inputStoreIndexLeftOutTupleList, int inputStoreIn

protected abstract boolean testFiltering(LeftTuple_ leftTuple, UniTuple<Right_> rightTuple);

private void insertOutTupleWithFiltering(LeftTuple_ leftTuple, UniTuple<Right_> rightTuple) {
if (testFiltering(leftTuple, rightTuple)) {
insertOutTupleWithoutFiltering(leftTuple, rightTuple);
}
}

private void insertOutTupleWithoutFiltering(LeftTuple_ leftTuple, UniTuple<Right_> rightTuple) {
protected final void insertOutTuple(LeftTuple_ leftTuple, UniTuple<Right_> rightTuple) {
var outTuple = createOutTuple(leftTuple, rightTuple);
ElementAwareList<OutTuple_> outTupleListLeft = leftTuple.getStore(inputStoreIndexLeftOutTupleList);
var outEntryLeft = outTupleListLeft.add(outTuple);
Expand All @@ -73,6 +61,28 @@ private void insertOutTupleWithoutFiltering(LeftTuple_ leftTuple, UniTuple<Right
propagationQueue.insert(outTuple);
}

protected final void insertOutTupleFiltered(LeftTuple_ leftTuple, UniTuple<Right_> rightTuple) {
if (!isFiltering || testFiltering(leftTuple, rightTuple)) {
insertOutTuple(leftTuple, rightTuple);
}
}

protected final void innerUpdateLeft(LeftTuple_ leftTuple, Consumer<Consumer<UniTuple<Right_>>> rightTupleConsumer) {
// Prefer an update over retract-insert if possible
ElementAwareList<OutTuple_> outTupleListLeft = leftTuple.getStore(inputStoreIndexLeftOutTupleList);
// Propagate the update for downstream filters, matchWeighers, ...
if (!isFiltering) {
for (var outTuple : outTupleListLeft) {
updateOutTupleLeft(outTuple, leftTuple);
}
} else {
rightTupleConsumer.accept(rightTuple -> {
ElementAwareList<OutTuple_> rightOutList = rightTuple.getStore(inputStoreIndexRightOutTupleList);
processOutTupleUpdate(leftTuple, rightTuple, rightOutList, outTupleListLeft, outputStoreIndexRightOutEntry);
});
}
}

private void updateOutTupleLeft(OutTuple_ outTuple, LeftTuple_ leftTuple) {
setOutTupleLeftFacts(outTuple, leftTuple);
doUpdateOutTuple(outTuple);
Expand All @@ -89,39 +99,29 @@ private void doUpdateOutTuple(OutTuple_ outTuple) {
propagationQueue.update(outTuple);
}

private void updateLeftTupleWithoutFiltering(LeftTuple_ leftTuple, UniTuple<Right_> rightTuple) {
ElementAwareList<OutTuple_> outTupleListLeft = leftTuple.getStore(inputStoreIndexLeftOutTupleList);
for (var outTuple : outTupleListLeft) {
updateOutTupleLeft(outTuple, leftTuple);
}
}

private void updateRightTupleWithoutFiltering(LeftTuple_ leftTuple, UniTuple<Right_> rightTuple) {
protected final void innerUpdateRight(UniTuple<Right_> rightTuple, Consumer<Consumer<LeftTuple_>> leftTupleConsumer) {
// Prefer an update over retract-insert if possible
ElementAwareList<OutTuple_> outTupleListRight = rightTuple.getStore(inputStoreIndexRightOutTupleList);
for (var outTuple : outTupleListRight) {
setOutTupleRightFact(outTuple, rightTuple);
doUpdateOutTuple(outTuple);
if (!isFiltering) {
// Propagate the update for downstream filters, matchWeighers, ...
for (var outTuple : outTupleListRight) {
setOutTupleRightFact(outTuple, rightTuple);
doUpdateOutTuple(outTuple);
}
} else {
leftTupleConsumer.accept(leftTuple -> {
ElementAwareList<OutTuple_> leftOutList = leftTuple.getStore(inputStoreIndexLeftOutTupleList);
processOutTupleUpdate(leftTuple, rightTuple, leftOutList, outTupleListRight, outputStoreIndexLeftOutEntry);
});
}
}

private void updateLeftTupleWithFiltering(LeftTuple_ leftTuple, UniTuple<Right_> rightTuple) {
ElementAwareList<OutTuple_> outTupleListLeft = leftTuple.getStore(inputStoreIndexLeftOutTupleList);
ElementAwareList<OutTuple_> rightOutList = rightTuple.getStore(inputStoreIndexRightOutTupleList);
processOutTupleUpdate(leftTuple, rightTuple, rightOutList, outTupleListLeft, outputStoreIndexRightOutEntry);
}

private void updateRightTupleWithFiltering(LeftTuple_ leftTuple, UniTuple<Right_> rightTuple) {
ElementAwareList<OutTuple_> outTupleListRight = rightTuple.getStore(inputStoreIndexRightOutTupleList);
ElementAwareList<OutTuple_> leftOutList = leftTuple.getStore(inputStoreIndexLeftOutTupleList);
processOutTupleUpdate(leftTuple, rightTuple, leftOutList, outTupleListRight, outputStoreIndexLeftOutEntry);
}

private void processOutTupleUpdate(LeftTuple_ leftTuple, UniTuple<Right_> rightTuple, ElementAwareList<OutTuple_> outList,
ElementAwareList<OutTuple_> outTupleList, int outputStoreIndexOutEntry) {
var outTuple = findOutTuple(outTupleList, outList, outputStoreIndexOutEntry);
if (testFiltering(leftTuple, rightTuple)) {
if (outTuple == null) {
insertOutTupleWithFiltering(leftTuple, rightTuple);
insertOutTuple(leftTuple, rightTuple);
} else {
updateOutTupleLeft(outTuple, leftTuple);
}
Expand Down Expand Up @@ -168,9 +168,4 @@ public Propagator getPropagator() {
return propagationQueue;
}

@FunctionalInterface
public interface TupleConsumer<LeftTuple_ extends AbstractTuple, Right_> extends BiConsumer<LeftTuple_, UniTuple<Right_>> {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final void insertLeft(LeftTuple_ leftTuple) {
ElementAwareList<OutTuple_> outTupleListLeft = new ElementAwareList<>();
leftTuple.setStore(inputStoreIndexLeftOutTupleList, outTupleListLeft);
for (UniTuple<Right_> tuple : rightTupleList) {
outTupleInserter.accept(leftTuple, tuple);
insertOutTupleFiltered(leftTuple, tuple);
}
}

Expand All @@ -57,7 +57,7 @@ public final void updateLeft(LeftTuple_ leftTuple) {
insertLeft(leftTuple);
return;
}
rightTupleList.forEach(rightTuple -> leftTupleUpdater.accept(leftTuple, rightTuple));
innerUpdateLeft(leftTuple, rightTupleList::forEach);
}

@Override
Expand All @@ -83,7 +83,7 @@ public final void insertRight(UniTuple<Right_> rightTuple) {
ElementAwareList<OutTuple_> outTupleListRight = new ElementAwareList<>();
rightTuple.setStore(inputStoreIndexRightOutTupleList, outTupleListRight);
for (LeftTuple_ tuple : leftTupleList) {
outTupleInserter.accept(tuple, rightTuple);
insertOutTupleFiltered(tuple, rightTuple);
}
}

Expand All @@ -95,7 +95,7 @@ public final void updateRight(UniTuple<Right_> rightTuple) {
insertRight(rightTuple);
return;
}
leftTupleList.forEach(leftTuple -> rightTupleUpdater.accept(leftTuple, rightTuple));
innerUpdateRight(rightTuple, leftTupleList::forEach);
}

@Override
Expand Down

0 comments on commit 459bfb6

Please sign in to comment.