Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Chunk#toByteVector when backed by a ByteBuffer #2941

Merged

Conversation

armanbilge
Copy link
Member

@armanbilge armanbilge commented Jul 10, 2022

When a Chunk is backed by ByteBuffer it is not necessary to copy the bytes to convert it to a ByteVector. This PR implements the conversion by creating a ByteVector "view" that directly wraps the chunk's underlying ByteBuffer.

@diesalbla
Copy link
Contributor

@armanbilge Can you give a quick summary of what would be the effect of this change?

@armanbilge armanbilge changed the title Optimize toByteVector when backed by a ByteBuffer Optimize Chunk#toByteVector when backed by a ByteBuffer Jul 11, 2022
case c: Chunk.ByteBuffer =>
// if c is a view, position/limit of underlying buffer may have changed
val bb = c.buf.asReadOnlyBuffer()
bb.position(c.offset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these need to take in to account the original position and limit as well? E.g. bb.position(bb.position + c.offset)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. bb.position() is not constant and bb.position(i) is not relative as far as I can make out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay got it, when we create a Chunk.ByteBuffer, we copy the position & limit in to offset & length.

@mpilquist mpilquist merged commit 445d891 into typelevel:main Jul 28, 2022
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.

3 participants