When you make a game, there are many parts that are never going to be a bottleneck, and in those sections you often write safe idiomatic code. That is

Using C++ as a Scripting Language, part 13

submited by
Style Pass
2024-05-20 05:30:03

When you make a game, there are many parts that are never going to be a bottleneck, and in those sections you often write safe idiomatic code. That is usually code that contains temporary heap allocations.

I have a verbose setting for the heap and other system calls that I can use to monitor what the program is doing. However, the real story is in reading the RISC-V assembly. It turns out that when you call new in C++, it first jumps to an empty wrapper, and then to the real C++ new function, which then again would call my heap system call wrapper, which finally would invoke the system call. In C++ it is possible for new to throw an exception when the allocation fails, however that is completely irrelevant here as I am in control of everything. If an allocation fails, the emulator will throw an exception. Same with running out of memory, or running too long, and so on. So, I would like to avoid the new call chain, and ideally directly invoke the system call.

Benchmarking it, it took around 50ns. That’s not too bad. But, it can be improved just by avoiding all the calls that do nothing but call another function.

Leave a Comment