The Cost Of a Closure in C

36 points by mond


fanf

The “man-or-boy” test is a curious one. It was originally proposed in Algol Bulletin AB17.2.4 and there were some responses in AB19.2.3. (I haven’t chased down any further correspondence from that era.)

The Rosetta Code intro to the man-or-boy test is slightly wrong, in that Algol 60 does not in fact require heap-allocated activation frames. Because procedure invocations and call-by-name thunks are second-class they can’t outlive their creator so a stack is sufficient.

ibookstein

I'm left wondering why GCC Nested Functions would transition to a heap instead of a thread-local bump allocator, as the latter could leverage the intrinsic stack-based nature of function activations and avoid the contention of a shared heap. It could be made lazy-initialized to avoid squandering too much memory.

icefox

Just for funsies I wanted to see what this test would look like in Rust. I translated it from the C++ lambda version and got this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=7daf9efccb1c88ae9c2a3f5d97d2eab3

However, it fails the test and I'm not familiar enough with either the test or C++ closures to know quite what I did wrong. There's one Funny Hack I have to do as well to deal with Rust closures not being able to refer to themselves. Can I nerd-snipe anyone into helping?