2 ms·
I realized that stdout was acquiring and releasing a mutex on every write operation; fixing that improved things. The good news is that when writing to /dev/nul
by usefulcat 8y ago
I realized that stdout was acquiring and releasing a mutex on every write operation; fixing that improved things. The good news is that when writing to /dev/null, it's now faster than the C++ version, but writing to stdout is still ~25% slower. I suppose there's something else suboptimal about the way I'm using stdout, but I haven't figured it out yet.
Code is here: https://gist.github.com/usefulcat/56f334bc58c97edb073b457b683a9f93 https://gist.github.com/usefulcat/56f334bc58c97edb073b457b68...
There is actually one other file but it only contains a couple of struct definitions for the libpcap file and packet headers. Thanks for having a look!
- usefulcat 8y agoUpdate: I wrapped the locked stdout handle in a BufWriter and now the rust version is >25% faster than the C++ version in all cases. I didn't think there was room for that much improvement; I'm really impressed.
- steveklabnik 8y agoAwesome! Glad you got it sorted. Those two things are always footguns...
- gnuvince 8y agoIf you have a blog, that would make for an amazing entry, going from 50% slower to 25% faster. Edit: HOLY CRAP! I have a program written in Rust, ppbert[1], and I just tried wrapping my StdoutLock object in a BufWriter, and I improved the performance of my pretty-printing by a factor of 2x! I knew to use BufReader for files, I didn't know it was helpful for stdin and stdout! Thank you _so much_ for sharing your experience, I've certainly benefited! Benchmark #1: ppbert -2 *.bert2 Time (mean ± σ): 3.816 s ± 0.115 s [User: 2.494 s, System: 1.321 s] Range (min … max): 3.688 s … 4.028 s Benchmark #2: ppbert-dev -2 *.bert2 Time (mean ± σ): 1.728 s ± 0.045 s [User: 1.493 s, System: 0.234 s] Range (min … max): 1.678 s … 1.843 s Summary 'ppbert-dev -2 *.bert2' ran 2.21x faster than 'ppbert -2 *.bert2' [1] https://github.com/gnuvince/ppbert https://github.com/gnuvince/ppbert