rust icon indicating copy to clipboard operation
rust copied to clipboard

Optimize `Seek::stream_len` impl for `File`

Open tbu- opened this issue 1 month ago • 2 comments

It uses the file metadata on Unix with a fallback for files incorrectly reported as zero-sized. It uses GetFileSizeEx on Windows.

This reduces the number of syscalls needed for determining the file size of an open file from 3 to 1.

tbu- avatar May 13 '24 18:05 tbu-

r? @ChrisDenton

rustbot has assigned @ChrisDenton. They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

rustbot avatar May 13 '24 18:05 rustbot

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
    Checking std_detect v0.1.5 (/checkout/library/stdarch/crates/std_detect)
error[E0308]: mismatched types
   --> library/std/src/sys/pal/windows/fs.rs:506:14
    |
506 |           Some(cvt(unsafe { c::GetFileSizeEx(self.handle.as_raw_handle(), &mut result) })
    |  _________----_^
    | |         arguments to this enum variant are incorrect
    | |         arguments to this enum variant are incorrect
507 | |             .map(|_| result))
    | |____________________________^ expected `Result<u64, Error>`, found `Result<i64, Error>`
    = note: expected enum `core::result::Result<u64, _>`
               found enum `core::result::Result<i64, _>`
               found enum `core::result::Result<i64, _>`
help: the type constructed contains `core::result::Result<i64, io::error::Error>` due to the type of the argument passed
   --> library/std/src/sys/pal/windows/fs.rs:506:9
    |
506 |            Some(cvt(unsafe { c::GetFileSizeEx(self.handle.as_raw_handle(), &mut result) })
    |  __________^____-
    | | _________|
    | ||
507 | ||             .map(|_| result))
    | ||____________________________-^
    |                               this argument influences the type of `Some`
note: tuple variant defined here
   --> /checkout/library/core/src/option.rs:579:5
    |

rust-log-analyzer avatar May 13 '24 18:05 rust-log-analyzer

The job x86_64-gnu-llvm-17 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#16 exporting to docker image format
#16 sending tarball 29.8s done
#16 DONE 35.5s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-17]
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-17', '--enable-llvm-link-shared', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'change-id=99999999', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-17/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.thin-lto-import-instr-limit := 10
configure: change-id            := 99999999
---
   Compiling addr2line v0.21.0
error: function cannot return without recursing
   --> library/std/src/fs.rs:953:5
    |
953 |     fn stream_len(&mut self) -> io::Result<u64> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
954 |         (&*self).stream_len()
    |         --------------------- recursive call site
    |
    = help: a `loop` may express intention better if this is on purpose
    = note: `-D unconditional-recursion` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(unconditional_recursion)]`

error[E0596]: cannot borrow data in a `&` reference as mutable
    |
    |
954 |         (&*self).stream_len()
    |         ^^^^^^^^ cannot borrow as mutable
For more information about this error, try `rustc --explain E0596`.
error: could not compile `std` (lib) due to 2 previous errors
Build completed unsuccessfully in 0:00:15
  local time: Sun May 19 15:52:39 UTC 2024

rust-log-analyzer avatar May 19 '24 15:05 rust-log-analyzer