logoalt Hacker News

jtwaleson01/23/20251 replyview on HN

Well I'm not sure about forbidding heap allocations, that would severely limit what you can do with a function. In low level languages like Rust or C it would be difficult to keep track of the total size of heap allocations in a performant way, but in e.g. Python it should be possible to add some tracing so that a function can only allocate X bytes, and beyond that throw an error or log a warning.

It would be great if we can mark some functions as non-Turing-complete, and avoid recursion. Would make it easier to reason about them.


Replies

actionfromafar01/23/2025

A pattern in some embedded programming is to have an "arena" (just a bunch of pre-allocated memory) and then allow "malloc" (really super simple malloc) from that. But "free" is a no-op and does nothing. (It leaks all memory.) Then you just release the whole arena when you are done. You just reason about and/or test your code until it runs with a given arena size and call it a day. This way you can run "legacy" code written with allocations in mind, but the allocation is super fast with computable upper bounds on how long it takes worst case.

Super crude but useful sometimes.