Optimize MappedJournalSegmentWriter.truncate() 00/110600/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 9 Mar 2024 13:33:11 +0000 (14:33 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Mar 2024 13:36:22 +0000 (14:36 +0100)
Reduce code duplication by separating current position manipulation and
entry zero-out.

Also use a single putLong() instead of two putInt()s, eliminating one
range check.

Overall this makes the two implementations much more similar, allowing
sharing code at some later point in the future.

JIRA: CONTROLLER-2095
Change-Id: I9791b5e27a779210b5500a7923ccada7156f26e5
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 5b9d16d22b8645011f8a79167da4d31a46195327)

atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java

index 6ca6ab30dd72cce4a8952782375d83a2e17b3c67..a851447d86122fd881a5c0fa8d03a745d92b85aa 100644 (file)
@@ -233,20 +233,18 @@ class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
     this.index.truncate(index);
 
     if (index < segment.index()) {
-      buffer.position(JournalSegmentDescriptor.BYTES);
-      buffer.putInt(0);
-      buffer.putInt(0);
+      // Reset the writer to the first entry.
       buffer.position(JournalSegmentDescriptor.BYTES);
     } else {
       // Reset the writer to the given index.
       reset(index);
-
-      // Zero entries after the given index.
-      int position = buffer.position();
-      buffer.putInt(0);
-      buffer.putInt(0);
-      buffer.position(position);
     }
+
+    // Zero the entry header at current buffer position.
+    int position = buffer.position();
+    // Note: we issue a single putLong() instead of two putInt()s.
+    buffer.putLong(0);
+    buffer.position(position);
   }
 
   @Override