Skip to content

Commit

Permalink
Use Chunk.single rather than Chunk.apply [series/2.x] (#8719)
Browse files Browse the repository at this point in the history
* Use Chunk.single rather than Chunk.apply

# Conflicts:
#	core/shared/src/main/scala/zio/internal/FiberRuntime.scala

* Fix

* Improve

* Fix NonEmptyChunk.single

* Optimize fromIterable

* Fix

* Revert "Fix"

This reverts commit 313a859.

* Revert "Optimize fromIterable"

This reverts commit 0b4afbe.

* Optimize Chunk.apply
  • Loading branch information
ghostdogpr committed Apr 11, 2024
1 parent 37b8b6d commit 53c4d2c
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/Chunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ object Chunk extends ChunkFactory with ChunkPlatformSpecific {
* Returns a chunk from a number of values.
*/
override def apply[A](as: A*): Chunk[A] =
fromIterable(as)
if (as.size == 1) single(as.head) else fromIterable(as)

/*
* Performs bitwise operations on boolean chunks returning a Chunk.BitChunk
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/Hub.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ object Hub {
strategy.unsafeCompleteSubscribers(hub, subscribers)
ZIO.succeed(true)
} else {
strategy.handleSurplus(hub, subscribers, Chunk(a), shutdownFlag)
strategy.handleSurplus(hub, subscribers, Chunk.single(a), shutdownFlag)
}
}
def publishAll[A1 <: A](as: Iterable[A1])(implicit trace: Trace): UIO[Chunk[A1]] =
Expand Down
14 changes: 11 additions & 3 deletions core/shared/src/main/scala/zio/NonEmptyChunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ object NonEmptyChunk {
* Constructs a `NonEmptyChunk` from one or more values.
*/
def apply[A](a: A, as: A*): NonEmptyChunk[A] =
nonEmpty(Chunk(a) ++ Chunk.fromIterable(as))
fromIterable(a, as)

/**
* Checks if a `chunk` is not empty and constructs a `NonEmptyChunk` from it.
Expand All @@ -304,7 +304,15 @@ object NonEmptyChunk {
* Constructs a `NonEmptyChunk` from an `Iterable`.
*/
def fromIterable[A](a: A, as: Iterable[A]): NonEmptyChunk[A] =
nonEmpty(Chunk.single(a) ++ Chunk.fromIterable(as))
if (as.isEmpty) single(a)
else
nonEmpty {
val builder = ChunkBuilder.make[A]()
builder.sizeHint(as, 1)
builder += a
builder ++= as
builder.result()
}

/**
* Constructs a `NonEmptyChunk` from an `Iterable` or `None` otherwise.
Expand All @@ -316,7 +324,7 @@ object NonEmptyChunk {
* Constructs a `NonEmptyChunk` from a single value.
*/
def single[A](a: A): NonEmptyChunk[A] =
NonEmptyChunk(a)
nonEmpty(Chunk.single(a))

/**
* Extracts the elements from a `Chunk`.
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/Queue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ object Queue {
if (succeeded)
ZIO.succeed(true)
else
strategy.handleSurplus(Chunk(a), queue, takers, shutdownFlag)
strategy.handleSurplus(Chunk.single(a), queue, takers, shutdownFlag)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/internal/FiberRuntime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ final class FiberRuntime[E, A](fiberId: FiberId.Runtime, fiberRefs0: FiberRefs,

def interruptAsFork(fiberId: FiberId)(implicit trace: Trace): UIO[Unit] =
ZIO.succeed {
val cause = Cause.interrupt(fiberId).traced(StackTrace(self.fiberId, Chunk(trace)))
val cause = Cause.interrupt(fiberId).traced(StackTrace(self.fiberId, Chunk.single(trace)))

tell(FiberMessage.InterruptSignal(cause))
}
Expand Down
4 changes: 2 additions & 2 deletions streams/shared/src/main/scala/zio/stream/ZPipeline.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2036,10 +2036,10 @@ object ZPipeline extends ZPipelinePlatformSpecificConstructors {
},
err =>
if (stringBuilder.isEmpty) ZChannel.refailCause(err)
else ZChannel.write(Chunk(stringBuilder.result)) *> ZChannel.refailCause(err),
else ZChannel.write(Chunk.single(stringBuilder.result)) *> ZChannel.refailCause(err),
done =>
if (stringBuilder.isEmpty) ZChannel.succeed(done)
else ZChannel.write(Chunk(stringBuilder.result)) *> ZChannel.succeed(done)
else ZChannel.write(Chunk.single(stringBuilder.result)) *> ZChannel.succeed(done)
)

new ZPipeline(loop)
Expand Down
14 changes: 7 additions & 7 deletions streams/shared/src/main/scala/zio/stream/ZStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ final class ZStream[-R, +E, +A] private (val channel: ZChannel[R, Any, Any, Any,
consumed <- consumed.get
sinkFiber <- forkSink
scheduleFiber <- timeout(Some(b)).forkIn(scope)
toWrite = c.fold[Chunk[Either[C, B]]](Chunk(Right(b)))(c => Chunk(Right(b), Left(c)))
toWrite = c.fold[Chunk[Either[C, B]]](Chunk.single(Right(b)))(c => Chunk(Right(b), Left(c)))
} yield
if (consumed)
ZChannel
Expand All @@ -271,7 +271,7 @@ final class ZStream[-R, +E, +A] private (val channel: ZChannel[R, Any, Any, Any,
case UpstreamEnd =>
ZChannel.unwrap {
consumed.get.map { p =>
if (p) ZChannel.write(Chunk(Right(b)))
if (p) ZChannel.write(Chunk.single(Right(b)))
else ZChannel.unit
}
}
Expand Down Expand Up @@ -2993,7 +2993,7 @@ final class ZStream[-R, +E, +A] private (val channel: ZChannel[R, Any, Any, Any,
driver.last.orDie.map { b =>
ZChannel.write(Chunk(f(a), g(b))) *> loop(driver, chunkIterator, index + 1)
} <* driver.reset,
_ => ZIO.succeed(ZChannel.write(Chunk(f(a))) *> loop(driver, chunkIterator, index + 1))
_ => ZIO.succeed(ZChannel.write(Chunk.single(f(a))) *> loop(driver, chunkIterator, index + 1))
)
}
else
Expand Down Expand Up @@ -4645,7 +4645,7 @@ object ZStream extends ZStreamPlatformSpecificConstructors {
case Some(e) => ZChannel.fail(e)
case None => ZChannel.unit
},
a => ZChannel.write(Chunk(a))
a => ZChannel.write(Chunk.single(a))
)
)
)
Expand Down Expand Up @@ -5755,7 +5755,7 @@ object ZStream extends ZStreamPlatformSpecificConstructors {
* terminates with the specified cause if this `Exit` is a `Failure`.
*/
def done(exit: Exit[E, A])(implicit trace: Trace): B =
apply(ZIO.done(exit.mapBothExit(e => Some(e), a => Chunk(a))))
apply(ZIO.done(exit.mapBothExit(e => Some(e), a => Chunk.single(a))))

/**
* Terminates with an end of stream signal.
Expand All @@ -5774,7 +5774,7 @@ object ZStream extends ZStreamPlatformSpecificConstructors {
* with the failure value of this effect.
*/
def fromEffect(zio: ZIO[R, E, A])(implicit trace: Trace): B =
apply(zio.mapBoth(e => Some(e), a => Chunk(a)))
apply(zio.mapBoth(e => Some(e), a => Chunk.single(a)))

/**
* Either emits the success value of this effect or terminates the stream
Expand All @@ -5793,7 +5793,7 @@ object ZStream extends ZStreamPlatformSpecificConstructors {
* Emits a chunk containing the specified value.
*/
def single(a: A)(implicit trace: Trace): B =
apply(ZIO.succeed(Chunk(a)))
apply(ZIO.succeed(Chunk.single(a)))
}

/**
Expand Down

0 comments on commit 53c4d2c

Please sign in to comment.