3

I'm doing extensive computations in f# on short arrays of uint64; I'd like to stack allocate them to avoid the garbage collector running. In C++, I'd do this:

int search(int n, uint64_t* data) {
    while ( /* something */ ) {
        // ... do some computation on data.
        uint64_t* data2 = (uint64_t*) alloca(n * sizeof(uint64_t));
        for (int i=0; i<n; ++i) { data2[i] = /* compute derived value */; }
        int candidate = search (n, data2);
        // ... determine if candidate is better than best etc.
    }
}

But f# doesn't seem to have a primitive, stack-allocated array type?

Søren Debois
  • 5,598
  • 26
  • 48
  • 6
    `NativePtr.Stackalloc`? https://msdn.microsoft.com/en-us/library/ee353741.aspx – John Palmer Mar 30 '15 at 09:04
  • what kind of computation is this? The C++ snippet seems to indicate that you need to mutate a lot so maybe using a common mutable array instead of recreating arrays might be what you want – Random Dev Mar 30 '15 at 09:08
  • I'm searching through a very large tree I'm unfolding as I go. The `uint64 []` identifies the current node. I'd prefer not to mutate the current node, as I'll then need to undo the mutation when (in the example) the recursive search call returns. – Søren Debois Mar 30 '15 at 09:32
  • 1
    Escape analysis for garbage collected language lets some things to get allocated on the stack. http://en.wikipedia.org/wiki/Escape_analysis – Ming-Tang May 02 '15 at 02:17

0 Answers0