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#to{Byte,Char}Buffer, add note about mutability #3147

Merged
merged 3 commits into from
Feb 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions core/shared/src/main/scala/fs2/Chunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -306,30 +306,38 @@ abstract class Chunk[+O] extends Serializable with ChunkPlatform[O] with ChunkRu
case _ => Chunk.ArraySlice(toArray, 0, size)
}

/** Converts this chunk to a `java.nio.ByteBuffer`. */
/** Converts this chunk to a `java.nio.ByteBuffer`.
* @note that even "read-only" interaction with a `ByteBuffer` may increment its `position`,
* so this method should be considered as unsafely allocating mutable state.
*/
def toByteBuffer[B >: O](implicit ev: B =:= Byte): JByteBuffer =
this match {
case c: Chunk.ArraySlice[_] if c.values.isInstanceOf[Array[Byte]] =>
JByteBuffer.wrap(c.values.asInstanceOf[Array[Byte]], c.offset, c.length)
case c: Chunk.ByteBuffer =>
val b = c.buf.asReadOnlyBuffer
val b = c.buf.duplicate // share contents, independent position/limit
Comment on lines -315 to +318
Copy link
Member Author

Choose a reason for hiding this comment

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

asReadOnlyBuffer and duplicate are similar in that the returned buffer has independent position/limit.

The difference is that a read-only buffer denies access to its underlying Array, while a duplicated buffer does not. Being able to get at that array is an important optimization.

if (c.offset == 0 && b.position() == 0 && c.size == b.limit()) b
else {
(b: JBuffer).position(c.offset.toInt)
(b: JBuffer).limit(c.offset.toInt + c.size)
b
}
case c: Chunk.ByteVectorChunk =>
c.bv.toByteBuffer
case _ =>
JByteBuffer.wrap(this.asInstanceOf[Chunk[Byte]].toArray, 0, size)
}

/** Converts this chunk to a `java.nio.CharBuffer`. */
/** Converts this chunk to a `java.nio.CharBuffer`.
* @note that even "read-only" interaction with a `CharBuffer` may increment its position,
* so this method should be considered as unsafely allocating mutable state.
*/
def toCharBuffer[B >: O](implicit ev: B =:= Char): JCharBuffer =
this match {
case c: Chunk.ArraySlice[_] if c.values.isInstanceOf[Array[Char]] =>
JCharBuffer.wrap(c.values.asInstanceOf[Array[Char]], c.offset, c.length)
case c: Chunk.CharBuffer =>
val b = c.buf.asReadOnlyBuffer
val b = c.buf.duplicate // share contents, independent position/limit
if (c.offset == 0 && b.position() == 0 && c.size == b.limit()) b
else {
(b: JBuffer).position(c.offset.toInt)
Expand Down