Improve entry crc32 computation
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / MappedJournalSegmentWriter.java
index a851447d86122fd881a5c0fa8d03a745d92b85aa..bc74e8b0ef899f99a17120d34447b1be60f2dd11 100644 (file)
@@ -97,23 +97,25 @@ 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);
-
-        // If the stored checksum equals the computed checksum, return the entry.
-        if (checksum == crc32.getValue()) {
-          slice.rewind();
-          final E entry = namespace.deserialize(slice);
-          lastEntry = new Indexed<>(nextIndex, entry, length);
-          this.index.index(nextIndex, position);
-          nextIndex++;
-        } else {
+        crc32.update(entryBytes);
+
+        // If the stored checksum does not equal the computed checksum, do not proceed further
+        if (checksum != crc32.getValue()) {
           break;
         }
 
+        entryBytes.rewind();
+        final E entry = namespace.deserialize(entryBytes);
+        lastEntry = new Indexed<>(nextIndex, entry, length);
+        this.index.index(nextIndex, position);
+        nextIndex++;
+
         // Update the current position for indexing.
         position = buffer.position() + length;
         buffer.position(position);