Skip to content

Commit 9a9105c

Browse files
committed
don't calculate queue lenghth
1 parent 16cf340 commit 9a9105c

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ static class LoaderQueueEntry<K, V> {
5656
final CompletableFuture<V> value;
5757
final Object callContext;
5858
final LoaderQueueEntry<K, V> prev;
59+
final int queueSize;
5960

60-
public LoaderQueueEntry(K key, CompletableFuture<V> value, Object callContext, LoaderQueueEntry<K, V> prev) {
61+
public LoaderQueueEntry(K key, CompletableFuture<V> value, Object callContext, LoaderQueueEntry<K, V> prev, int queueSize) {
6162
this.key = key;
6263
this.value = value;
6364
this.callContext = callContext;
6465
this.prev = prev;
66+
this.queueSize = queueSize;
6567
}
6668

6769
K getKey() {
@@ -204,7 +206,7 @@ CompletableFuture<V> load(K key, Object loadContext) {
204206
private void addEntryToLoaderQueue(K key, CompletableFuture<V> future, Object loadContext) {
205207
while (true) {
206208
LoaderQueueEntry<K, V> prev = loaderQueue.get();
207-
LoaderQueueEntry<K, V> curr = new LoaderQueueEntry<>(key, future, loadContext, prev);
209+
LoaderQueueEntry<K, V> curr = new LoaderQueueEntry<>(key, future, loadContext, prev, prev != null ? prev.queueSize + 1 : 1);
208210
if (loaderQueue.compareAndSet(prev, curr)) {
209211
return;
210212
}
@@ -241,7 +243,7 @@ DispatchResult<V> dispatch() {
241243
instrCtx.onDispatched();
242244
return endDispatchCtx(instrCtx, emptyDispatchResult());
243245
}
244-
int queueSize = calcQueueDepth(loaderQueueEntryHead);
246+
int queueSize = loaderQueueEntryHead.queueSize;
245247
// we copy the pre-loaded set of futures ready for dispatch
246248
Object[] keysArray = new Object[queueSize];
247249
CompletableFuture[] queuedFuturesArray = new CompletableFuture[queueSize];
@@ -633,16 +635,12 @@ private DataLoaderInstrumentation instrumentation() {
633635
}
634636

635637
int dispatchDepth() {
636-
return calcQueueDepth(loaderQueue.get());
637-
}
638-
639-
private int calcQueueDepth(LoaderQueueEntry<K, V> head) {
640-
int count = 0;
641-
while (head != null) {
642-
count++;
643-
head = head.prev;
638+
LoaderQueueEntry<K, V> loaderQueueEntry = loaderQueue.get();
639+
if (loaderQueueEntry != null) {
640+
return loaderQueueEntry.queueSize;
641+
} else {
642+
return 0;
644643
}
645-
return count;
646644
}
647645

648646

0 commit comments

Comments
 (0)