vimpunk / cratetorrent

A BitTorrent V1 engine library for Rust (and currently Linux)
473 stars 35 forks source link

Profile: is coalescing buffers or multiple writes faster? #98

Open vimpunk opened 3 years ago

vimpunk commented 3 years ago

https://github.com/mandreyel/cratetorrent/pull/95 introduced a fallback for disk writes on other platforms. It currently uses a less efficient approach than the Linux positional vectored IO APIs as they are not present on other platforms.

There were two options for the fallback, quoting directly from the implementation:

                // 1. Coalesce the slices to a continuous preallocated memory buffer and
                //    write it at once.
                // 2. Iterate through the slices and issue a separate write call for each.
                //
                // The first option implies preallocating a short-living buffer
                // and destroying it shortly after. The second option
                // implies issuing a big number of writes, and possibly disk I/O operations.

We should benchmark which is actually faster at some point.

ExceptionallyHandsome commented 3 years ago

Any time disk I/O perf is considered, OS caching/buffering must be taken into account.

As things stand with #95 , we use a user-space buffer, and then the OS does its own buffering. I think this double-buffer makes the data transfer slower than it might have been. So yeah, careful and ample benchmarking should become our mantra eventually, but for now it can wait.

In fact, we have not two, but three options to proceed:

Also, user-space buffering has its own optimizations to apply...