Skip to content

Commit

Permalink
Allocate space for the file before we write it
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ruuda committed Jul 12, 2024
1 parent 893509f commit cfba4cc
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,20 @@ impl FileReceiver {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir)?;
}
File::create(path)?
let file = File::create(path)?;

// Resize the file to its final size already:
// * So that the file system can do a better job of allocating
// a single extent for it, and it doesn't have to fragment
// the file.
// * If we run out of space, we learn about that before we waste
// time on the transfer (although then maybe we should do it
// before we receive a chunk after all?).
// This can make debugging a bit harder, because when you look
// at just the file size you might think it's fully transferred.
file.set_len(self.total_len)?;

file
}
Some(f) => f,
};
Expand Down

0 comments on commit cfba4cc

Please sign in to comment.