scala fs2 流中的理想块在生产中获得性能提升

Ideal chunk in scala fs2 stream performance gain in production

想知道 scala fs2 stream 中块大小的增加是否会带来性能提升?

    import cats.effect.{IO, Sync}
import fs2.{io, text}
import java.nio.file.Paths

def fahrenheitToCelsius(f: Double): Double =
  (f - 32.0) * (5.0/9.0)

def converter[F[_]](implicit F: Sync[F]): F[Unit] =
  io.file.readAll[F](Paths.get("testdata/fahrenheit.txt"), 4096)
    .through(text.utf8Decode)
    .through(text.lines)
    .filter(s => !s.trim.isEmpty && !s.startsWith("//"))
    .map(line => fahrenheitToCelsius(line.toDouble).toString)
    .intersperse("\n")
    .through(text.utf8Encode)
    .through(io.file.writeAll(Paths.get("testdata/celsius.txt")))
    .compile.drain

// at the end of the universe...
val u: Unit = converter[IO].unsafeRunSync()

这个chunk size就是你从文件系统读取文件内容时使用的buffer的大小。所以你的问题等同于 "will increasing buffer size when reading the file give performance gain?".

这个问题 OS/hardware 具体。大多数情况下的简短答案 - 4K 就足够了。