Skip to content

Commit cfba4cc

Browse files
committed
Allocate space for the file before we write it
Not sure if this is a good idea in the end or not, it harms debugging but creates cleaner files, which on SSDs probably does not matter but on spinning disks it can. But we use SSDs, so I am totally guilty of optimizing prematurely here.
1 parent 893509f commit cfba4cc

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/main.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,20 @@ impl FileReceiver {
314314
if let Some(dir) = path.parent() {
315315
std::fs::create_dir_all(dir)?;
316316
}
317-
File::create(path)?
317+
let file = File::create(path)?;
318+
319+
// Resize the file to its final size already:
320+
// * So that the file system can do a better job of allocating
321+
// a single extent for it, and it doesn't have to fragment
322+
// the file.
323+
// * If we run out of space, we learn about that before we waste
324+
// time on the transfer (although then maybe we should do it
325+
// before we receive a chunk after all?).
326+
// This can make debugging a bit harder, because when you look
327+
// at just the file size you might think it's fully transferred.
328+
file.set_len(self.total_len)?;
329+
330+
file
318331
}
319332
Some(f) => f,
320333
};

0 commit comments

Comments
 (0)