From: Robert Varga Date: Sat, 9 Mar 2024 13:33:11 +0000 (+0100) Subject: Optimize MappedJournalSegmentWriter.truncate() X-Git-Tag: v8.0.5~50 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=45d8ce60e08fa1715cc42a06ab3516984f73450f;p=controller.git Optimize MappedJournalSegmentWriter.truncate() 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 (cherry picked from commit 5b9d16d22b8645011f8a79167da4d31a46195327) --- diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java index 6ca6ab30dd..a851447d86 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java @@ -233,20 +233,18 @@ class MappedJournalSegmentWriter implements JournalWriter { 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