Skip to content

Commit

Permalink
fix: 200: Improve performance: WritableStreamingData.writeBytes(Buffe…
Browse files Browse the repository at this point in the history
…redData) (#341)

Fixes: #200
Reviewed-by: Anthony Petrov <anthony@swirldslabs.com>, Ivan Malygin <ivan@swirldslabs.com>, Oleg Mazurov <oleg.mazurov@swirldslabs.com>
Signed-off-by: Artem Ananev <artem.ananev@swirldslabs.com>
  • Loading branch information
artemananiev authored Jan 8, 2025
1 parent dfaa501 commit 3d3b2ff
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pbj-core/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Version number
version=0.9.11-SNAPSHOT
version=0.9.13-SNAPSHOT

# Need increased heap for running Gradle itself, or SonarQube will run the JVM out of metaspace
org.gradle.jvmargs=-Xmx2048m
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.hedera.pbj.runtime.io;

import com.hedera.pbj.runtime.io.buffer.BufferedData;
import com.hedera.pbj.runtime.io.buffer.RandomAccessData;
import com.hedera.pbj.runtime.io.stream.WritableStreamingData;
import java.io.ByteArrayOutputStream;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

@SuppressWarnings("unused")
@State(Scope.Benchmark)
@Fork(1)
@Warmup(iterations = 3, time = 2)
@Measurement(iterations = 3, time = 5)
@OutputTimeUnit(TimeUnit.SECONDS)
@BenchmarkMode(Mode.Throughput)
public class WritableStreamingDataBench {

private static final int SIZE = 100_000;

private static final BufferedData BUF_TO_WRITE;

static {
final Random random = new Random(1234509876);
byte[] data = new byte[SIZE];
random.nextBytes(data);
BUF_TO_WRITE = BufferedData.wrap(data);
}

@Setup(Level.Invocation)
public void beforeEach() {
BUF_TO_WRITE.reset();
}

// Tests WritableSequentialData.writeBytes(BufferedData)
@Benchmark
public void writeBufferedData() {
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
final WritableSequentialData out = new WritableStreamingData(bout);
out.writeBytes(BUF_TO_WRITE);
assert out.position() == SIZE;
assert bout.toByteArray().length == SIZE;
}

// Tests WritableSequentialData.writeBytes(RandomAccessData)
@Benchmark
public void writeRandomAccessData() {
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
final WritableSequentialData out = new WritableStreamingData(bout);
out.writeBytes((RandomAccessData) BUF_TO_WRITE);
assert out.position() == SIZE;
assert bout.toByteArray().length == SIZE;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hedera.pbj.runtime.io.stream;

import com.hedera.pbj.runtime.io.WritableSequentialData;
import com.hedera.pbj.runtime.io.buffer.BufferedData;
import com.hedera.pbj.runtime.io.buffer.RandomAccessData;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.Closeable;
Expand Down Expand Up @@ -225,6 +226,20 @@ public void writeBytes(@NonNull final ByteBuffer src) {
}
}

/**
* {@inheritDoc}
*/
@Override
public void writeBytes(@NonNull BufferedData src) throws BufferOverflowException, UncheckedIOException {
if (remaining() < src.remaining()) {
throw new BufferOverflowException();
}
final int pos = Math.toIntExact(src.position());
final int len = Math.toIntExact(src.remaining());
src.writeTo(out, pos, len);
position += len;
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 3d3b2ff

Please sign in to comment.