cratetorrent
cratetorrent copied to clipboard
Make cross-platform
There's really no need to make cratetorrent Linux-only. The only places that rely on platform specific APIs are:
- https://github.com/mandreyel/cratetorrent/blob/master/cratetorrent/src/disk/io/file.rs#L83
- https://github.com/mandreyel/cratetorrent/blob/master/cratetorrent/src/disk/io/file.rs#L141
- and uses of
/tmp
in tests an examples.
The real issue is the first two. The solution is very simple: feature gate those methods of TorrentFile
behind a Linux flag, and fallback to coalescing buffers and issuing a single write call for other platforms.
[update]: It may actually be better to create a fallback of the Piece::write
method instead of the lower level TorrentFile::write
method.
This is because the current piece write method allocates a temporary buffer of iovecs, which is only needed for the pwritev
syscall. However, if we're coalescing buffers and issuing a single write
, we can skip the iovecs allocation step and save an allocation and other additional logic. A little more work for better performance.
I presume something resembling preadv
could be built on top of Overlapped IO on Windows, but I'm not sure it would worth it.
That could be a future optimization for sure, but I think for now something simpler is enough. Besides, that solution would not fix incompatibility with mac/darwin/bsd, so someting more generic that is easy to maintain is better for now.
Actually, Write::write_vectored
should be able to replace it entirely. I'll send you a PR tomorrow.