tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

Be smarter of stack<->heap allocations for "larger" systems.

Open dgryski opened this issue 1 year ago • 7 comments

Since https://github.com/tinygo-org/tinygo/pull/2628 , all allocations larger than 256 bytes are moved to the heap. This might not make sense on systems where the default stack size is larger.

This would also allow a larger work queue size for the garbage collector's mark phase which will reduce the number of full heap rescans on overflow.

dgryski avatar Dec 07 '22 17:12 dgryski

@aykevl I want to fix this; do you have any suggestions for how to determine a "large' vs "small" system. I was thinking for non-small systems to bump the max stack allocation size to 1024 bytes.

dgryski avatar Mar 31 '23 06:03 dgryski

The default stack size is known to the compiler, you could check whether the stack is at least 8kB or so before you allow larger objects to be allocated on the heap.

aykevl avatar May 01 '23 19:05 aykevl

Seems easy enough. I'll give it a shot.

dgryski avatar May 01 '23 19:05 dgryski

Hmm... guess the compiler is only one half of this, since we need need to get that value to the runtime somehow so we can tune the value of

        markStackSize      = 4 * unsafe.Sizeof((*int)(nil)) // number of to-be-marked blocks to queue before forcing a rescan

dgryski avatar May 01 '23 19:05 dgryski

@dgryski huh, why does the runtime need to know this? Is it just so that markStackSize can be larger (as an optimization)?

aykevl avatar May 03 '23 01:05 aykevl

Yes, that was one of my goals for this. The size of that stack drastically affects the runtime of the garbage collection cycles.

dgryski avatar May 03 '23 01:05 dgryski

We could add a magic function call that returns the configured default stack size, and then allocate the array using make. It should trivially const-propagate and be allocated on the stack instead of the heap (although it might break -opt=0).

That said, I'm thinking of adding a generic way to get system configuration variables that can be read from the runtime or elsewhere. This is mainly intended as a way to get board-specific variables into the runtime and machine packages (things like crystal oscillator configuration), but could also be used for the default stack size.

aykevl avatar May 03 '23 15:05 aykevl