Skip to content

Avoid allocations in DataLoaderHelper.dispatch when there's no work #136

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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/main/java/org/dataloader/DataLoaderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,21 @@ Object getCacheKeyWithContext(K key, Object context) {

DispatchResult<V> dispatch() {
boolean batchingEnabled = loaderOptions.batchingEnabled();
//
// we copy the pre-loaded set of futures ready for dispatch
final List<K> keys = new ArrayList<>();
final List<Object> callContexts = new ArrayList<>();
final List<CompletableFuture<V>> queuedFutures = new ArrayList<>();
final List<K> keys;
final List<Object> callContexts;
final List<CompletableFuture<V>> queuedFutures;
synchronized (dataLoader) {
int queueSize = loaderQueue.size();
if (queueSize == 0) {
lastDispatchTime.set(now());
return emptyDispatchResult();
}

// we copy the pre-loaded set of futures ready for dispatch
keys = new ArrayList<>(queueSize);
callContexts = new ArrayList<>(queueSize);
queuedFutures = new ArrayList<>(queueSize);

loaderQueue.forEach(entry -> {
keys.add(entry.getKey());
queuedFutures.add(entry.getValue());
Expand All @@ -176,8 +185,8 @@ DispatchResult<V> dispatch() {
loaderQueue.clear();
lastDispatchTime.set(now());
}
if (!batchingEnabled || keys.isEmpty()) {
return new DispatchResult<>(completedFuture(emptyList()), 0);
if (!batchingEnabled) {
return emptyDispatchResult();
}
final int totalEntriesHandled = keys.size();
//
Expand Down Expand Up @@ -524,4 +533,11 @@ private CompletableFuture<List<V>> setToValueCache(List<V> assembledValues, List
}
return CompletableFuture.completedFuture(assembledValues);
}

private static final DispatchResult<?> EMPTY_DISPATCH_RESULT = new DispatchResult<>(completedFuture(emptyList()), 0);

@SuppressWarnings("unchecked") // Casting to any type is safe since the underlying list is empty
private static <T> DispatchResult<T> emptyDispatchResult() {
return (DispatchResult<T>) EMPTY_DISPATCH_RESULT;
}
}