-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Remove unnecessary lseek syscall when using std::fs::read #106664
Remove unnecessary lseek syscall when using std::fs::read #106664
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
The result from openat(AT_FDCWD, "./p/f.rs", O_RDONLY|O_CLOEXEC) = 3
statx(0, NULL, AT_STATX_SYNC_AS_STAT, STATX_ALL, NULL) = -1 EFAULT (Bad address)
statx(3, "", AT_STATX_SYNC_AS_STAT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_ALL|0x1000, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=88, ...}) = 0
read(3, "fn main() {\n let res = std::f"..., 88) = 88
read(3, "", 32) = 0
close(3) |
|
I'd suggest modifying |
Make sense. |
eea7dfb
to
eae615d
Compare
r? rust-lang/libs |
Thanks! I don't think I am a suitable reviewer, so I re-assigned it to someone on the libs team. |
library/std/src/fs.rs
Outdated
let mut bytes = Vec::new(); | ||
file.read_to_end(&mut bytes)?; | ||
let size = file.metadata().map(|m| m.len()).unwrap_or(0); | ||
bytes.reserve(size as usize); |
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.
You can do let mut bytes = Vec::with_capacity(size as usize)
instead of two lines reserve
and Vec::new()
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.
Yes, I thought on this, but I have a dig on the implementation of with_capacity
and reserve
, seems it's not simply share the same code path, so I'm not 100% sure is there any flaw to use with_capacity
here.
I just have a simple bench test, seems with_capacity
perform better:
cat@LAPTOP-V6U0QKD4:~/code/play/bench$ cargo bench
Finished bench [optimized] target(s) in 0.00s
Running unittests src/main.rs (target/release/deps/bench-b73bcfb176559323)
running 2 tests
test tests::bench_reserve_capacity ... bench: 677,683 ns/iter (+/- 828,000)
test tests::bench_with_capacity ... bench: 469,924 ns/iter (+/- 319,682)
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured; 0 filtered out; finished in 10.56s
cat@LAPTOP-V6U0QKD4:~/code/play/bench$ cargo bench
Finished bench [optimized] target(s) in 0.01s
Running unittests src/main.rs (target/release/deps/bench-b73bcfb176559323)
running 2 tests
test tests::bench_reserve_capacity ... bench: 645,487 ns/iter (+/- 347,440)
test tests::bench_with_capacity ... bench: 415,772 ns/iter (+/- 162,321)
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured; 0 filtered out; finished in 10.09s
with bench code:
#![feature(test)]
extern crate test;
pub fn test_with_capacity(size: usize) {
let mut v: Vec<i32> = Vec::with_capacity(size);
v.push(3);
assert_eq!(v.len(), 1);
}
pub fn test_reserve_capacity(size: usize) {
let mut v: Vec<i32> = Vec::new();
v.reserve(size);
v.push(3);
assert_eq!(v.len(), 1);
}
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_with_capacity(b: &mut Bencher) {
b.iter(|| {
for s in 0..10000 {
test_with_capacity(s);
}
});
}
#[bench]
fn bench_reserve_capacity(b: &mut Bencher) {
b.iter(|| {
for s in 0..10000 {
test_reserve_capacity(s);
}
});
}
}
library/std/src/fs.rs
Outdated
file.read_to_string(&mut string)?; | ||
let size = file.metadata().map(|m| m.len()).unwrap_or(0); | ||
string.reserve(size as usize); |
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.
Same here.
@bors r+ |
🌲 The tree is currently closed for pull requests below priority 999. This pull request will be tested once the tree is reopened. |
…e-lseek, r=cuviper Remove unnecessary lseek syscall when using std::fs::read Fixes rust-lang#106597 r? ``@bjorn3``
Rollup of 9 pull requests Successful merges: - rust-lang#106321 (Collect and emit proper backtraces for `delay_span_bug`s) - rust-lang#106397 (Check `impl`'s `where` clauses in `consider_impl_candidate` in experimental solver) - rust-lang#106427 (Improve fluent error messages) - rust-lang#106570 (add tests for div_duration_* functions) - rust-lang#106648 (Polymorphization cleanup) - rust-lang#106664 (Remove unnecessary lseek syscall when using std::fs::read) - rust-lang#106709 (Disable "split dwarf inlining" by default.) - rust-lang#106715 (Autolabel and ping wg for changes to new solver) - rust-lang#106717 (fix typo LocalItemId -> ItemLocalId) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Fixes #106597
r? @bjorn3