Skip to content

Commit 415ff76

Browse files
authored
Merge pull request #136 from kilink/data-loader-helper-allocations
Avoid allocations in DataLoaderHelper.dispatch when there's no work
2 parents b4d67cc + 6b20182 commit 415ff76

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/main/java/org/dataloader/DataLoaderHelper.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,21 @@ Object getCacheKeyWithContext(K key, Object context) {
162162

163163
DispatchResult<V> dispatch() {
164164
boolean batchingEnabled = loaderOptions.batchingEnabled();
165-
//
166-
// we copy the pre-loaded set of futures ready for dispatch
167-
final List<K> keys = new ArrayList<>();
168-
final List<Object> callContexts = new ArrayList<>();
169-
final List<CompletableFuture<V>> queuedFutures = new ArrayList<>();
165+
final List<K> keys;
166+
final List<Object> callContexts;
167+
final List<CompletableFuture<V>> queuedFutures;
170168
synchronized (dataLoader) {
169+
int queueSize = loaderQueue.size();
170+
if (queueSize == 0) {
171+
lastDispatchTime.set(now());
172+
return emptyDispatchResult();
173+
}
174+
175+
// we copy the pre-loaded set of futures ready for dispatch
176+
keys = new ArrayList<>(queueSize);
177+
callContexts = new ArrayList<>(queueSize);
178+
queuedFutures = new ArrayList<>(queueSize);
179+
171180
loaderQueue.forEach(entry -> {
172181
keys.add(entry.getKey());
173182
queuedFutures.add(entry.getValue());
@@ -176,8 +185,8 @@ DispatchResult<V> dispatch() {
176185
loaderQueue.clear();
177186
lastDispatchTime.set(now());
178187
}
179-
if (!batchingEnabled || keys.isEmpty()) {
180-
return new DispatchResult<>(completedFuture(emptyList()), 0);
188+
if (!batchingEnabled) {
189+
return emptyDispatchResult();
181190
}
182191
final int totalEntriesHandled = keys.size();
183192
//
@@ -524,4 +533,11 @@ private CompletableFuture<List<V>> setToValueCache(List<V> assembledValues, List
524533
}
525534
return CompletableFuture.completedFuture(assembledValues);
526535
}
536+
537+
private static final DispatchResult<?> EMPTY_DISPATCH_RESULT = new DispatchResult<>(completedFuture(emptyList()), 0);
538+
539+
@SuppressWarnings("unchecked") // Casting to any type is safe since the underlying list is empty
540+
private static <T> DispatchResult<T> emptyDispatchResult() {
541+
return (DispatchResult<T>) EMPTY_DISPATCH_RESULT;
542+
}
527543
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy