Skip to content

Small speedups in RecyclerBytesStreamOutput#140257

Closed
DaveCTurner wants to merge 1 commit intoelastic:mainfrom
DaveCTurner:2026/01/07/RecyclerBytesStreamOutput-writeVLong
Closed

Small speedups in RecyclerBytesStreamOutput#140257
DaveCTurner wants to merge 1 commit intoelastic:mainfrom
DaveCTurner:2026/01/07/RecyclerBytesStreamOutput-writeVLong

Conversation

@DaveCTurner
Copy link
Contributor

@DaveCTurner DaveCTurner commented Jan 7, 2026

  1. No need to compute vIntLength(i) in the very common case that
    there's more than 5 bytes of space left on the current page.

  2. No need to subtract the current offset from the return value of
    putVInt() only to add it back again: we can just return the updated
    offset directly.

  3. No need to use putVInt() when we've already handled the multibyte
    case.

  4. writeVLong() also shows up in profiling sometimes so deserves
    similar optimizations to writeVInt().

Microbenchmark results before change:

  1. As committed

     Benchmark                                      Mode  Cnt     Score     Error  Units
     RecyclerBytesStreamOutputBenchmark.writeVInt   avgt    3  1734.806 ±  45.790  ns/op
     RecyclerBytesStreamOutputBenchmark.writeVLong  avgt    3  6196.593 ± 405.058  ns/op
    
  2. With arrays of length 16kiB+1 to overflow page

     Benchmark                                      Mode  Cnt       Score      Error  Units
     RecyclerBytesStreamOutputBenchmark.writeVInt   avgt    3   71214.075 ± 6742.644  ns/op
     RecyclerBytesStreamOutputBenchmark.writeVLong  avgt    3  186032.816 ± 2546.869  ns/op
    

Microbenchmark results after change:

  1. As committed

     Benchmark                                      Mode  Cnt     Score    Error  Units
     RecyclerBytesStreamOutputBenchmark.writeVInt   avgt    3  1456.083 ±  8.507  ns/op
     RecyclerBytesStreamOutputBenchmark.writeVLong  avgt    3  1699.816 ± 77.845  ns/op
    
  2. With arrays of length 16kiB+1 to overflow page

     Benchmark                                      Mode  Cnt      Score      Error  Units
     RecyclerBytesStreamOutputBenchmark.writeVInt   avgt    3  64393.687 ± 3891.214  ns/op
     RecyclerBytesStreamOutputBenchmark.writeVLong  avgt    3  74050.944 ± 7618.977  ns/op
    

1. No need to compute `vIntLength(i)` in the very common case that
   there's more than 5 bytes of space left on the current page.

2. No need to subtract the current offset from the return value of
   `putVInt()` only to add it back again: we can just return the updated
   offset directly.

3. No need to use `putVInt()` when we've already handled the multibyte
   case.

4. `writeVLong()` also shows up in profiling sometimes so deserves
   similar optimizations to `writeVInt()`.

Microbenchmark results before change:

1. As committed

    Benchmark                                      Mode  Cnt     Score     Error  Units
    RecyclerBytesStreamOutputBenchmark.writeVInt   avgt    3  1734.806 ±  45.790  ns/op
    RecyclerBytesStreamOutputBenchmark.writeVLong  avgt    3  6196.593 ± 405.058  ns/op

2. With arrays of length 16kiB+1 to overflow page

    Benchmark                                      Mode  Cnt       Score      Error  Units
    RecyclerBytesStreamOutputBenchmark.writeVInt   avgt    3   71214.075 ± 6742.644  ns/op
    RecyclerBytesStreamOutputBenchmark.writeVLong  avgt    3  186032.816 ± 2546.869  ns/op

Microbenchmark results after change:

1. As committed

    Benchmark                                      Mode  Cnt     Score    Error  Units
    RecyclerBytesStreamOutputBenchmark.writeVInt   avgt    3  1456.083 ±  8.507  ns/op
    RecyclerBytesStreamOutputBenchmark.writeVLong  avgt    3  1699.816 ± 77.845  ns/op

2. With arrays of length 16kiB+1 to overflow page

    Benchmark                                      Mode  Cnt      Score      Error  Units
    RecyclerBytesStreamOutputBenchmark.writeVInt   avgt    3  64393.687 ± 3891.214  ns/op
    RecyclerBytesStreamOutputBenchmark.writeVLong  avgt    3  74050.944 ± 7618.977  ns/op
@DaveCTurner DaveCTurner requested a review from a team as a code owner January 7, 2026 10:45
@DaveCTurner DaveCTurner added >non-issue :Distributed/Network Http and internode communication implementations v9.4.0 labels Jan 7, 2026
@elasticsearchmachine elasticsearchmachine added the Team:Distributed Coordination (obsolete) Meta label for Distributed Coordination team. Obsolete. Please do not use. label Jan 7, 2026
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-distributed-coordination (Team:Distributed Coordination)

@DaveCTurner
Copy link
Contributor Author

Ah wait there's a bug in this (and a big gap in the tests it seems!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Distributed/Network Http and internode communication implementations >non-issue Team:Distributed Coordination (obsolete) Meta label for Distributed Coordination team. Obsolete. Please do not use. v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants