From d65837cffb7128b7a790eca9c73d5d8fabc3da57 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 15 Mar 2024 10:58:55 +0100 Subject: [PATCH] Improve ByteBuffer invocations 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 --- .../journal/FileChannelJournalSegmentReader.java | 3 +-- .../journal/FileChannelJournalSegmentWriter.java | 13 ++++--------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentReader.java b/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentReader.java index c744477f87..e4409ad257 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentReader.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentReader.java @@ -56,8 +56,7 @@ final class FileChannelJournalSegmentReader extends JournalSegmentReader { // 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(); } diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java index d6d77fbe02..ec4d4a6dbb 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java @@ -96,8 +96,7 @@ final class FileChannelJournalSegmentWriter extends JournalSegmentWriter { 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 extends JournalSegmentWriter { // 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 extends JournalSegmentWriter { 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 extends JournalSegmentWriter { 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) { -- 2.36.6