News for hackers. Stuff that matters.
Re:Understanding thread stack sizes and how Alpine is different by sys_64738 on Saturday June 26, 2021 @08:31AM (#27641296)
In other words, use heap space and not stack space. This is pretty elementary in C programming.
Re:Understanding thread stack sizes and how Alpine is different by viraptor on Saturday June 26, 2021 @09:06AM (#27641501)
I'd say "be aware of stack sizes". If you can get away with just the appropriately sized stack in a thread, that's a nice performance gain over dealing with multithreading heap allocators.
Re:Understanding thread stack sizes and how Alpine is different by IshKebab on Saturday June 26, 2021 @09:05AM (#27641499)
Sure, but 128 kB is really small even if you do that properly.

Seems like it would be more sensible if the stack space could just grow when required. Surely not that difficult?

Re:Understanding thread stack sizes and how Alpine is different by CodesInChaos on Saturday June 26, 2021 @09:39AM (#27641711)
Since standard OS stacks are contiguous and unmovable, you can't grow them once they run out of space. However while the address space for the maximum stack size gets reserved, each page only requires backing memory once it's first used. So as far as physical memory consumption is concerned, typical stacks act as growable with a fixed maximum size. Since address space is huge on 64-bit systems, choosing a large stack size is cheap on such systems (at least of they allow over-commit).

For the main thread, a system can also try to keep other allocations far from the stack without committing to any particular size (heap grows upwards from the bottom of the address space, stack downwards from the top). But this doesn't scale to multiple threads and leads to an unpredictable maximum stack size, so I prefer the fixed reserved space approach.

Re:Understanding thread stack sizes and how Alpine is different by viraptor on Saturday June 26, 2021 @09:16AM (#27641550)
The main thread already does that. The thread stack does not. It probably made sense on 32b with relatively limited address space... But I'm curious why we're not applying that to all threads on 64b. Reserving a few 10MB of address space per thread shouldn't be a big issue, right? (Without actually mapping those pages)
Re:Understanding thread stack sizes and how Alpine is different by megous on Saturday June 26, 2021 @01:38PM (#27644001)
Mapping a lot of pages has some overhead, too. So if you have large stacks per thread you'll take a hit.

If you use huge pages to alleviate that, you'll waste a lot of physical memory.

Re:Understanding thread stack sizes and how Alpine is different by tyingq on Saturday June 26, 2021 @09:23AM (#27641592)
It is easy for a single threaded program, but since each thread has it's own part of the stack, it would be non-trivial for a multithreaded program.
Re:Understanding thread stack sizes and how Alpine is different by megous on Saturday June 26, 2021 @01:48PM (#27644111)
Wait until you realize OS kernel thread stack size is 8 KiB or in that range, if you think 128 KiB is small. :))
Re:Understanding thread stack sizes and how Alpine is different by pjmlp on Saturday June 26, 2021 @10:55AM (#27642384)
Enough for a full COM binary plus one overlay section. :)
Re:Understanding thread stack sizes and how Alpine is different by aidenn0 on Saturday June 26, 2021 @09:38AM (#27641701)
A typical stack frame is around a couple dozen words. Let's round that up to 32 words (256 bytes). 128K is enough for a 500 deep stack at that size. 128K is huge.
Re:Understanding thread stack sizes and how Alpine is different by kstenerud on Saturday June 26, 2021 @12:51PM (#27643496)
That might have been true in the old days when memory wasn't the bottleneck, but in today's world where a cache miss is catastrophic, it makes MUCH more sense to use stack space where you can. This also has the side effect of facilitating idempotent functions and function purity in general.

No sense in clinging to old world ideals when they no longer make sense.

Re:Understanding thread stack sizes and how Alpine is different by ariadneconill on Saturday June 26, 2021 @01:06PM (#27643628)
That is certainly a take.

Storing small data, like function-local ints, pointers, etc, on the stack is beneficial due to L1$ prefetching semantics, but storing a 512KB scratchpad on the stack (which is what the article is about) will totally trash your L1$ and you'll have MORE cache misses than you would if that scratchpad was not on the stack.

Re:Understanding thread stack sizes and how Alpine is different by jlokier on Saturday June 26, 2021 @03:36PM (#27645080)
I agree that using the stack instead of heap makes sense for cache reasons.

But in the contemporary world, the trend is increasingly to transform functions to "async" forms where much of the functions' local state including return address is stored in heap-allocated space instead.

Re:Understanding thread stack sizes and how Alpine is different by dnautics on Saturday June 26, 2021 @12:29PM (#27643275)
this is too reductive. Usually stack space is faster because you will have fewer cache misses.

future languages will be able to suss this out at compile-time: https://github.com/ziglang/zig/blob/2ac769eab9b7dba4cd38e5de...

Re:Understanding thread stack sizes and how Alpine is different by sys_64738 on Saturday June 26, 2021 @12:40PM (#27643385)
If speed issues then it sounds like this is critical code path that is sensitive to time. You'd be looking for alternative data structures at that point so this point would be moot.
Re:Understanding thread stack sizes and how Alpine is different by ynik on Saturday June 26, 2021 @12:31PM (#27643301)
It's a bit ridiculous to complicate recursive algorithms just because the stack sizes haven't been increased in the past 3 decades.

Nowadays we have at least 48bit virtual address space available; what's the harm in giving each thread a full GB of stack?

Re:Understanding thread stack sizes and how Alpine is different by lonjil on Saturday June 26, 2021 @03:26PM (#27644990)
If you need a bigger stack, you can get one. This stuff is merely the default.
Re:Understanding thread stack sizes and how Alpine is different by sys_64738 on Saturday June 26, 2021 @12:38PM (#27643378)
Generally recursion is not something you want in production code. It's cute for academia when studying algorithms but where there's an iterative alternative that should be used.