Tuning buffer sizes


When the typical read/write size is small, using a buffer can make a significant improvement in performance. However when reading/writing large amounts of data, additional buffered doesn't help and can add a small amount of overhead.

Additionally, it can be tempting to assume; the larger the buffer the better. However is appears a more modest buffer size, around the size of your L1 cache could be better. e.g 8 KB to 32 KB.

The test

In the following test I compared using buffered and unbuffered reads and writes to a temporary file on a tmpfs filesystem (ram disk)

Size of
12 MB/s86 MB/s3 MB/s72 MB/s
24 MB/s165 MB/s6 MB/s147 MB/s
48 MB/s333 MB/s11 MB/s291 MB/s
817 MB/s578 MB/s24 MB/s560 MB/s
1634 MB/s920 MB/s49 MB/s983 MB/s
3267 MB/s1,345 MB/s99 MB/s1,679 MB/s
64133 MB/s1,746 MB/s198 MB/s2,518 MB/s
128254 MB/s2,024 MB/s391 MB/s3,387 MB/s
256463 MB/s2,172 MB/s742 MB/s4,104 MB/s
512798 MB/s2,270 MB/s1,334 MB/s4,549 MB/s
1,0241,270 MB/s2,299 MB/s2,355 MB/s4,752 MB/s
2,0481,789 MB/s2,310 MB/s3,704 MB/s4,923 MB/s
4,0962,287 MB/s2,301 MB/s5,324 MB/s4,859 MB/s
8,1922,589 MB/s2,497 MB/s6,346 MB/s6,142 MB/s
16,3842,534 MB/s2,559 MB/s5,764 MB/s5,697 MB/s
32,7682,591 MB/s2,561 MB/s5,793 MB/s5,723 MB/s
65,5362,613 MB/s2,581 MB/s5,861 MB/s5,883 MB/s
131,0722,580 MB/s2,581 MB/s5,401 MB/s5,405 MB/s
262,1441,918 MB/s1,907 MB/s3,269 MB/s3,262 MB/s
524,2881,749 MB/s1,734 MB/s2,845 MB/s2,851 MB/s
1,048,5761,471 MB/s1,469 MB/s2,501 MB/s2,502 MB/s

The default size of a BufferedInputStream and BufferedOutputStream is 8KB. This is part of the reason why they don't help at this point and larger.

The code



Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues