Skip to content

Commit

Permalink
When passing a sub-array (offset, length) to the Storage#create method
Browse files Browse the repository at this point in the history
the array is needlessly cloned leading to unnecessary memory
allocations.

Signed-off-by: benoit <b.wiart@ubik-ingenierie.com>
  • Loading branch information
benbenw committed Sep 14, 2020
1 parent 10cc743 commit d0e86f2
Showing 1 changed file with 7 additions and 8 deletions.
Expand Up @@ -154,7 +154,7 @@ public Blob create(BlobInfo blobInfo, BlobTargetOption... options) {
.setMd5(EMPTY_BYTE_ARRAY_MD5)
.setCrc32c(EMPTY_BYTE_ARRAY_CRC32C)
.build();
return internalCreate(updatedInfo, EMPTY_BYTE_ARRAY, options);
return internalCreate(updatedInfo, EMPTY_BYTE_ARRAY, 0, 0, options);
}

@Override
Expand All @@ -168,23 +168,22 @@ public Blob create(BlobInfo blobInfo, byte[] content, BlobTargetOption... option
BaseEncoding.base64()
.encode(Ints.toByteArray(Hashing.crc32c().hashBytes(content).asInt())))
.build();
return internalCreate(updatedInfo, content, options);
return internalCreate(updatedInfo, content, 0, content.length, options);
}

@Override
public Blob create(
BlobInfo blobInfo, byte[] content, int offset, int length, BlobTargetOption... options) {
content = firstNonNull(content, EMPTY_BYTE_ARRAY);
byte[] subContent = Arrays.copyOfRange(content, offset, offset + length);
BlobInfo updatedInfo =
blobInfo
.toBuilder()
.setMd5(BaseEncoding.base64().encode(Hashing.md5().hashBytes(subContent).asBytes()))
.setMd5(BaseEncoding.base64().encode(Hashing.md5().hashBytes(content, offset, length).asBytes()))
.setCrc32c(
BaseEncoding.base64()
.encode(Ints.toByteArray(Hashing.crc32c().hashBytes(subContent).asInt())))
.encode(Ints.toByteArray(Hashing.crc32c().hashBytes(content, offset, length).asInt())))
.build();
return internalCreate(updatedInfo, subContent, options);
return internalCreate(updatedInfo, content, offset, length, options);
}

@Override
Expand All @@ -199,7 +198,7 @@ public Blob create(BlobInfo blobInfo, InputStream content, BlobWriteOption... op
return Blob.fromPb(this, storageRpc.create(blobPb, inputStreamParam, optionsMap));
}

private Blob internalCreate(BlobInfo info, final byte[] content, BlobTargetOption... options) {
private Blob internalCreate(BlobInfo info, final byte[] content, final int offset, final int length, BlobTargetOption... options) {
Preconditions.checkNotNull(content);
final StorageObject blobPb = info.toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(info, options);
Expand All @@ -210,7 +209,7 @@ private Blob internalCreate(BlobInfo info, final byte[] content, BlobTargetOptio
new Callable<StorageObject>() {
@Override
public StorageObject call() {
return storageRpc.create(blobPb, new ByteArrayInputStream(content), optionsMap);
return storageRpc.create(blobPb, new ByteArrayInputStream(content, offset, length), optionsMap);
}
},
getOptions().getRetrySettings(),
Expand Down

0 comments on commit d0e86f2

Please sign in to comment.