Skip to content

Commit

Permalink
Micro-opt: collections with expected size (#7006)
Browse files Browse the repository at this point in the history
  • Loading branch information
snazy committed Jun 7, 2023
1 parent edf4f2c commit aa93eb8
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static void deserializeObjIds(List<ByteString> bytes, Consumer<ObjId> rec
}

public static List<ObjId> deserializeObjIds(List<ByteString> predecessorsList) {
List<ObjId> result = new ArrayList<>();
List<ObjId> result = new ArrayList<>(predecessorsList.size());
deserializeObjIds(predecessorsList, result::add);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Maps.newHashMapWithExpectedSize;
import static com.google.common.collect.Sets.newHashSetWithExpectedSize;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.emptyIterator;
Expand Down Expand Up @@ -59,7 +60,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -596,7 +596,7 @@ public CommitObj buildCommitObj(
// Results in a bulk-(pre)fetch of the requested index stripes
fullIndex.loadIfNecessary(keys);

Map<UUID, CommitOp> removes = new HashMap<>();
Map<UUID, CommitOp> removes = newHashMapWithExpectedSize(createCommit.removes().size());
for (Remove remove : createCommit.removes()) {
StoreKey key = remove.key();
UUID contentId = remove.contentId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Maps.newHashMapWithExpectedSize;
import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -714,7 +715,7 @@ void validateMergeTransplantCommit(
commitValidation.addOperations(commitOperation(identifiedKey, OperationType.DELETE));
}

Map<ContentKey, UUID> seenContentIds = new HashMap<>();
Map<ContentKey, UUID> seenContentIds = newHashMapWithExpectedSize(createCommit.adds().size());
for (CreateCommit.Add add : createCommit.adds()) {
ContentKey key = storeKeyToKey(add.key());
if (key == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ CommitResult<Commit> commit(
RetryException,
ObjTooLargeException {
CreateCommit.Builder commit = newCommitBuilder().parentCommitId(headId());
List<Obj> objectsToStore = new ArrayList<>(operations.size());
List<Obj> objectsToStore = new ArrayList<>(operations.size() + 1);

CommitRetryState commitRetryState =
retryState.map(x -> (CommitRetryState) x).orElseGet(CommitRetryState::new);
Expand Down Expand Up @@ -212,13 +212,15 @@ void commitAddOperations(
CommitRetryState commitRetryState,
ImmutableCommitValidation.Builder commitValidation)
throws ObjNotFoundException, ReferenceConflictException {
Set<ContentKey> allKeys = new HashSet<>();
int num = operations.size();
Set<ContentKey> allKeys = newHashSetWithExpectedSize(num);

Set<StoreKey> storeKeysForHead =
expectedIndex() != headIndex() ? newHashSetWithExpectedSize(operations.size()) : null;

List<StoreKey> storeKeys = new ArrayList<>();
for (Operation operation : operations) {
List<StoreKey> storeKeys = new ArrayList<>(num);
for (int i = 0; i < num; i++) {
Operation operation = operations.get(i);
ContentKey key = operation.getKey();
checkArgument(allKeys.add(key), "Duplicate key in commit operations: %s", key);
StoreKey storeKey = keyToStoreKey(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.projectnessie.versioned.storage.versionstore;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Maps.newHashMapWithExpectedSize;
import static java.util.Objects.requireNonNull;
import static org.projectnessie.versioned.storage.common.logic.Logics.indexesLogic;
import static org.projectnessie.versioned.storage.common.objtypes.ContentValueObj.contentValue;
Expand All @@ -25,7 +26,6 @@
import static org.projectnessie.versioned.storage.versionstore.TypeMapping.toCommitMeta;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -71,9 +71,9 @@ public Content fetchContent(@Nonnull @jakarta.annotation.Nonnull ObjId objId)
public Map<ContentKey, Content> fetchContents(
@Nonnull @jakarta.annotation.Nonnull Map<ObjId, ContentKey> idsToKeys)
throws ObjNotFoundException {
Map<ContentKey, Content> r = new HashMap<>();
ObjId[] ids = idsToKeys.keySet().toArray(new ObjId[0]);
Obj[] objs = persist.fetchObjs(ids);
Map<ContentKey, Content> r = newHashMapWithExpectedSize(ids.length);
for (int i = 0; i < ids.length; i++) {
Obj obj = objs[i];
if (obj instanceof ContentValueObj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.projectnessie.versioned.storage.versionstore;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Sets.newHashSetWithExpectedSize;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -31,8 +32,9 @@ final class MergeBehaviors {

MergeBehaviors(MergeTransplantOpBase mergeTransplantOpBase) {
this.mergeTransplantOpBase = mergeTransplantOpBase;
this.remainingKeys = new HashSet<>(mergeTransplantOpBase.mergeKeyBehaviors().keySet());
this.keysUsedForCommit = new HashSet<>();
Set<ContentKey> allKeys = mergeTransplantOpBase.mergeKeyBehaviors().keySet();
this.remainingKeys = new HashSet<>(allKeys);
this.keysUsedForCommit = newHashSetWithExpectedSize(allKeys.size());
validate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.projectnessie.versioned.storage.versionstore;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Maps.newHashMapWithExpectedSize;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singleton;
Expand Down Expand Up @@ -59,7 +60,6 @@
import static org.projectnessie.versioned.store.DefaultStoreWorker.contentTypeForPayload;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -718,7 +718,7 @@ public Map<ContentKey, ContentResult> getValues(Ref ref, Collection<ContentKey>
index.loadIfNecessary(
keys.stream().map(TypeMapping::keyToStoreKey).collect(Collectors.toSet()));

Map<ObjId, ContentKey> idsToKeys = new HashMap<>();
Map<ObjId, ContentKey> idsToKeys = newHashMapWithExpectedSize(keys.size());
for (ContentKey key : keys) {
StoreKey storeKey = keyToStoreKey(key);
StoreIndexElement<CommitOp> indexElement = index.get(storeKey);
Expand Down

0 comments on commit aa93eb8

Please sign in to comment.