Many download managers that use several connections save each connection's data to its own temporary file, then glue the pieces together at the end. It is easy to write and easy to reason about. It also has three costs that users feel.
The three costs of merging
- Double disk space. For a moment, the parts and the joined file exist side by side. A 60 GB download needs 120 GB free.
- A slow last minute. The progress bar says 100%, then the app spends another minute copying data it already has.
- A fragile finish. If the computer sleeps or the disk fills during the merge, the download can fail after every byte has arrived.
Writing in place
Demonic Downloader creates the final file at its full size before the first byte arrives. Each range knows its offset and writes there directly. When the last range finishes, the file is complete. There is nothing to merge.
file: 6,291,456,000 bytes (allocated up front)
range 1 [0 .. 1,048,575,999] → offset 0
range 2 [1,048,576,000 .. 2,097,151,999] → offset 1,048,576,000
…
Remembering where you were
Writing in place means the file alone cannot tell you which parts are real data and which are still empty. So next to every download sits a small journal: for each range, the last offset that was safely flushed to disk. The journal is updated every few seconds and after every range finishes. If the power goes, the worst case is re-downloading a few megabytes.
Stealing work
Ranges never finish at the same moment. Without help, the last minute of a big download is one lonely connection. When a range finishes early, it looks at the slowest range still running and takes over the second half of what it has left. The browser demo on our try page shows the simple version of this idea: a file split into ranges, fetched in parallel, verified with SHA-256 at the end.
What it costs us
Pre-allocating needs filesystem support for sparse or fast allocation, which differs between NTFS, APFS, ext4 and Android's storage. That took longer to get right than the rest of the engine combined. We think it was worth it.