跳到正文
数字商店
原文
John Carmack· @ID_AA_Carmack · X·· 2026-10-24AI 评分16
AI 导读

John Carmack 主张在 64 位架构下默认给每个栈分配等同物理内存的虚拟地址空间,让被普遍排斥的 alloca() 重新可用。他认为仅靠栈就能做好内存管理,分配只是指针调整、释放完全免费,线性紧凑布局对各级缓存和 huge/gigantic pages 更友好。

正文 · 原文

I always thought it was a shame that modest default stack setups made alloca() a generally-frowned-upon technique in C/C++. With 64 bit architectures, give every stack a virtual address space equal to the physical memory by default!

You can do a surprisingly good job of memory management with just the stack, and I contend that it has both correctness and performance advantages.

Allocations are a trivial pointer adjustment, and frees are literally free. Keeping everything linearly packed is friendlier for all layers of the cache hierarchy, and it makes keeping everything in huge / gigantic pages easy.

Leaving temporary intermediate blocks on the stack can seem wasteful, but unless you are dealing with algorithms that make > O(n) temporary allocations, it is usually not a big deal.

Static analysis can check if you store a stack pointer into a structure deeper in the stack, which is a use-after-free, and then memory management correctness is guaranteed.

The discipline of caller-allocates-return is awkward in some dynamic cases. It would take a change in low level calling conventions, but you could plausibly have functions that return with the stack extended from where it was when they were called. I’m not holding my breath for that.

It isn’t obvious if having AI coding agents makes discussions of software architecture more or less important!

来源:John Carmack · x.com