question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ByteBuf very poor sequential read/write performance compared with ByteBuffer or byte[]

See original GitHub issue

Expected behavior

ByteBuf better performance

Actual behavior

ByteBuf seq R/w is the worst

Steps to reproduce

$java SeqRwPerf
Netty ByteBuf seq R/W time: 5668ms
Java ByteBuffer seq R/W time: 4241ms
Java byte[] seq R/W time: 1528ms

Minimal yet complete reproducer code (or URL to code)

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

import java.nio.ByteBuffer;

public class SeqRwPerf {

    static final ByteBufAllocator ALLOC = ByteBufAllocator.DEFAULT;
    static final int N = 4 << 10, M = 100 * N;

    public static void main(String[] args) {
        long ta = System.currentTimeMillis();
        testNettyRw();
        long tb = System.currentTimeMillis();
        System.out.printf("Netty ByteBuf seq R/W time: %sms%n", tb - ta);

        ta = System.currentTimeMillis();
        testNioRw();
        tb = System.currentTimeMillis();
        System.out.printf("Java ByteBuffer seq R/W time: %sms%n", tb - ta);

        ta = System.currentTimeMillis();
        testByteaRw();
        tb = System.currentTimeMillis();
        System.out.printf("Java byte[] seq R/W time: %sms%n", tb - ta);
    }

    public static void testNettyRw() {
        for (int i = 0; i < M; ++i) {
            ByteBuf buf = ALLOC.buffer(N);

            for (int j = 0; j < N; ++j) {
                buf.writeByte(j);
            }
            for (int j = 0; j < N; ++j) {
                byte b = buf.readByte();
                assertEquals(b, (byte) j);
            }

            buf.release();
        }
    }

    public static void testNioRw() {
        for (int i = 0; i < M; ++i) {
            ByteBuffer buf = ByteBuffer.allocate(N);

            for (int j = 0; j < N; ++j) {
                buf.put((byte)j);
            }
            buf.flip();
            for (int j = 0; j < N; ++j) {
                byte b = buf.get();
                assertEquals(b, (byte) j);
            }
        }
    }

    public static void testByteaRw() {
        for (int i = 0; i < M; ++i) {
            byte[] buf = new byte[N];

            for (int j = 0; j < N; ++j) {
                buf[j] = (byte)j;
            }
            for (int j = 0; j < N; ++j) {
                byte b = buf[j];
                assertEquals(b, (byte) j);
            }
        }
    }

    static void assertEquals(int a, int b) {
        if (a != b) throw new AssertionError(a + " != " + b);
    }

}

Netty version

netty-4.1.67.Final

JVM version (e.g. java -version)

java version “1.8.0_231” Java™ SE Runtime Environment (build 1.8.0_231-b11) Java HotSpot™ 64-Bit Server VM (build 25.231-b11, mixed mode)

OS version (e.g. uname -a)

Linux 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
hyperxprocommented, Sep 14, 2021

@franz1981

Looks like I have found more friends who do benchmark in a lazy way. Hahahahaha…

0reactions
normanmaurercommented, Dec 9, 2021

Closing as there was no more response

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Bytebuffer can only read sequentially? - Stack Overflow
I am mapping a file to memory and reading it back with java's ByteBuffer. This proves to be a really fast way of...
Read more >
Chapter 5. ByteBuf - Netty in Action - liveBook · Manning
In this chapter we'll illustrate the superior functionality and flexibility of ByteBuf as compared to the JDK's ByteBuffer .
Read more >
Efficient Java I/O: byte[], ByteBuffers, and ... - Evan Jones
To quantify the performance differences, I wrote a small microbenchmark that compares byte[], direct ByteBuffers and heap ByteBuffers.
Read more >
ByteBuf (Netty API Reference (4.0.56.Final))
A random and sequential accessible sequence of zero or more bytes (octets). This interface provides an abstract view for one or more primitive...
Read more >
Which memory is faster Heap or ByteBuffer or Direct ?
DirectBytebuffer sucks in read speed, i am not sure why it is so slow. So if memory read/write is becoming bottle neck in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found