Skip to content

Conversation

fzhinkin
Copy link
Collaborator

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 fzhinkin mentioned this pull request Jun 27, 2024
7 tasks
@fzhinkin fzhinkin closed this Jul 8, 2024
@fzhinkin fzhinkin deleted the do-not-lose-shared-segments branch July 8, 2024 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant