Skip to content

Commit

Permalink
Merge pull request #142 from dfa1/presize-result-loadMany
Browse files Browse the repository at this point in the history
Pre-size resulting lists
  • Loading branch information
bbakerman authored Mar 17, 2024
2 parents 415ff76 + 58ad658 commit e5e449e
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/dataloader/BatchLoaderEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public <K> Builder keyContexts(List<K> keys, List<Object> keyContexts) {
Assertions.nonNull(keyContexts);

Map<Object, Object> map = new HashMap<>();
List<Object> list = new ArrayList<>();
List<Object> list = new ArrayList<>(keys.size());
for (int i = 0; i < keys.size(); i++) {
K key = keys.get(i);
Object keyContext = null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dataloader/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
nonNull(keyContexts);

synchronized (this) {
List<CompletableFuture<V>> collect = new ArrayList<>();
List<CompletableFuture<V>> collect = new ArrayList<>(keys.size());
for (int i = 0; i < keys.size(); i++) {
K key = keys.get(i);
Object keyContext = null;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/dataloader/DataLoaderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ DispatchResult<V> dispatch() {
private CompletableFuture<List<V>> sliceIntoBatchesOfBatches(List<K> keys, List<CompletableFuture<V>> queuedFutures, List<Object> callContexts, int maxBatchSize) {
// the number of keys is > than what the batch loader function can accept
// so make multiple calls to the loader
List<CompletableFuture<List<V>>> allBatches = new ArrayList<>();
int len = keys.size();
int batchCount = (int) Math.ceil(len / (double) maxBatchSize);
List<CompletableFuture<List<V>>> allBatches = new ArrayList<>(batchCount);
for (int i = 0; i < batchCount; i++) {

int fromIndex = i * maxBatchSize;
Expand Down Expand Up @@ -477,7 +477,7 @@ private CompletableFuture<List<V>> invokeMapBatchLoader(List<K> keys, BatchLoade
}
CompletableFuture<Map<K, V>> mapBatchLoad = nonNull(loadResult, () -> "Your batch loader function MUST return a non null CompletionStage").toCompletableFuture();
return mapBatchLoad.thenApply(map -> {
List<V> values = new ArrayList<>();
List<V> values = new ArrayList<>(keys.size());
for (K key : keys) {
V value = map.get(key);
values.add(value);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dataloader/ValueCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ default CompletableFuture<List<Try<V>>> getValues(List<K> keys) throws ValueCach
* @throws ValueCachingNotSupported if this cache wants to short-circuit this method completely
*/
default CompletableFuture<List<V>> setValues(List<K> keys, List<V> values) throws ValueCachingNotSupported {
List<CompletableFuture<V>> cacheSets = new ArrayList<>();
List<CompletableFuture<V>> cacheSets = new ArrayList<>(keys.size());
for (int i = 0; i < keys.size(); i++) {
K k = keys.get(i);
V v = values.get(i);
Expand Down

0 comments on commit e5e449e

Please sign in to comment.