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

Unnecessary lseek syscall when using std::fs::read #106597

Closed
bjorn3 opened this issue Jan 8, 2023 · 1 comment · Fixed by #106664
Closed

Unnecessary lseek syscall when using std::fs::read #106597

bjorn3 opened this issue Jan 8, 2023 · 1 comment · Fixed by #106664
Assignees
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@bjorn3
Copy link
Member

bjorn3 commented Jan 8, 2023

fn main() {
    let _ = std::fs::read("test.rs");
}

gives the following strace output:

[...]
openat(AT_FDCWD, "test.rs", O_RDONLY|O_CLOEXEC) = 3
statx(0, NULL, AT_STATX_SYNC_AS_STAT, STATX_ALL, NULL) = -1 EFAULT (Ongeldig adres)
statx(3, "", AT_STATX_SYNC_AS_STAT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_ALL|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=52, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
read(3, "fn main() {\n    let _ = std::fs:"..., 52) = 52
read(3, "", 32)                         = 0
close(3)                                = 0
[...]

The lseek call is unnecessary as we know for a fact that we are at the start of the file and thus the result is always 0. It accounts for about 14% of the time when stracing. This is likely inflated due to syscall overhead when using strace, but still it may be a good idea to remove this syscall.

@bjorn3 bjorn3 added the I-slow Issue: Problems and improvements with respect to performance of generated code. label Jan 8, 2023
@chenyukang
Copy link
Member

@rustbot claim

Noratrieb added a commit to Noratrieb/rust that referenced this issue Jan 11, 2023
…e-lseek, r=cuviper

Remove unnecessary lseek syscall when using std::fs::read

Fixes rust-lang#106597
r? ``@bjorn3``
@bors bors closed this as completed in 9a820f7 Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants