logoalt Hacker News

mwkaufmalast Sunday at 6:15 PM9 repliesview on HN

TL;DR

- no exceptions

- no recursion

- no malloc()/free() in the inner-loop


Replies

thefourthchimelast Sunday at 7:01 PM

I've worked on a playout system for broadcast television. The software has to run for years at a time and not have any leaks, We need to send out one frame of television exactly on time, every time.

It is "C++", but we also follow the same standards. Static memory allocation, no exceptions, no recursion. We don't use templates. We barely use inheritance. It's more like C with classes.

show 1 reply
krashidovlast Sunday at 7:36 PM

Has anyone else here banned exceptions (for the most part) in less critical settings (like a web app)?

I feel like that's the way to go since you don't obscure control flow. I have also been considered adding assertions like TigerBeetle does

https://github.com/tigerbeetle/tigerbeetle/blob/main/docs/TI...

show 3 replies
Taniwhalast Sunday at 6:56 PM

yup, same for any real time code, new/malloc/free/delete use hidden mutexes and can cause priority inversion as a result - heisenbugs, that audio/video dropout that happens rarely and you can't quite catch - best to code to avoid them

show 1 reply
pton_xdlast Sunday at 7:58 PM

That's standard in the games industry as well. Plus many others like no rtti, no huge dependencies like boost, no smart pointers, generally avoid ctors / dtors, etc.

wiseowiselast Sunday at 6:36 PM

That’s hardly 90% of C++.

show 2 replies
jandrewrogerslast Sunday at 6:26 PM

i.e. standard practice for every C++ code base I've ever worked on

show 1 reply
tialaramexlast Sunday at 7:13 PM

Forbidding recursion is pretty annoying. One of the nice things that's on the distant horizon for Rust is an explicit tail recursion operator perhaps named `become`. Unlike naive recursion, which as this video (I haven't followed the link but I'm assuming it is Laurie's recent video) explains risks stack overflow, optimized tail recursion doesn't grow the stack.

The idea of `become` is to signal "I believe this can be tail recursive" and then the compiler is either going to agree and deliver the optimized machine code, or disagree and your program won't compile, so in neither case have you introduced a stack overflow.

Rust's Drop mechanism throws a small spanner into this, in principle if every function foo makes a Goose, and then in most cases calls foo again, we shouldn't Drop each Goose until the functions return, which is too late, that's now our tail instead of the call. So the `become` feature AIUI will spot this, and Drop that Goose early (or refuse to compile) to support the optimization.

show 3 replies
petermcneeleylast Sunday at 9:46 PM

This is basically video games prior to 2010

show 1 reply
mslalast Sunday at 7:45 PM

At that point, why not write in C? Do they think it's C/C++ and not understand the difference?

> no recursion

Does this actually mean no recursion or does it just mean to limit stack use? Because processing a tree, for example, is recursive even if you use an array, for example, instead of the stack to keep track of your progress. The real trick is limiting memory consumption, which requires limiting input size.

show 4 replies