kotlinx-io icon indicating copy to clipboard operation
kotlinx-io copied to clipboard

Track precise number of shared segment copies to return the last one into a segment pool

Open fzhinkin opened this issue 1 year ago • 0 comments

There are two really nice Okio features kotlinx-io inherited:

  • copying buffers is cheap, thanks to segment cloning/sharing;
  • adding a new segment to a buffer on JVM is also cheap, thanks to segment pooling.

Unfortunately, these two features don't work well together: once a segment is shared (and that is happening not only on explicit buffer copying but also in less obvious scenarios, like using the PeekSource), it will never be returned into a pool.

The main cause is that the current "shared" state is tracked by a flag. It's easy to transfer from an unshared to a shared state, but there's no way back.

To address that issue, the shared state should be tracked using reference counting. Obviously, such precision comes with a price, but state transitions and its checks are not that common, so it should be almost transparent performance-wise.

fzhinkin avatar Jun 27 '24 12:06 fzhinkin