mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
perf(s3): compute CRC64NVME eight bytes at a time (#4126)
S3 uses CRC64NVME when a request declares no checksum algorithm, so every such PutObject, UploadPart and CompleteMultipartUpload pays for it. The byte-at-a-time loop took about 2.4 ms per MB. Slicing-by-8 takes about 0.6 ms per MB and gives the same values.
This commit is contained in:
1 parent
d8239b3e66
commit
d6682cf452
2 files changed
+73
-8
No files matched your search
@@ -6,8 +6,12 @@ import io.github.hectorvent.floci.services.s3.model.ChecksumType;
|
||||
import io.github.hectorvent.floci.services.s3.model.S3Checksum;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -65,6 +69,37 @@ class S3ChecksumTest {
|
||||
assertEquals(S3Checksum.crc64NvmeBase64(data), ChecksumAlgorithm.CRC64NVME.compute(data));
|
||||
}
|
||||
|
||||
@Test
|
||||
void crc64NvmeMatchesTheCatalogueCheckValue() {
|
||||
assertEquals("rosUhgp5mIg=", S3Checksum.crc64NvmeBase64("123456789".getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void crc64NvmeMatchesABitwiseReferenceAtEveryLength() {
|
||||
Random random = new Random(64);
|
||||
List<Integer> lengths = new ArrayList<>();
|
||||
for (int length = 0; length <= 64; length++) {
|
||||
lengths.add(length);
|
||||
}
|
||||
lengths.addAll(List.of(1023, 1024, 1025, 65537, 1048579));
|
||||
for (int length : lengths) {
|
||||
byte[] data = new byte[length];
|
||||
random.nextBytes(data);
|
||||
assertEquals(bitwiseCrc64Nvme(data), S3Checksum.crc64NvmeBase64(data), "length " + length);
|
||||
}
|
||||
}
|
||||
|
||||
private static String bitwiseCrc64Nvme(byte[] data) {
|
||||
long crc = ~0L;
|
||||
for (byte b : data) {
|
||||
crc ^= b & 0xFFL;
|
||||
for (int bit = 0; bit < 8; bit++) {
|
||||
crc = (crc & 1) != 0 ? (crc >>> 1) ^ 0x9a6c9329ac4bc9b5L : crc >>> 1;
|
||||
}
|
||||
}
|
||||
return Base64.getEncoder().encodeToString(ByteBuffer.allocate(Long.BYTES).putLong(~crc).array());
|
||||
}
|
||||
|
||||
@Test
|
||||
void objectWithoutDeclaredAlgorithmGetsCrc64Nvme() {
|
||||
byte[] data = "hello".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
Reference in new issue
Block a user