Skip to content

Commit

Permalink
Further optimise zio.http.Body.FileBody.asStream code
Browse files Browse the repository at this point in the history
Following #3215
  • Loading branch information
guizmaii committed Dec 5, 2024
1 parent 1f8ef1f commit bcf6033
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions zio-http/shared/src/main/scala/zio/http/Body.scala
Original file line number Diff line number Diff line change
Expand Up @@ -578,13 +578,18 @@ object Body {
(fs, size) = r
} yield ZStream
.repeatZIOOption[Any, Throwable, Chunk[Byte]] {
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
ZIO.attempt {
val buffer = new Array[Byte](size)
val len = fs.read(buffer)
if (len > 0) Some(Chunk.fromArray(buffer.slice(0, len)))
else None
}.foldZIO(
failure = e => Exit.fail(Some(e)),
success = {
case Some(bytes) => Exit.succeed(bytes)
case None => Exit.failNone
}
)
}
.ensuring(ZIO.attempt(fs.close()).ignoreLogged)
}
Expand Down

4 comments on commit bcf6033

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

🚀 : Performance Benchmarks (SimpleEffectBenchmarkServer)

concurrency: 256
requests/sec: 340909

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

🚀 : Performance Benchmarks (PlainTextBenchmarkServer)

concurrency: 256
requests/sec: 349565

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

🚀 : Performance Benchmarks (PlainTextBenchmarkServer)

concurrency: 256
requests/sec: 362330

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

🚀 : Performance Benchmarks (SimpleEffectBenchmarkServer)

concurrency: 256
requests/sec: 352544

Please sign in to comment.