How to select computer hardware to accomodate Rust

In my codes, I have a data: HashMap<i32, Vec<f32>>, which has more than 5000 key-value pairs, and each value (the Vec<f32>) has a length bigger than 100_000. When I call data.clear(), it takes more than 150 seconds. I thought it was a problem about computer hardware.
My computer harderware:

OS: Windows (x86_64-w64-mingw32)
CPU: 20 × 12th Gen Intel(R) Core(TM) i7-12700K
WORD_SIZE: 64
RAM: 128G, 3600MHz
DISK: volume 466G, Disk Random 16.0 Write: 394.20 MB/s. Disk Random 16.0 Read 568.70 MB/s.

If I need to accelerate data.clear() and put others aside, what should I do?

I made some simple tests and figured data.clear() should cost ~160ms. It's not something that hardware spec matters that much (~1000x). Maybe check your code first, or post a minimum repro so we can try to figure out what's wrong.

1 Like

Excuse me, my mistake. There is 1000 datas like data above. Like this

struct Datas {
    data_vec: Vec<HashMap<i32, Vec<f32>>
}

impl Datas {
    fn clear(&mut self) {
        for data in self.data_vec.iter_mut() {
            data.clear();
        }
    }
}

There is datas: Datas and datas.data_vec.len() == 1000.

I made a playground, I test and found it maybe a matter of removing String.

At a guess, yeah, the allocation per String may be killing you. Perhaps a string interner would help.

2 Likes

Is there a reason you can't just clear data_vec?

impl Datas {
    fn clear(&mut self) {
        self.data_vec.clear();
    }
}
1 Like

Thats going to run into the same problem, which is that there are 300,000,000 Strings being dropped as all of the Prices are dropped.

1 Like

I know. But eliminating the loop should shave off some time and is nearly free to test.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.