Closed NobodyXu closed 2 years ago
For thread-safe spsc, I found:
Both provides a sync interface but should be trivial to adapt them in async by using tokio::sync::Notify
.
Edit:
Maybe this will be proved to be a premature optimization, so perhaps we should try the 1st TODO to adjust size of mpsc::channel
before trying SPSC.
I am thinking about running the downloader in the same blocking thread as the extracter using tokio::runtime::Handle
.
This would have the following advantage:
mpsc
channel, which:
mpsc
.mpsc
channel, which avoid potentially OOM situation.The disadvantages would be that the downloader can no longer be run in parallel to the extracter.
If the bottleneck is the decompressor, then the downloader should also pause and wait for the decompressor to consume the data.
But if the bottleneck is the network, then that might be an issue.
@passcod What's your thought on this?
Alternatively, we can do:
mpsc
channel to a very small number, e.g. 4.tokio::spawn
the downloader so that it can be run in parallel with the extracter.tokio::task::block_in_place
to run the extracter on the core thread so that we don't have to spawn another thread.tokio::runtime::Handle
to poll tokio::sync::mpsc::Receiver::recv
so that the core thread can still run async tasks if the data is not yet ready.Compared to the previous approach I proposed, this preserve the parallelism between the downloader and the extracter, while reduce the internal buffer of mpsc
and make it possible for the downloader and extracter to be run on the same thread (though not guaranteed).
A summary of what could be/need to be improved:
Adjust the size of theCompletely remove thempsc::channel
to keep the extracter busy while avoiding reading in too much (OOM) and gives feedback to the server. Fix could be dynamically decide the buffer based onfmt: PkgFmt
since some extracters (PkgFmg::Bin
andPkgFmt::Zip
) do less work on the critical path and need bigger buffer size.mpsc
channel, runs the downloader and extracter on the same thread. #180Exploring other channels, like single-producer single-consumer thread-safe fixed-size channel, since it is likely to be much faster than mpsc which requires synchronization between producers.Most likely to be premature optimization.The aforementioned of improvement "theWe should request for the feature in upstream instead of coming up with homebrew solutionZipArchiver
can read bytes out of the file while the bytes are being appended into the file."https://github.com/zip-rs/zip/issues/16https://github.com/zip-rs/zip/issues/314extract_archive_stream
: Create a separate API forPkgFmt::Bin
. https://github.com/ryankurte/cargo-binstall/pull/180