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

Create a Chunk from an IterableOnce #2722

Merged
merged 4 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions core/shared/src/main/scala-2.13/fs2/ChunkPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ private[fs2] trait ChunkCompanionPlatform { self: Chunk.type =>
val arr = arraySeq.unsafeArray.asInstanceOf[Array[O]]
array(arr)(ClassTag[O](arr.getClass.getComponentType))
}

/** Creates a chunk from a `scala.collection.IterableOnce`. */
def iterableOnce[O](i: collection.IterableOnce[O]): Chunk[O] =
iterator(i.iterator)
}
4 changes: 4 additions & 0 deletions core/shared/src/main/scala-3/fs2/ChunkPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,8 @@ private[fs2] trait ChunkCompanionPlatform { self: Chunk.type =>
if (offset == 0 && length == values.length) values
else super.toIArray[O2]
}

/** Creates a chunk from a `scala.collection.IterableOnce`. */
def iterableOnce[O](i: collection.IterableOnce[O]): Chunk[O] =
iterator(i.iterator)
}
35 changes: 15 additions & 20 deletions core/shared/src/main/scala/fs2/Chunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -596,18 +596,22 @@ object Chunk
case ix: GIndexedSeq[O] => indexedSeq(ix)
case _ =>
if (i.isEmpty) empty
Copy link
Contributor

@nikiforo nikiforo Nov 26, 2021

Choose a reason for hiding this comment

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

emptiness gets checked in iterableOnce, can we remove this check from here and L626?

Copy link
Contributor

@nikiforo nikiforo Nov 29, 2021

Choose a reason for hiding this comment

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

Now the implementation has changed and it's an optimization. Thus, the comment is resolved.

else {
val itr = i.iterator
val head = itr.next()
if (itr.hasNext) {
val bldr = collection.mutable.Buffer.newBuilder[O]
bldr += head
bldr ++= itr
buffer(bldr.result())
} else singleton(head)
}
else iterator(i.iterator)
})

/** Creates a chunk from a `scala.collection.Iterator`. */
def iterator[O](itr: collection.Iterator[O]): Chunk[O] =
if (itr.isEmpty) empty
else {
val head = itr.next()
if (itr.hasNext) {
val bldr = collection.mutable.Buffer.newBuilder[O]
Copy link
Contributor

Choose a reason for hiding this comment

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

According to this
https://github.com/typelevel/fs2/pull/2720/files#diff-e8fdf3ccd3d2479432355b3abf34a16cd548373afeddeaf231b8d8a1147b1d48R115
ArrayBuilder shows x2 performance compared to mutable.Buffer, shouldn't we change a type of builder used here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In those cases we have the (expected) size, we don't in this case.

Copy link
Member

Choose a reason for hiding this comment

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

ArrayBuilder should still beat mutable buffer though, even without a size hint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using ArrayBuilder requires us to add a ClassTag constraint, I don't think that is something that we want?

That would mean that we couldn't use it in iterable and chain.

Copy link
Member

@mpilquist mpilquist Nov 29, 2021

Choose a reason for hiding this comment

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

Merge the main branch in to this PR and then use makeArrayBuilder[Any]. We'll have to cast the resulting chunk back to a Chunk[A] just like we do in methods like filter.

bldr += head
bldr ++= itr
buffer(bldr.result())
} else singleton(head)
}

/** Creates a chunk backed by a mutable `ArraySeq`.
*/
def arraySeq[O](arraySeq: mutable.ArraySeq[O]): Chunk[O] = {
Expand All @@ -618,16 +622,7 @@ object Chunk
/** Creates a chunk backed by a `Chain`. */
def chain[O](c: Chain[O]): Chunk[O] =
if (c.isEmpty) empty
else {
val itr = c.iterator
val head = itr.next()
if (itr.hasNext) {
val bldr = collection.mutable.Buffer.newBuilder[O]
bldr += head
bldr ++= itr
buffer(bldr.result())
} else singleton(head)
}
else iterator(c.iterator)

/** Creates a chunk backed by a mutable buffer. The underlying buffer must not be modified after
* it is passed to this function.
Expand Down