Optimize SegmentedJournalReader.getCurrentEntry() 82/110682/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 12 Mar 2024 20:49:59 +0000 (21:49 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 13 Mar 2024 07:28:57 +0000 (08:28 +0100)
This method has very simple logic, the reasoning for which is left
unexplained.

Add a comment about what is going on and use assign-and-check construct
to trim a bit of byte code.

JIRA: CONTROLLER-2106
Change-Id: I5d4e5551ae089489636da38600109f7821fa306b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedJournalReader.java

index 3022b5190324b8ba7d25076aab1e9bbdbce7a854..e5a9094f3697c47c70532a6faabfb6881af7c579 100644 (file)
@@ -53,11 +53,10 @@ sealed class SegmentedJournalReader<E> implements JournalReader<E> permits Commi
 
   @Override
   public final Indexed<E> getCurrentEntry() {
-    Indexed<E> currentEntry = currentReader.getCurrentEntry();
-    if (currentEntry != null) {
-      return currentEntry;
-    }
-    return previousEntry;
+    // If previousEntry was the last in the previous segment, we may have moved currentReader to the next segment.
+    // That segment may be empty, though, in which case we need to report the previousEntry.
+    final Indexed<E> currentEntry;
+    return (currentEntry = currentReader.getCurrentEntry()) != null ? currentEntry : previousEntry;
   }
 
   @Override