0

I need to declare and initialize array with size at least 8GB (1 billion of f64 variables). Everything I've tried so far is very complicated or inefficient or gives STATUS_STACK_OVERFLOW (0xC00000FD) error because it's first being created on heap.

How can I do it efficiently, clean and nice?

Is there any way to allocate a standard Rust array directly on the heap, skipping the stack entirely? Solutions proposed here use Vec, which does have performance impact.

trincot
  • 317,000
  • 35
  • 244
  • 286
MarioPL98
  • 23
  • 7
  • 1
    `vec![0.0f64; 1_000_000]` – Shepmaster May 26 '21 at 19:53
  • Vec has performance impact. I need raw array to reduce memory usage and increase performanceas much as possible. – MarioPL98 May 26 '21 at 19:57
  • Note the first words of the first answer: *Summary: your benchmark is flawed; just use a `Vec`*. Please show _your_ benchmark that backs up the claim "Vec, which does have performance impact." – Shepmaster May 26 '21 at 19:57
  • Also note that there are [**multiple answers**](https://stackoverflow.com/a/56426372/155423) – Shepmaster May 26 '21 at 19:59
  • Doesn't Vec store additional data compared to array? – MarioPL98 May 26 '21 at 19:59
  • Other answer uses unsafe keyword which I want to avoid. – MarioPL98 May 26 '21 at 19:59
  • 5
    It does. A `Vec` of a billion elements will also take up 3x `usize` values. On a 64-bit machine, a `Vec` will have the "overhead" of 24 bytes. Compared to your other memory allocation of *gigabytes*, I think you can spare it. – Shepmaster May 26 '21 at 20:00
  • Since this array is heap allocated, you'd still need at least 8 of those bytes to reach it; if its size is not statically known then you'd need another 8 bytes for that (i.e. together being the 16 bytes of the slice reference). So the overhead of `Vec` is actually 8 or 16 bytes, not 24. – eggyal May 27 '21 at 09:25

0 Answers0