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

Conversation

guizmaii
Copy link
Member

@guizmaii guizmaii commented Dec 5, 2024

Following #3215

@guizmaii guizmaii force-pushed the optimise_asStream_2 branch 4 times, most recently from ae26d2b to d9c9624 Compare December 5, 2024 13:15
@guizmaii guizmaii marked this pull request as draft December 5, 2024 13:16
@guizmaii guizmaii requested a review from kyri-petrou December 5, 2024 13:16
@guizmaii guizmaii force-pushed the optimise_asStream_2 branch 2 times, most recently from e439994 to 8c69633 Compare December 5, 2024 13:17
}
(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

@guizmaii guizmaii marked this pull request as ready for review December 5, 2024 13:20
} yield bytes
Exit.succeed {
ZStream.repeatZIOChunkOption {
ZIO.suspendSucceed {
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 Are we sure this ZIO.suspendSucceed and the ZIO.attempt(fs.close()) will be run on a blocking thread? 🤔

@guizmaii guizmaii force-pushed the optimise_asStream_2 branch 3 times, most recently from f7a737c to 96653a6 Compare December 5, 2024 13:53
@guizmaii guizmaii force-pushed the optimise_asStream_2 branch from 96653a6 to ab1fc3d Compare December 5, 2024 13:54
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

@guizmaii guizmaii self-assigned this Dec 5, 2024
@987Nabil 987Nabil merged commit 265f503 into main Dec 14, 2024
67 checks passed
@987Nabil 987Nabil deleted the optimise_asStream_2 branch December 14, 2024 17:56
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.

2 participants