sharkdp / hexyl

A command-line hex viewer
Apache License 2.0
9.07k stars 227 forks source link

Write lines into an in-memory buffer before printing to stdout #33

Closed lilyball closed 5 years ago

lilyball commented 5 years ago

Running them through an in-memory buffer first produces a 25% speedup (when tested against a 1.3MB file).

Also bump the buffer size to 256 bytes. On my machine this pushes it to a 33% speedup.

Benchmark #1: ./hexyl-0.3.1 ./hexyl-0.3.1
  Time (mean ± σ):     277.7 ms ±   1.3 ms    [User: 230.7 ms, System: 45.2 ms]
  Range (min … max):   275.9 ms … 280.8 ms

Benchmark #2: target/release/hexyl ./hexyl-0.3.1
  Time (mean ± σ):     184.2 ms ±   1.4 ms    [User: 151.7 ms, System: 30.9 ms]
  Range (min … max):   181.4 ms … 186.0 ms

Summary
  'target/release/hexyl ./hexyl-0.3.1' ran
    1.51 ± 0.01 times faster than './hexyl-0.3.1 ./hexyl-0.3.1'
lilyball commented 5 years ago

StdoutLock is actually a wrapper around a line-buffered writer already, but for some reason it's faster to do this. I guess the line-buffered writer is still doing a bunch of work per-write.

Unfortunately I don't see any way to get a non-buffered writer short of dropping down to RawFd/RawHandle.

sharkdp commented 5 years ago

Thank you very much!