Skip to content

Commit e5e449e

Browse files
authored
Merge pull request #142 from dfa1/presize-result-loadMany
Pre-size resulting lists
2 parents 415ff76 + 58ad658 commit e5e449e

File tree

4 files changed

+5
-5
lines changed

4 files changed

+5
-5
lines changed

src/main/java/org/dataloader/BatchLoaderEnvironment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public <K> Builder keyContexts(List<K> keys, List<Object> keyContexts) {
8383
Assertions.nonNull(keyContexts);
8484

8585
Map<Object, Object> map = new HashMap<>();
86-
List<Object> list = new ArrayList<>();
86+
List<Object> list = new ArrayList<>(keys.size());
8787
for (int i = 0; i < keys.size(); i++) {
8888
K key = keys.get(i);
8989
Object keyContext = null;

src/main/java/org/dataloader/DataLoader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
561561
nonNull(keyContexts);
562562

563563
synchronized (this) {
564-
List<CompletableFuture<V>> collect = new ArrayList<>();
564+
List<CompletableFuture<V>> collect = new ArrayList<>(keys.size());
565565
for (int i = 0; i < keys.size(); i++) {
566566
K key = keys.get(i);
567567
Object keyContext = null;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ DispatchResult<V> dispatch() {
213213
private CompletableFuture<List<V>> sliceIntoBatchesOfBatches(List<K> keys, List<CompletableFuture<V>> queuedFutures, List<Object> callContexts, int maxBatchSize) {
214214
// the number of keys is > than what the batch loader function can accept
215215
// so make multiple calls to the loader
216-
List<CompletableFuture<List<V>>> allBatches = new ArrayList<>();
217216
int len = keys.size();
218217
int batchCount = (int) Math.ceil(len / (double) maxBatchSize);
218+
List<CompletableFuture<List<V>>> allBatches = new ArrayList<>(batchCount);
219219
for (int i = 0; i < batchCount; i++) {
220220

221221
int fromIndex = i * maxBatchSize;
@@ -477,7 +477,7 @@ private CompletableFuture<List<V>> invokeMapBatchLoader(List<K> keys, BatchLoade
477477
}
478478
CompletableFuture<Map<K, V>> mapBatchLoad = nonNull(loadResult, () -> "Your batch loader function MUST return a non null CompletionStage").toCompletableFuture();
479479
return mapBatchLoad.thenApply(map -> {
480-
List<V> values = new ArrayList<>();
480+
List<V> values = new ArrayList<>(keys.size());
481481
for (K key : keys) {
482482
V value = map.get(key);
483483
values.add(value);

src/main/java/org/dataloader/ValueCache.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ default CompletableFuture<List<Try<V>>> getValues(List<K> keys) throws ValueCach
115115
* @throws ValueCachingNotSupported if this cache wants to short-circuit this method completely
116116
*/
117117
default CompletableFuture<List<V>> setValues(List<K> keys, List<V> values) throws ValueCachingNotSupported {
118-
List<CompletableFuture<V>> cacheSets = new ArrayList<>();
118+
List<CompletableFuture<V>> cacheSets = new ArrayList<>(keys.size());
119119
for (int i = 0; i < keys.size(); i++) {
120120
K k = keys.get(i);
121121
V v = values.get(i);

0 commit comments

Comments
 (0)