Improve ByteBuffer invocations 44/110744/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 15 Mar 2024 09:58:55 +0000 (10:58 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 15 Mar 2024 09:59:58 +0000 (10:59 +0100)
ByteBuffer has a fluent API, returning the same buffer from methods
which do not return something else.

Take advantage of this a chain invocations, resulting in denser byte
code.

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

index c744477f87940ad0027e8ec8c4b34e733be20fb1..e4409ad25727a55bbf96c2b8892b57be0e2428b2 100644 (file)
@@ -56,8 +56,7 @@ final class FileChannelJournalSegmentReader<E> extends JournalSegmentReader<E> {
       // Read more bytes from the segment if necessary.
       if (memory.remaining() < maxEntrySize) {
         long position = currentPosition + memory.position();
-        memory.clear();
-        channel.read(memory, position);
+        channel.read(memory.clear(), position);
         currentPosition = position;
         memory.flip();
       }
index d6d77fbe02e48a31cd79d8babc5fe7c351945e4e..ec4d4a6dbb7883f6fec15b7e5f326623cacf640b 100644 (file)
@@ -96,8 +96,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
     try {
       // Clear memory buffer and read fist chunk
-      memory.clear();
-      channel.read(memory, JournalSegmentDescriptor.BYTES);
+      channel.read(memory.clear(), JournalSegmentDescriptor.BYTES);
       memory.flip();
 
       // Read the entry length.
@@ -134,8 +133,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
         // Read more bytes from the segment if necessary.
         if (memory.remaining() < maxEntrySize) {
-          memory.compact();
-          channel.read(memory);
+          channel.read(memory.compact());
           memory.flip();
         }
 
@@ -174,10 +172,8 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     final long index = getNextIndex();
 
     // Serialize the entry.
-    memory.clear();
-    memory.position(Integer.BYTES + Integer.BYTES);
     try {
-      namespace.serialize(entry, memory);
+      namespace.serialize(entry, memory.clear().position(Integer.BYTES + Integer.BYTES));
     } catch (KryoException e) {
       throw new StorageException.TooLarge("Entry size exceeds maximum allowed bytes (" + maxEntrySize + ")");
     }
@@ -201,8 +197,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     final long checksum = crc32.getValue();
 
     // Create a single byte[] in memory for the entire entry and write it as a batch to the underlying buffer.
-    memory.putInt(0, length);
-    memory.putInt(Integer.BYTES, (int) checksum);
+    memory.putInt(0, length).putInt(Integer.BYTES, (int) checksum);
     try {
       channel.write(memory, currentPosition);
     } catch (IOException e) {