Skip to content

Instantly share code, notes, and snippets.

@vlsi
Last active December 16, 2023 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlsi/7f4411515a4f2dbb0925fffde92ccb1d to your computer and use it in GitHub Desktop.
Save vlsi/7f4411515a4f2dbb0925fffde92ccb1d to your computer and use it in GitHub Desktop.
Benchmark results for JDK-8321271 Add OutputStream.write(ByteBuffer)

Links

Findings

ByteArrayInputStream.transferTo(BufferedOutputStream) no longer allocates temporary arrays, and it improves performance ~2x for 1MiB inputs.

The current ByteArrayInputStream always allocates 128KiB buffer (it allocates min(bufferedData.length, 128KiB) copies the data for untrusted streams which is likely the reason the suggested in JDK-8321271 approach yields ~5x improvement for 10KiB input.

ByteArrayInputStream.transferTo(FileOutputStream) was special-cased previously, so the performance is the same. However, ByteArrayInputStream.transferTo(DataOutputStream(FileOutputStream)) can not be special-cased (see transferToDataFile), and the suggested in JDK-8321271 approach again reduces allocations and add improves performance.

Test setup

Apple M1, openjdk ce108446ca1fe604ecc24bbefb0bf1c6318271c7

$ make test TEST="micro:ByteArrayInputStreamTransfer" MICRO="FORK=2;WARMUP_ITER=2;OPTIONS=-prof gc"

Response time results

UDP 2023-12-16: ZIP-related failures are fixed, so benchmark results are valid now

Before

Benchmark                                   (size)  Mode  Cnt       Score     Error   Units
transferToBAOS                                1024  avgt   12       0,046 ±   0,004   us/op
transferToBAOS                              102400  avgt   12       1,637 ±   0,395   us/op
transferToBAOS                             1024000  avgt   12      20,467 ±   1,614   us/op
transferToBufferedBAOS                        1024  avgt   12       0,109 ±   0,002   us/op
transferToBufferedBAOS                      102400  avgt   12       7,012 ±   0,043   us/op
transferToBufferedBAOS                     1024000  avgt   12      47,927 ±   0,948   us/op
transferToDataFile                            1024  avgt   12      49,816 ±   1,173   us/op
transferToDataFile                          102400  avgt   12      79,839 ±   2,705   us/op
transferToDataFile                         1024000  avgt   12     442,584 ±  24,911   us/op
transferToFile                                1024  avgt   12      50,021 ±   1,661   us/op
transferToFile                              102400  avgt   12      75,704 ±   2,429   us/op
transferToFile                             1024000  avgt   12     415,853 ±  32,296   us/op
transferToGZIP                                1024  avgt   12      18,019 ±   0,828   us/op
transferToGZIP                              102400  avgt   12    1783,493 ±  21,657   us/op
transferToGZIP                             1024000  avgt   12   19647,320 ± 157,758   us/op

After

Benchmark                                   (size)  Mode  Cnt      Score     Error   Units
transferToBAOS                                1024  avgt   12      0,046 ±   0,008   us/op
transferToBAOS                              102400  avgt   12      1,539 ±   0,381   us/op
transferToBAOS                             1024000  avgt   12     20,561 ±   2,254   us/op
transferToBufferedBAOS                        1024  avgt   12      0,071 ±   0,002   us/op
transferToBufferedBAOS                      102400  avgt   12      1,596 ±   0,346   us/op
transferToBufferedBAOS                     1024000  avgt   12     18,198 ±   3,705   us/op
transferToDataFile                            1024  avgt   12     48,778 ±   2,007   us/op
transferToDataFile                          102400  avgt   12     75,509 ±   1,703   us/op
transferToDataFile                         1024000  avgt   12    389,400 ±  20,381   us/op
transferToFile                                1024  avgt   12     49,936 ±   2,021   us/op
transferToFile                              102400  avgt   12     76,250 ±   4,523   us/op
transferToFile                             1024000  avgt   12    553,221 ± 119,340   us/op
transferToGZIP                                1024  avgt   12     19,963 ±   1,815   us/op
transferToGZIP                              102400  avgt   12   1786,237 ±  10,187   us/op
transferToGZIP                             1024000  avgt   12  19593,844 ±  55,469   us/op

Full results

Before

Benchmark                                   (size)  Mode  Cnt       Score     Error   Units
transferToBAOS                                1024  avgt   12       0,046 ±   0,004   us/op
transferToBAOS:gc.alloc.rate                  1024  avgt   12       0,009 ±   0,001  MB/sec
transferToBAOS:gc.alloc.rate.norm             1024  avgt   12       0,001 ±   0,001    B/op
transferToBAOS:gc.count                       1024  avgt   12         ~ 0            counts
transferToBAOS                              102400  avgt   12       1,637 ±   0,395   us/op
transferToBAOS:gc.alloc.rate                102400  avgt   12       0,105 ±   0,001  MB/sec
transferToBAOS:gc.alloc.rate.norm           102400  avgt   12       0,185 ±   0,044    B/op
transferToBAOS:gc.count                     102400  avgt   12         ~ 0            counts
transferToBAOS                             1024000  avgt   12      20,467 ±   1,614   us/op
transferToBAOS:gc.alloc.rate               1024000  avgt   12       0,978 ±   0,002  MB/sec
transferToBAOS:gc.alloc.rate.norm          1024000  avgt   12      21,077 ±   1,659    B/op
transferToBAOS:gc.count                    1024000  avgt   12         ~ 0            counts
transferToBufferedBAOS                        1024  avgt   12       0,109 ±   0,002   us/op
transferToBufferedBAOS:gc.alloc.rate          1024  avgt   12    7028,938 ± 104,401  MB/sec
transferToBufferedBAOS:gc.alloc.rate.norm     1024  avgt   12    1040,001 ±   0,001    B/op
transferToBufferedBAOS:gc.count               1024  avgt   12      96,000            counts
transferToBufferedBAOS:gc.time                1024  avgt   12      68,000                ms
transferToBufferedBAOS                      102400  avgt   12       7,012 ±   0,043   us/op
transferToBufferedBAOS:gc.alloc.rate        102400  avgt   12   13857,300 ±  85,362  MB/sec
transferToBufferedBAOS:gc.alloc.rate.norm   102400  avgt   12  102416,776 ±   0,004    B/op
transferToBufferedBAOS:gc.count             102400  avgt   12     142,000            counts
transferToBufferedBAOS:gc.time              102400  avgt   12     100,000                ms
transferToBufferedBAOS                     1024000  avgt   12      47,927 ±   0,948   us/op
transferToBufferedBAOS:gc.alloc.rate       1024000  avgt   12    2602,958 ±  52,568  MB/sec
transferToBufferedBAOS:gc.alloc.rate.norm  1024000  avgt   12  131137,294 ±   0,972    B/op
transferToBufferedBAOS:gc.count            1024000  avgt   12      53,000            counts
transferToBufferedBAOS:gc.time             1024000  avgt   12      32,000                ms
transferToDataFile                            1024  avgt   12      49,816 ±   1,173   us/op
transferToDataFile:gc.alloc.rate              1024  avgt   12      24,180 ±   0,567  MB/sec
transferToDataFile:gc.alloc.rate.norm         1024  avgt   12    1264,461 ±   0,017    B/op
transferToDataFile:gc.count                   1024  avgt   12       2,000            counts
transferToDataFile:gc.time                    1024  avgt   12       2,000                ms
transferToDataFile                          102400  avgt   12      79,839 ±   2,705   us/op
transferToDataFile:gc.alloc.rate            102400  avgt   12    1225,219 ±  40,589  MB/sec
transferToDataFile:gc.alloc.rate.norm       102400  avgt   12  102648,813 ±   0,293    B/op
transferToDataFile:gc.count                 102400  avgt   12      26,000            counts
transferToDataFile:gc.time                  102400  avgt   12      18,000                ms
transferToDataFile                         1024000  avgt   12     442,584 ±  24,911   us/op
transferToDataFile:gc.alloc.rate           1024000  avgt   12     283,786 ±  15,741  MB/sec
transferToDataFile:gc.alloc.rate.norm      1024000  avgt   12  131766,626 ±  26,260    B/op
transferToDataFile:gc.count                1024000  avgt   12       6,000            counts
transferToDataFile:gc.time                 1024000  avgt   12       2,000                ms
transferToFile                                1024  avgt   12      50,021 ±   1,661   us/op
transferToFile:gc.alloc.rate                  1024  avgt   12       2,752 ±   0,090  MB/sec
transferToFile:gc.alloc.rate.norm             1024  avgt   12     144,466 ±   0,012    B/op
transferToFile:gc.count                       1024  avgt   12         ~ 0            counts
transferToFile                              102400  avgt   12      75,704 ±   2,429   us/op
transferToFile:gc.alloc.rate                102400  avgt   12       1,920 ±   0,058  MB/sec
transferToFile:gc.alloc.rate.norm           102400  avgt   12     152,519 ±   0,602    B/op
transferToFile:gc.count                     102400  avgt   12         ~ 0            counts
transferToFile                             1024000  avgt   12     415,853 ±  32,296   us/op
transferToFile:gc.alloc.rate               1024000  avgt   12       1,309 ±   0,025  MB/sec
transferToFile:gc.alloc.rate.norm          1024000  avgt   12     571,616 ±  33,224    B/op
transferToFile:gc.count                    1024000  avgt   12         ~ 0            counts
transferToGZIP                                1024  avgt   12      18,019 ±   0,828   us/op
transferToGZIP:gc.alloc.rate                  1024  avgt   12      95,582 ±   4,362  MB/sec
transferToGZIP:gc.alloc.rate.norm             1024  avgt   12    1808,167 ±   0,010    B/op
transferToGZIP:gc.count                       1024  avgt   12       2,000            counts
transferToGZIP:gc.time                        1024  avgt   12       2,000                ms
transferToGZIP                              102400  avgt   12    1783,493 ±  21,657   us/op
transferToGZIP:gc.alloc.rate                102400  avgt   12      55,247 ±   0,662  MB/sec
transferToGZIP:gc.alloc.rate.norm           102400  avgt   12  103380,407 ±   2,432    B/op
transferToGZIP:gc.count                     102400  avgt   12         ~ 0            counts
transferToGZIP                             1024000  avgt   12   19647,320 ± 157,758   us/op
transferToGZIP:gc.alloc.rate               1024000  avgt   12       7,358 ±   0,050  MB/sec
transferToGZIP:gc.alloc.rate.norm          1024000  avgt   12  151838,119 ± 246,437    B/op
transferToGZIP:gc.count                    1024000  avgt   12       2,000            counts
transferToGZIP:gc.time                     1024000  avgt   12       2,000                ms

After

Benchmark                                   (size)  Mode  Cnt      Score     Error   Units
transferToBAOS                                1024  avgt   12      0,046 ±   0,008   us/op
transferToBAOS:gc.alloc.rate                  1024  avgt   12      0,009 ±   0,001  MB/sec
transferToBAOS:gc.alloc.rate.norm             1024  avgt   12      0,001 ±   0,001    B/op
transferToBAOS:gc.count                       1024  avgt   12        ~ 0            counts
transferToBAOS                              102400  avgt   12      1,539 ±   0,381   us/op
transferToBAOS:gc.alloc.rate                102400  avgt   12     35,240 ±   8,539  MB/sec
transferToBAOS:gc.alloc.rate.norm           102400  avgt   12     56,173 ±   0,042    B/op
transferToBAOS:gc.count                     102400  avgt   12      2,000            counts
transferToBAOS:gc.time                      102400  avgt   12      3,000                ms
transferToBAOS                             1024000  avgt   12     20,561 ±   2,254   us/op
transferToBAOS:gc.alloc.rate               1024000  avgt   12      0,978 ±   0,002  MB/sec
transferToBAOS:gc.alloc.rate.norm          1024000  avgt   12     21,170 ±   2,325    B/op
transferToBAOS:gc.count                    1024000  avgt   12        ~ 0            counts
transferToBufferedBAOS                        1024  avgt   12      0,071 ±   0,002   us/op
transferToBufferedBAOS:gc.alloc.rate          1024  avgt   12      0,009 ±   0,001  MB/sec
transferToBufferedBAOS:gc.alloc.rate.norm     1024  avgt   12      0,001 ±   0,001    B/op
transferToBufferedBAOS:gc.count               1024  avgt   12        ~ 0            counts
transferToBufferedBAOS                      102400  avgt   12      1,596 ±   0,346   us/op
transferToBufferedBAOS:gc.alloc.rate        102400  avgt   12     33,768 ±   7,426  MB/sec
transferToBufferedBAOS:gc.alloc.rate.norm   102400  avgt   12     56,179 ±   0,038    B/op
transferToBufferedBAOS:gc.count             102400  avgt   12      2,000            counts
transferToBufferedBAOS:gc.time              102400  avgt   12      3,000                ms
transferToBufferedBAOS                     1024000  avgt   12     18,198 ±   3,705   us/op
transferToBufferedBAOS:gc.alloc.rate       1024000  avgt   12      0,978 ±   0,001  MB/sec
transferToBufferedBAOS:gc.alloc.rate.norm  1024000  avgt   12     18,732 ±   3,817    B/op
transferToBufferedBAOS:gc.count            1024000  avgt   12        ~ 0            counts
transferToDataFile                            1024  avgt   12     48,778 ±   2,007   us/op
transferToDataFile:gc.alloc.rate              1024  avgt   12      7,669 ±   0,310  MB/sec
transferToDataFile:gc.alloc.rate.norm         1024  avgt   12    392,454 ±   0,018    B/op
transferToDataFile:gc.count                   1024  avgt   12      2,000            counts
transferToDataFile:gc.time                    1024  avgt   12      2,000                ms
transferToDataFile                          102400  avgt   12     75,509 ±   1,703   us/op
transferToDataFile:gc.alloc.rate            102400  avgt   12      5,404 ±   0,498  MB/sec
transferToDataFile:gc.alloc.rate.norm       102400  avgt   12    428,315 ±  37,443    B/op
transferToDataFile:gc.count                 102400  avgt   12      2,000            counts
transferToDataFile:gc.time                  102400  avgt   12      2,000                ms
transferToDataFile                         1024000  avgt   12    389,400 ±  20,381   us/op
transferToDataFile:gc.alloc.rate           1024000  avgt   12      3,035 ±   0,111  MB/sec
transferToDataFile:gc.alloc.rate.norm      1024000  avgt   12   1240,317 ±  21,035    B/op
transferToDataFile:gc.count                1024000  avgt   12        ~ 0            counts
transferToFile                                1024  avgt   12     49,936 ±   2,021   us/op
transferToFile:gc.alloc.rate                  1024  avgt   12      6,495 ±   0,760  MB/sec
transferToFile:gc.alloc.rate.norm             1024  avgt   12    340,219 ±  37,149    B/op
transferToFile:gc.count                       1024  avgt   12      2,000            counts
transferToFile:gc.time                        1024  avgt   12      2,000                ms
transferToFile                              102400  avgt   12     76,250 ±   4,523   us/op
transferToFile:gc.alloc.rate                102400  avgt   12      4,009 ±   0,223  MB/sec
transferToFile:gc.alloc.rate.norm           102400  avgt   12    320,410 ±   0,497    B/op
transferToFile:gc.count                     102400  avgt   12      2,000            counts
transferToFile:gc.time                      102400  avgt   12      2,000                ms
transferToFile                             1024000  avgt   12    553,221 ± 119,340   us/op
transferToFile:gc.alloc.rate               1024000  avgt   12      2,325 ±   0,329  MB/sec
transferToFile:gc.alloc.rate.norm          1024000  avgt   12   1328,495 ± 122,318    B/op
transferToFile:gc.count                    1024000  avgt   12        ~ 0            counts
transferToGZIP                                1024  avgt   12     19,963 ±   1,815   us/op
transferToGZIP:gc.alloc.rate                  1024  avgt   12     39,448 ±   3,517  MB/sec
transferToGZIP:gc.alloc.rate.norm             1024  avgt   12    824,184 ±   0,018    B/op
transferToGZIP:gc.count                       1024  avgt   12      2,000            counts
transferToGZIP:gc.time                        1024  avgt   12      2,000               ms
transferToGZIP                              102400  avgt   12   1786,237 ±  10,187   us/op
transferToGZIP:gc.alloc.rate                102400  avgt   12      0,574 ±   0,004  MB/sec
transferToGZIP:gc.alloc.rate.norm           102400  avgt   12   1075,544 ±   2,749    B/op
transferToGZIP:gc.count                     102400  avgt   12        ~ 0            counts
transferToGZIP                             1024000  avgt   12  19593,844 ±  55,469   us/op
transferToGZIP:gc.alloc.rate               1024000  avgt   12      1,026 ±   0,003  MB/sec
transferToGZIP:gc.alloc.rate.norm          1024000  avgt   12  21123,436 ±   2,204    B/op
transferToGZIP:gc.count                    1024000  avgt   12        ~ 0            counts 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment