gpt4 book ai didi

rust - 为什么 BufReader 实际上不会缓冲短查找?

转载 作者:行者123 更新时间:2023-12-03 07:50:42 25 4
gpt4 key购买 nike

我的应用程序使用一个非常大的文件,并从小窗口内的各种偏移量执行许多非常小的读取。我将直接文件读取替换为通过 BufReader 读取。然后我使用进程监视器(来自 Sysinternals 套件)检查了操作系统执行的实际读取内容,似乎根本没有缓冲任何内容。更糟糕的是,它现在每次都会读取整个缓冲区容量,这使得性能变得更糟。

重现:

use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};

fn main() {
let path = "data.bin";
let f = File::open(path).unwrap();
let mut r = BufReader::with_capacity(8 * 1024, f);

let mut buf = [0; 8];

r.seek(SeekFrom::Start(0)).unwrap();
r.read(&mut buf).unwrap();

r.seek(SeekFrom::Start(100)).unwrap();
r.read(&mut buf).unwrap();

r.seek(SeekFrom::Start(200)).unwrap();
r.read(&mut buf).unwrap();
}

在这样的场景中我应该如何使用BufReader

最佳答案

这是通过 Seek::seek 的实现来调用的对于 BufReader :

Seek to an offset, in bytes, in the underlying reader.

The position used for seeking with SeekFrom::Current(_) is theposition the underlying reader would be at if the BufReader<R> had nointernal buffer.

Seeking always discards the internal buffer, even if the seek positionwould otherwise fall within it. This guarantees that callingBufReader::into_inner() immediately after a seek yields the underlyingreader at the same position.

To seek without discarding the internal buffer, useBufReader::seek_relative.

See std::io::Seek for more details.

Note: In the edge case where you’re seeking with SeekFrom::Current(n)where n minus the internal buffer length overflows an i64, two seekswill be performed instead of one. If the second seek returns Err, theunderlying reader will be left at the same position it would have ifyou called seek with SeekFrom::Current(0).

推理已列出,并提供了针对您的用例的解决方法...具体来说,请调用 BufReader::seek_relative相反:

Seeks relative to the current position. If the new position lies within the buffer, the buffer will not be flushed, allowing for more efficient seeks. This method does not return the location of the underlying reader, so the caller must track this information themselves if it is required.

关于rust - 为什么 BufReader 实际上不会缓冲短查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77192724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com