How to determine the size of the L1 cache at compile time?

how do I determine the size of the L1 cache at compile time? I am aware of the raw_cpuid crate, but sadly it's function to do that isn't const.

If you need the L1 cache size of the host, you can just use raw_cpuid in - more on this is in the Cargo book. Note though that it might not be the same as the size on the target machine, of course.

L1 cache size isn't a property of the target. What are you hoping to do with this information?

You might be interested in Cache-oblivious algorithm - Wikipedia, which is about making things that work well even when you don't know the exact size.

1 Like

I need to implement something similar to the Sieve of Erathosthenes, where you go over a large array in rather large step sizes repeatedly and mark some positions. It's usually best to do this in blocks of the L1 size. I wanted to use an array instead of a vector, because I thought it could be faster for some reason.

The array is not any better if you just allocate the vector once and reuse it many times.

But I agree with @scottmcm that some sort of cache-oblivious approach might be useful here. In segmented Erathosthenes sieve, you could recursively split your segment into smaller sub-segments when sieving with smaller primes.

Thank you. I read up on what the segmented sieve is, and yeah, thats exactly my plan, but you usually get best results when the block size is the L1 size. So I think I'll just use raw_cpuid at runtime and create a vector of suitable length, since I don't know a simple way to make such a thing cache-oblivious (of course, you can just use a fixed block size that works reasonably well on all platforms, but then I don't see a benefit over querying for the correct size).

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.