-
Notifications
You must be signed in to change notification settings - Fork 211
perf: simplify how value ranges are built #2425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+67
−36
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package ai.timefold.solver.core.impl.score.director; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.BitSet; | ||
| import java.util.HashMap; | ||
| import java.util.IdentityHashMap; | ||
|
|
@@ -35,7 +36,6 @@ final class ValueRangeState<Solution_, Entity_, Value_> { | |
|
|
||
| // Solution related fields | ||
| private @Nullable ValueRangeItem<Solution_, Entity_, ValueRange<Value_>, Value_> fromSolutionItem; | ||
| private @Nullable Map<Value_, Integer> fromSolutionValueIndexMap; | ||
|
|
||
| // Entity related fields | ||
| private @Nullable Map<Entity_, ValueRangeItem<Solution_, Entity_, ValueRange<Value_>, Value_>> fromEntityMap; | ||
|
|
@@ -54,7 +54,6 @@ public ValueRange<Value_> getFromSolution(Solution_ solution, @Nullable Selectio | |
| if (fromSolutionItem == null) { | ||
| var valueRange = fetchValueRangeFromSolution(solution, sorter); | ||
| fromSolutionItem = ValueRangeItem.ofLeft(null, valueRange, sorter); | ||
| fromSolutionValueIndexMap = buildIndexMap(valueRange.createOriginalIterator(), (int) valueRange.getSize()); | ||
| return valueRange; | ||
| } | ||
| var valueRange = pickValueBySorter(fromSolutionItem, sorter, null); | ||
|
|
@@ -66,9 +65,6 @@ public ValueRange<Value_> getFromSolution(Solution_ solution, @Nullable Selectio | |
| var sortedValueRange = sortValueRange(Objects.requireNonNull(fromSolutionItem.leftItem()), sorter); | ||
| fromSolutionItem = ValueRangeItem.of(null, sortedValueRange, sorter, fromSolutionItem.rightItem(), | ||
| fromSolutionItem.rightSorter()); | ||
| // We need to update the index map or the positions may become inconsistent | ||
| fromSolutionValueIndexMap = | ||
| buildIndexMap(sortedValueRange.createOriginalIterator(), (int) sortedValueRange.getSize()); | ||
| return sortedValueRange; | ||
| } else if (fromSolutionItem.rightItem() == null) { | ||
| var sortedValueRange = sortValueRange(Objects.requireNonNull(fromSolutionItem.leftItem()), sorter); | ||
|
|
@@ -103,14 +99,6 @@ public ValueRange<Value_> getFromSolution(Solution_ solution, @Nullable Selectio | |
| return null; | ||
| } | ||
|
|
||
| private Map<Value_, Integer> getIndexMapFromSolution() { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... this method. |
||
| if (fromSolutionValueIndexMap == null) { | ||
| // We call getFromSolution to ensure the solution-range is loaded and the related index map is created | ||
| getFromSolution(cachedWorkingSolution, null); | ||
| } | ||
| return fromSolutionValueIndexMap; | ||
| } | ||
|
|
||
| private ValueRange<Value_> fetchValueRangeFromSolution(Solution_ solution, | ||
| @Nullable SelectionSorter<Solution_, Value_> sorter) { | ||
| var valueRange = extractValueRange(solution); | ||
|
|
@@ -153,7 +141,7 @@ public ValueRange<Value_> getFromEntity(Entity_ entity, | |
| } | ||
| return Objects.requireNonNull(newItem.leftItem()); | ||
| } | ||
| var valueRange = pickValueBySorter(item, sorter, (p, s) -> getFromEntity(p, s)); | ||
| var valueRange = pickValueBySorter(item, sorter, this::getFromEntity); | ||
| if (valueRange != null) { | ||
| return valueRange; | ||
| } | ||
|
|
@@ -282,10 +270,14 @@ private ReachableValues<Entity_, Value_> fetchReachableValues(GenuineVariableDes | |
| var expectedTypeOfValue = valueRangeDescriptor.getVariableDescriptor().getVariableMetaModel().type(); | ||
| var reachableValueList = initReachableValueList(valueList, entityList.size()); | ||
| var valueIndexItem = new ReachableValuesIndex<>(valueIndexMap, reachableValueList); | ||
| var entityIndicesByRange = new IdentityHashMap<ValueRange<Value_>, List<Integer>>(); | ||
| for (var i = 0; i < entityList.size(); i++) { | ||
| var entity = entityList.get(i); | ||
| var valueRange = getFromEntity(entity, null); | ||
| loadEntityValueRange(i, valueIndexMap, valueRange, reachableValueList); | ||
| entityIndicesByRange.computeIfAbsent(valueRange, k -> new ArrayList<>()).add(i); | ||
| } | ||
| for (var entry : entityIndicesByRange.entrySet()) { | ||
| loadEntityValueRange(entry.getValue(), valueIndexMap, entry.getKey(), reachableValueList); | ||
| } | ||
| var sorterAdapter = sorter != null ? SelectionSorterAdapter.of(cachedWorkingSolution, sorter) : null; | ||
| return new ReachableValues<>(entityIndexItem, valueIndexItem, expectedTypeOfValue, sorterAdapter, | ||
|
|
@@ -308,31 +300,34 @@ private static <Type_> Map<Type_, Integer> buildIndexMap(Iterator<@Nullable Type | |
| private List<ReachableItemValue<Entity_, Value_>> initReachableValueList(ValueRange<Value_> valueRange, | ||
| int entityListSize) { | ||
| var valuesSize = (int) valueRange.getSize(); | ||
| Iterator<@Nullable Value_> iterator = valueRange.createOriginalIterator(); | ||
| var iterator = valueRange.createOriginalIterator(); | ||
| var spliterator = Spliterators.spliterator(iterator, valuesSize, Spliterator.ORDERED | Spliterator.IMMUTABLE); | ||
| var idx = new MutableInt(-1); | ||
| return StreamSupport.stream(spliterator, false).filter(Objects::nonNull) | ||
| .map(v -> new ReachableItemValue<Entity_, Value_>(idx.increment(), v, entityListSize, valuesSize)).toList(); | ||
| } | ||
|
|
||
| private static <Entity_, Value_> void loadEntityValueRange(int entityIndex, Map<Value_, Integer> valueIndexMap, | ||
| private static <Entity_, Value_> void loadEntityValueRange(List<Integer> entityIndices, Map<Value_, Integer> valueIndexMap, | ||
| ValueRange<Value_> valueRange, List<ReachableItemValue<Entity_, Value_>> reachableValueList) { | ||
| // We create a bitset containing all possible values from the range to optimize operations | ||
| // Build bitset once per distinct range to avoid redundant work for entities sharing a range. | ||
| var allValuesBitSet = buildBitSetForValueRange(valueRange, valueIndexMap); | ||
| // The second pass need only to iterate over the bits we already set. | ||
| // The second pass only iterates over the bits we already set. | ||
| var valueIndex = allValuesBitSet.nextSetBit(0); | ||
| while (valueIndex >= 0) { | ||
| var item = reachableValueList.get(valueIndex); | ||
| item.addEntity(entityIndex); | ||
| // We unset the current value index to import only the values that are reachable | ||
| // Co-values populated once per distinct range; addValuesExcept is idempotent across shared ranges. | ||
| item.addValuesExcept(allValuesBitSet, valueIndex); | ||
| // Entity membership registered per entity. | ||
| for (var entityIndex : entityIndices) { | ||
| item.addEntity(entityIndex); | ||
| } | ||
| valueIndex = allValuesBitSet.nextSetBit(valueIndex + 1); | ||
| } | ||
| } | ||
|
|
||
| private static <Value_> BitSet buildBitSetForValueRange(ValueRange<Value_> valueRange, Map<Value_, Integer> valueIndexMap) { | ||
| var valueBitSet = new BitSet((int) valueRange.getSize()); | ||
| Iterator<@Nullable Value_> iterator = valueRange.createOriginalIterator(); | ||
| var iterator = valueRange.createOriginalIterator(); | ||
| while (iterator.hasNext()) { | ||
| var value = iterator.next(); | ||
| if (value == null) { | ||
|
|
@@ -370,7 +365,7 @@ public static <Solution_, Entity_, Type_, Value_> ValueRangeItem<Solution_, Enti | |
| * The record holds a reference to {@link ValueRange}, | ||
| * a precomputed hash to avoid recalculating it every time. | ||
| */ | ||
| record HashedValueRange<T>(ValueRange<T> item, int hash) { | ||
| private record HashedValueRange<T>(ValueRange<T> item, int hash) { | ||
|
|
||
| public static <Value_> HashedValueRange<Value_> of(ValueRange<Value_> valueRange) { | ||
| return new HashedValueRange<>(valueRange, valueRange.hashCode()); | ||
|
|
@@ -383,10 +378,9 @@ public int hashCode() { | |
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (!(o instanceof HashedValueRange<?> that)) { | ||
| return false; | ||
| } | ||
| return hash == that.hash && Objects.equals(item, that.item); | ||
| return o instanceof HashedValueRange<?>(var otherItem, var otherHash) | ||
| && hash == otherHash | ||
| && Objects.equals(item, otherItem); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was only returned by a method that was never called...