1919import java .time .Clock ;
2020import java .time .Instant ;
2121import java .util .ArrayList ;
22+ import java .util .Arrays ;
2223import java .util .Collection ;
23- import java .util .Collections ;
2424import java .util .LinkedHashSet ;
2525import java .util .List ;
2626import java .util .Map ;
@@ -228,9 +228,6 @@ DispatchResult<V> dispatch() {
228228DataLoaderInstrumentationContext <DispatchResult <?>>instrCtx =ctxOrNoopCtx (instrumentation ().beginDispatch (dataLoader ));
229229
230230boolean batchingEnabled =loaderOptions .batchingEnabled ();
231- final List <K >keys ;
232- final List <Object >callContexts ;
233- final List <CompletableFuture <V >>queuedFutures ;
234231
235232LoaderQueueEntry <K ,V >loaderQueueEntryHead ;
236233while (true ) {
@@ -246,21 +243,21 @@ DispatchResult<V> dispatch() {
246243 }
247244int queueSize =calcQueueDepth (loaderQueueEntryHead );
248245// we copy the pre-loaded set of futures ready for dispatch
249- keys =new ArrayList <>( queueSize ) ;
250- callContexts =new ArrayList <>( queueSize ) ;
251- queuedFutures =new ArrayList <>( queueSize ) ;
252-
246+ Object [] keysArray =new Object [ queueSize ] ;
247+ CompletableFuture [] queuedFuturesArray =new CompletableFuture [ queueSize ] ;
248+ Object [] callContextsArray =new Object [ queueSize ] ;
249+ int index = queueSize - 1 ;
253250while (loaderQueueEntryHead !=null ) {
254- keys . add ( loaderQueueEntryHead .getKey () );
255- queuedFutures . add ( loaderQueueEntryHead .getValue () );
256- callContexts . add ( loaderQueueEntryHead .getCallContext () );
251+ keysArray [ index ] = loaderQueueEntryHead .getKey ();
252+ queuedFuturesArray [ index ] = loaderQueueEntryHead .getValue ();
253+ callContextsArray [ index ] = loaderQueueEntryHead .getCallContext ();
257254loaderQueueEntryHead =loaderQueueEntryHead .prev ;
255+ index --;
258256 }
259- //TODO: to many test depend on the previous order, therefore we reverse the lists here
260- // but this should not matter and we should change the tests
261- Collections .reverse (keys );
262- Collections .reverse (callContexts );
263- Collections .reverse (queuedFutures );
257+ final List <K >keys = (List <K >)Arrays .asList (keysArray );
258+ final List <CompletableFuture <V >>queuedFutures =Arrays .asList (queuedFuturesArray );
259+ final List <Object >callContexts =Arrays .asList (callContextsArray );
260+
264261lastDispatchTime .set (now ());
265262if (!batchingEnabled ) {
266263instrCtx .onDispatched ();