Improve entry crc32 computation 71/110571/3
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 9 Mar 2024 15:03:10 +0000 (16:03 +0100)
committerRobert Varga <nite@hq.sk>
Mon, 11 Mar 2024 12:51:11 +0000 (12:51 +0000)
We have a codepath difference here, where we use either a ByteBuffer or
a raw array to compute CRC32.

MappedJournalSegmentWriter does not make it immediately clear we use
this buffer twice -- once for CRC32 and once for deserialization.

Move acquisition of slice just after we have read the expected CRC32, so
it is clear it is something we would be doing even if there were no
checksum involved.

Mirror the same in FileChannelJournalSegmentWriter, as this will allow
us to further consolidate the code and stop mucking with memory.limit(),
which is causing us to invalidate our previously-set mark.

JIRA: CONTROLLER-2095
Change-Id: I355bd97cd8acb4f5d9d91310de97ecb2cbd70282
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java
atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java

index 5bd5212c2eb83675327cb529c1ea18e5e0817c3d..39d23636769a351a2fb866f4e38668732dd60c04 100644 (file)
@@ -95,9 +95,13 @@ class FileChannelJournalSegmentWriter<E> implements JournalWriter<E> {
         // Read the checksum of the entry.
         final long checksum = memory.getInt() & 0xFFFFFFFFL;
 
+        // Slice off the entry's bytes
+        final ByteBuffer entryBytes = memory.slice();
+        entryBytes.limit(length);
+
         // Compute the checksum for the entry bytes.
         final CRC32 crc32 = new CRC32();
-        crc32.update(memory.array(), memory.position(), length);
+        crc32.update(entryBytes);
 
         // If the stored checksum does not equal the computed checksum, do not proceed further
         if (checksum != crc32.getValue()) {
index 7ff58590d996729c51e7b8c2612f7a23e82deed5..bc74e8b0ef899f99a17120d34447b1be60f2dd11 100644 (file)
@@ -97,19 +97,21 @@ class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
         // Read the checksum of the entry.
         final long checksum = buffer.getInt() & 0xFFFFFFFFL;
 
+        // Slice off the entry's bytes
+        final ByteBuffer entryBytes = buffer.slice();
+        entryBytes.limit(length);
+
         // Compute the checksum for the entry bytes.
         final CRC32 crc32 = new CRC32();
-        ByteBuffer slice = buffer.slice();
-        slice.limit(length);
-        crc32.update(slice);
+        crc32.update(entryBytes);
 
         // If the stored checksum does not equal the computed checksum, do not proceed further
         if (checksum != crc32.getValue()) {
           break;
         }
 
-        slice.rewind();
-        final E entry = namespace.deserialize(slice);
+        entryBytes.rewind();
+        final E entry = namespace.deserialize(entryBytes);
         lastEntry = new Indexed<>(nextIndex, entry, length);
         this.index.index(nextIndex, position);
         nextIndex++;