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

Further optimise zio.http.Body.FileBody.asStream code #3234

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Changes from 1 commit
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
46 changes: 28 additions & 18 deletions zio-http/shared/src/main/scala/zio/http/Body.scala
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ object Body {
override def asChunk(implicit trace: Trace): Task[Chunk[Byte]] = zioEmptyChunk

override def asStream(implicit trace: Trace): ZStream[Any, Throwable, Byte] = ZStream.empty
override def isComplete: Boolean = true

override def isComplete: Boolean = true

override def isEmpty: Boolean = true

Expand All @@ -471,7 +472,8 @@ object Body {
override private[zio] def unsafeAsArray(implicit unsafe: Unsafe): Array[Byte] = Array.empty[Byte]

override def contentType(newContentType: Body.ContentType): Body = this
override def contentType: Option[Body.ContentType] = None

override def contentType: Option[Body.ContentType] = None

override def knownContentLength: Option[Long] = Some(0L)
}
Expand Down Expand Up @@ -568,27 +570,35 @@ object Body {
override def asStream(implicit trace: Trace): ZStream[Any, Throwable, Byte] =
ZStream.unwrap {
ZIO.blocking {
for {
r <- ZIO.attempt {
ZIO.suspendSucceed {
try {
val fs = new FileInputStream(file)
val size = Math.min(chunkSize.toLong, file.length()).toInt

(fs, size)
}
(fs, size) = r
} yield ZStream
.repeatZIOOption[Any, Throwable, Chunk[Byte]] {
Copy link
Member Author

@guizmaii guizmaii Dec 5, 2024

Choose a reason for hiding this comment

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

Replacing ZStream.repeatZIOOption with ZStream.repeatZIOChunkOption allows us to remove the .flattenChunks

Anyway, repeatZIOOption is using repeatZIOChunkOption underneath:

Screenshot 2024-12-06 at 12 19 16 AM

for {
buffer <- ZIO.succeed(new Array[Byte](size))
len <- ZIO.attempt(fs.read(buffer)).mapError(Some(_))
bytes <-
if (len > 0) ZIO.succeed(Chunk.fromArray(buffer.slice(0, len)))
else ZIO.fail(None)
} yield bytes
val read: Task[Option[Chunk[Byte]]] =
ZIO.suspendSucceed {
try {
val buffer = new Array[Byte](size)
val len = fs.read(buffer)
if (len > 0) Exit.succeed(Some(Chunk.fromArray(buffer.slice(0, len))))
else Exit.none
} catch {
case e: Throwable => Exit.fail(e)
}
}

Exit.succeed {
// Optimised for our needs version of `ZIO.repeatZIOChunkOption`
ZStream
.unfoldChunkZIO(read)(_.map(_.map(_ -> read)))
Copy link
Member Author

@guizmaii guizmaii Dec 5, 2024

Choose a reason for hiding this comment

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

@kyri-petrou Funnily enough, if we were managing to find a way to write a recursive type in Scala,

  1. we wouldn't need this ugly (_.map(_.map(_ -> read))
  2. we could write this:
              type Read = Task[Option[(Chunk[Byte], Read)]]
              val read: Read =
                ZIO.suspendSucceed {
                  try {
                    val buffer = new Array[Byte](size)
                    val len    = fs.read(buffer)
                    if (len > 0) Exit.succeed(Some(Chunk.fromArray(buffer.slice(0, len) -> read)))
                    else Exit.none
                  } catch {
                    case e: Throwable => Exit.fail(e)
                  }
                }

              Exit.succeed {
                ZStream
                  .unfoldChunkZIO(read)(ZIO.identityFn)
                  .ensuring(ZIO.attempt(fs.close()).ignoreLogged)
              }

😍

Copy link
Contributor

Choose a reason for hiding this comment

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

I can imagine, that there is a nice way to do this for Scala 3. But I think we are unable to find a nice way for Scala 2

.ensuring(ZIO.attempt(fs.close()).ignoreLogged)
}
} catch {
case e: Throwable => Exit.fail(e)
}
.ensuring(ZIO.attempt(fs.close()).ignoreLogged)
}
}
}.flattenChunks
}

override def contentType(newContentType: Body.ContentType): Body = copy(contentType = Some(newContentType))

Expand Down
Loading