Skip to content

How to use rayon to speedup reading files by blocks? #1164

Answered by adamreichold
Evian-Zhang asked this question in Q&A
Discussion options

You must be logged in to vote

Since you appear to be able to compute the block offsets upfront, you should be able to start with Rayon's ParallelIterator implementation for Range, e.g.

let file = File::open("path/to/large/file")?;

let len = file.metadata()?.len();

let block_size = 4096;

let blocks = len / block_size;

(0..blocks)
    .into_par_iter()
    .map(|block| block * block_size)
    .try_for_each_init(
        || vec![0; block_size],
        |buf, offset| {
            file.read_exact_at(buf, offset)?;
            // TODO: process buf...
            buf.clear();
            Ok(())
        },
    )?;

Alternatively, you might want to look into using the memmap2 crate to map the whole file into your process' a…

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@adamreichold
Comment options

@adamreichold
Comment options

@Evian-Zhang
Comment options

@adamreichold
Comment options

@Evian-Zhang
Comment options

Answer selected by Evian-Zhang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants