-
Notifications
You must be signed in to change notification settings - Fork 202
perf: Support reusing the best solution instead of cloning in LS #2332
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Semaphore; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.concurrent.locks.Lock; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
| import java.util.function.BiConsumer; | ||
| import java.util.function.BooleanSupplier; | ||
| import java.util.function.Consumer; | ||
|
|
@@ -66,33 +68,36 @@ public ConsumerSupport(ProblemId_ problemId, @Nullable Consumer<NewBestSolutionE | |
| } | ||
|
|
||
| void consumeIntermediateBestSolution(Solution_ solution, EventProducerId producerId, | ||
| BooleanSupplier isEveryProblemChangeProcessed) { | ||
| BooleanSupplier isEveryProblemChangeProcessed, Lock lock) { | ||
| /* | ||
| * If the bestSolutionConsumer is not provided, the best solution is still set for the purpose of recording | ||
| * problem changes. | ||
| */ | ||
| bestSolutionHolder.set(solution, producerId, isEveryProblemChangeProcessed); | ||
| if (bestSolutionConsumer != null) { | ||
| tryConsumeWaitingIntermediateBestSolution(); | ||
| tryConsumeWaitingIntermediateBestSolution(lock); | ||
| } | ||
| } | ||
|
|
||
| // Called both on the Solver thread and the Consumer thread. | ||
| private void tryConsumeWaitingIntermediateBestSolution() { | ||
| private void tryConsumeWaitingIntermediateBestSolution(Lock lock) { | ||
| if (bestSolutionHolder.isEmpty()) { | ||
| return; // There is no best solution to consume. | ||
| } | ||
| if (activeConsumption.tryAcquire()) { | ||
| scheduleIntermediateBestSolutionConsumption() | ||
| scheduleIntermediateBestSolutionConsumption(lock) | ||
| .whenCompleteAsync((solution, throwable) -> { | ||
| activeConsumption.release(); | ||
| tryConsumeWaitingIntermediateBestSolution(); | ||
| tryConsumeWaitingIntermediateBestSolution(lock); | ||
| }, consumerExecutor); | ||
| } | ||
| } | ||
|
|
||
| private CompletableFuture<Void> scheduleIntermediateBestSolutionConsumption() { | ||
| private CompletableFuture<Void> scheduleIntermediateBestSolutionConsumption(Lock lock) { | ||
|
Contributor
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. I believe here is another place where we should consider keeping the existing contract unchanged. Let's say we can create |
||
| return CompletableFuture.runAsync(() -> { | ||
| // If reuse best solution is enabled, this will block if the best solution | ||
| // is currently being updated. | ||
| lock.lock(); | ||
| var bestSolutionContainingProblemChanges = bestSolutionHolder.take(); | ||
| if (bestSolutionContainingProblemChanges != null) { | ||
| try { | ||
|
|
@@ -107,6 +112,7 @@ private CompletableFuture<Void> scheduleIntermediateBestSolutionConsumption() { | |
| bestSolutionContainingProblemChanges.completeProblemChangesExceptionally(throwable); | ||
| } | ||
| } | ||
| lock.unlock(); | ||
| }, consumerExecutor); | ||
| } | ||
|
|
||
|
|
@@ -175,7 +181,7 @@ void consumeFinalBestSolution(Solution_ solution) { // Called on the Solver thre | |
| // Situation: | ||
| // The consumer is consuming the last but one best solution. The final best solution is waiting for the consumer. | ||
| if (bestSolutionConsumer != null) { | ||
| scheduleIntermediateBestSolutionConsumption(); | ||
| scheduleIntermediateBestSolutionConsumption(new ReentrantLock()); | ||
| } | ||
| scheduleFinalBestSolutionConsumption(solution) | ||
| .whenComplete((unused, throwable) -> releaseAll()); | ||
|
|
||
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.
I'm not sure if we need a new property. Do we always want to use the new related logic? Are there scenarios where enabling this feature by default may not be recommended?
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.
+1. Already discussed on Slack with Chris; no external configuration, we do the right thing.
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.
The primary reason for the switch is that this reuses the existing solution; users might rely on getting new best solution instances if they save them outside the event handler (ex: store them in a
MutableReference).