kotlinx-io
kotlinx-io copied to clipboard
Track precise number of shared segment copies to return the last one into a segment pool
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.