Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(virtual-fs): Fix horrible write performance for in-memory files #3905

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 61 additions & 29 deletions lib/virtual-fs/src/mem_fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,39 +1304,22 @@ impl File {

impl File {
pub fn write(&mut self, buf: &[u8], cursor: &mut u64) -> io::Result<usize> {
match *cursor {
// The cursor is at the end of the buffer: happy path!
position if position == self.buffer.len() as u64 => {
self.buffer.extend_from_slice(buf)?;
}

// The cursor is at the beginning of the buffer (and the
// buffer is not empty, otherwise it would have been
// caught by the previous arm): almost a happy path!
0 => {
// FIXME(perf,theduke): make this faster, it's horrible!
let mut new_buffer = TrackedVec::with_capacity(
self.buffer.len() + buf.len(),
self.buffer.limiter().cloned(),
)?;
new_buffer.extend_from_slice(buf)?;
new_buffer.append(&mut self.buffer)?;

self.buffer = new_buffer;
}
if buf.is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the file by truncated in that case, depending on the write mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buf is only the newly written data.

return Ok(0);
}

// The cursor is somewhere in the buffer: not the happy path.
position => {
self.buffer.reserve_exact(buf.len())?;
// Extend the data buffer if necessary.
let start_cursor: usize = (*cursor) as usize;
let end_cursor = start_cursor + buf.len();

// FIXME(perf,theduke): make this faster, it's horrible!
let mut remainder = self.buffer.split_off(position as usize)?;
self.buffer.extend_from_slice(buf)?;
self.buffer.append(&mut remainder)?;
}
if end_cursor > self.buffer.len() {
self.buffer.resize(end_cursor, 0)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't the file (well the vec) be srinked if it's smaller?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why it's inside an if check.
This only runs when the new data makes the file larger than it previously was.

}

*cursor += buf.len() as u64;
let target = &mut self.buffer.as_mut_slice()[start_cursor..end_cursor];
target.copy_from_slice(buf);

*cursor = end_cursor as u64;

Ok(buf.len())
}
Expand Down Expand Up @@ -1399,3 +1382,52 @@ impl ReadOnlyFile {
Ok(())
}
}

#[cfg(test)]
mod tests {
use std::ops::Deref;

use super::*;

#[test]
fn test_memfs_file() {
let mut file = File::new(None);

let mut cursor = 0;

file.write(b"hello", &mut cursor).unwrap();
assert_eq!(file.buffer.deref(), b"hello");
assert_eq!(cursor, 5);

file.write(b" ", &mut cursor).unwrap();
assert_eq!(file.buffer.deref(), b"hello ");
assert_eq!(cursor, 6);

file.write(b"world", &mut cursor).unwrap();
assert_eq!(file.buffer.deref(), b"hello world");
assert_eq!(cursor, 11);

cursor = 0;
file.write(b"alpha", &mut cursor).unwrap();
assert_eq!(file.buffer.deref(), b"alpha world");
assert_eq!(cursor, 5);

cursor = 2;
file.write(b"ter", &mut cursor).unwrap();
assert_eq!(file.buffer.deref(), b"alter world");
assert_eq!(cursor, 5);

cursor = 6;
file.write(b"means", &mut cursor).unwrap();
assert_eq!(file.buffer.deref(), b"alter means");
assert_eq!(cursor, 11);

cursor = 6;
file.write(b"the universe!", &mut cursor).unwrap();
assert_eq!(file.buffer.deref(), b"alter the universe!");
assert_eq!(cursor, 19);

file.write(b" mkay?", &mut cursor).unwrap();
assert_eq!(file.buffer.deref(), b"alter the universe! mkay?");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a test with a seek and truncate could be interesting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely.

We should have a proper test suite for trait FileSystem implementations.

I only added a tests for write() because that's the only thing I changed.

}