-
Notifications
You must be signed in to change notification settings - Fork 825
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
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)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's why it's inside an |
||
} | ||
|
||
*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()) | ||
} | ||
|
@@ -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?"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a test with a seek and truncate could be interesting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely. We should have a proper test suite for I only added a tests for |
||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.