Skip to content

Commit b2760e9

Browse files
committed
perf: further improve performance of precompute
1 parent f8b9aa8 commit b2760e9

File tree

15 files changed

+40
-59
lines changed

15 files changed

+40
-59
lines changed

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/RecordAndReplayPropagator.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public final class RecordAndReplayPropagator<Tuple_ extends AbstractTuple>
4242
private final Supplier<BavetPrecomputeBuildHelper<Tuple_>> precomputeBuildHelperSupplier;
4343
private final UnaryOperator<Tuple_> internalTupleToOutputTupleMapper;
4444
private final Map<Object, List<Tuple_>> objectToOutputTuplesMap;
45+
private final Set<Object> alreadyUpdatingMap = Collections.newSetFromMap(new IdentityHashMap<>());
4546
private final Map<Class<?>, Boolean> objectClassToIsEntitySourceClass;
4647

4748
private final StaticPropagationQueue<Tuple_> propagationQueue;
@@ -77,14 +78,18 @@ public void insert(Object object) {
7778
}
7879

7980
public void update(Object object) {
81+
if (!alreadyUpdatingMap.add(object)) {
82+
// The list was already sent to the propagation queue.
83+
// Don't iterate over it again, even though the queue would deduplicate its contents.
84+
return;
85+
}
8086
// Updates happen very frequently, so we optimize by avoiding the update queue
8187
// and going straight to the propagation queue.
8288
// The propagation queue deduplicates updates internally.
8389
var outTupleList = objectToOutputTuplesMap.get(object);
84-
if (outTupleList == null) {
85-
return;
90+
if (outTupleList != null) {
91+
outTupleList.forEach(propagationQueue::update);
8692
}
87-
outTupleList.forEach(propagationQueue::update);
8893
}
8994

9095
public void retract(Object object) {
@@ -158,6 +163,7 @@ private static <A> List<BavetRootNode<A>> getRootNodes(Object object, NodeNetwor
158163
@Override
159164
public void propagateUpdates() {
160165
propagationQueue.propagateUpdates();
166+
alreadyUpdatingMap.clear();
161167
}
162168

163169
@Override
@@ -207,7 +213,7 @@ private void recalculateTuples(NodeNetwork internalNodeNetwork,
207213
internalNodeNetwork.settle();
208214
}
209215
if (mappedTuples.isEmpty()) {
210-
objectToOutputTuplesMap.put(invalidated, Collections.emptyList());
216+
objectToOutputTuplesMap.remove(invalidated);
211217
} else {
212218
objectToOutputTuplesMap.put(invalidated, mappedTuples);
213219
}

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/index/BiCompositeKey.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public <Key_> Key_ get(int id) {
2727

2828
@Override
2929
public int hashCode() {
30-
var hash = 7;
31-
hash = 31 * hash + Objects.hashCode(propertyA);
32-
hash = 31 * hash + Objects.hashCode(propertyB);
33-
return hash;
30+
return 31 * Objects.hashCode(propertyA) + Objects.hashCode(propertyB);
3431
}
3532

3633
}

core/src/main/java/ai/timefold/solver/core/impl/domain/valuerange/buildin/bigdecimal/BigDecimalValueRange.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,9 @@ public boolean equals(Object o) {
158158

159159
@Override
160160
public int hashCode() {
161-
var hash = 7;
162-
hash = 31 * hash + Objects.hashCode(from);
161+
var hash = Objects.hashCode(from);
163162
hash = 31 * hash + Objects.hashCode(to);
164-
hash = 31 * hash + Objects.hashCode(incrementUnit);
165-
return hash;
163+
return 31 * hash + Objects.hashCode(incrementUnit);
166164
}
167165

168166
@Override

core/src/main/java/ai/timefold/solver/core/impl/domain/valuerange/buildin/biginteger/BigIntegerValueRange.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,9 @@ public boolean equals(Object o) {
141141

142142
@Override
143143
public int hashCode() {
144-
var hash = 7;
145-
hash = 31 * hash + Objects.hashCode(from);
144+
var hash = Objects.hashCode(from);
146145
hash = 31 * hash + Objects.hashCode(to);
147-
hash = 31 * hash + Objects.hashCode(incrementUnit);
148-
return hash;
146+
return 31 * hash + Objects.hashCode(incrementUnit);
149147
}
150148

151149
@Override

core/src/main/java/ai/timefold/solver/core/impl/domain/valuerange/buildin/collection/ListValueRange.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ public boolean equals(Object o) {
8888

8989
@Override
9090
public int hashCode() {
91-
var hash = 7;
92-
hash = 31 * hash + Boolean.hashCode(isValueImmutable);
93-
hash = 31 * hash + Objects.hashCode(list);
94-
return hash;
91+
return 31 * Boolean.hashCode(isValueImmutable) + Objects.hashCode(list);
9592
}
9693

9794
@Override

core/src/main/java/ai/timefold/solver/core/impl/domain/valuerange/buildin/collection/SetValueRange.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ public boolean equals(Object o) {
8989

9090
@Override
9191
public int hashCode() {
92-
var hash = 7;
93-
hash = 31 * hash + Boolean.hashCode(isValueImmutable);
94-
hash = 31 * hash + Objects.hashCode(set);
95-
return hash;
92+
return 31 * Boolean.hashCode(isValueImmutable) + Objects.hashCode(set);
9693
}
9794

9895
@Override

core/src/main/java/ai/timefold/solver/core/impl/domain/valuerange/buildin/composite/CompositeCountableValueRange.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ public boolean equals(Object o) {
102102

103103
@Override
104104
public int hashCode() {
105-
var hash = 7;
106-
hash = 31 * hash + Boolean.hashCode(isValueImmutable);
107-
hash = 31 * hash + Objects.hashCode(valueRangeList);
108-
return hash;
105+
return 31 * Boolean.hashCode(isValueImmutable) + Objects.hashCode(valueRangeList);
109106
}
110107
}

core/src/main/java/ai/timefold/solver/core/impl/domain/valuerange/buildin/primdouble/DoubleValueRange.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ public boolean equals(Object o) {
9797

9898
@Override
9999
public int hashCode() {
100-
var hash = 7;
101-
hash = 31 * hash + Double.hashCode(from);
102-
hash = 31 * hash + Double.hashCode(to);
103-
return hash;
100+
return 31 * Double.hashCode(from) + Double.hashCode(to);
104101
}
105102

106103
@Override

core/src/main/java/ai/timefold/solver/core/impl/domain/valuerange/buildin/primint/IntValueRange.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,9 @@ public boolean equals(Object o) {
141141

142142
@Override
143143
public int hashCode() {
144-
var hash = 7;
145-
hash = 31 * hash + Integer.hashCode(from);
144+
var hash = Integer.hashCode(from);
146145
hash = 31 * hash + Integer.hashCode(to);
147-
hash = 31 * hash + Integer.hashCode(incrementUnit);
148-
return hash;
146+
return 31 * hash + Integer.hashCode(incrementUnit);
149147
}
150148

151149
@Override

core/src/main/java/ai/timefold/solver/core/impl/domain/valuerange/buildin/primlong/LongValueRange.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ public boolean equals(Object o) {
146146

147147
@Override
148148
public int hashCode() {
149-
var hash = 7;
150-
hash = 31 * hash + Long.hashCode(from);
149+
var hash = Long.hashCode(from);
151150
hash = 31 * hash + Long.hashCode(to);
152-
hash = 31 * hash + Long.hashCode(incrementUnit);
153-
return hash;
151+
return 31 * hash + Long.hashCode(incrementUnit);
154152
}
155153

156154
@Override

0 commit comments

Comments
 (0)