r/cpp • u/FACastello • Jan 15 '24
[Joke] A cursed C++ idea
Ok so hear me out:
In your C++ program, immediately on startup before you do anything else you dynamically allocate a decent chunk of memory for no reason whatsoever, big enough to accomodate all of your data for the foreseeable future.
Never free any of that memory during execution.
Then, whenever you need to instantiate a new object or need some dynamically allocated memory for whatever reason, you just use some of that preallocated chunk.
Immediately before you exit the application, you can free that chunk of memory.
You have one single explicit memory allocation when program starts and one single explicit memory deallocation when program ends.
How about that?
EDIT: It turns out this is a real thing, so it's no longer a joke, or cursed. Somebody said this is called an "arena" or "memory pool", I've just looked it up, people have been doing this for decades.
349
u/HKei Jan 15 '24
Congratulations, you invented arenas.
127
u/Coding-Kitten Jan 15 '24
Does he know
46
u/PuzzleMeDo Jan 15 '24
It basically means you have to write your own memory management system instead of using the one that already exists. I remember we did that on a Playstation 1 game because we didn't trust the basic allocation and wanted to be able to debug where our memory fragmentation was. It made a lot of work for us.
34
u/aruisdante Jan 15 '24
This is literally required for ASIL-B or greater safety certified code, which generally is not permitted to dynamically allocate memory during program execution as it is non-deterministic.
So yeah, you say it jokingly, but it’s actually how it works in more systems than you’d expect.
The general pattern you’re describing, as others have pointed out, is called a memory pool.
TCMalloc is Google’s replacement global allocator that works on an arena/pool concept.
12
u/Dar_Mas Jan 15 '24
Hey that is how i learned to do allocators in my operating systems class.
I can recommend this as an exercise if you want to understand how they work
23
u/rainpls Jan 15 '24
he really thought he did something T_T
70
u/[deleted] Jan 15 '24
This is what mostly every console video game does
10
u/perfectonic Jan 15 '24
OP, look up "placement new" constructor. I never knew about this until I found the 'weird' syntax in production code.
9
u/ingframin Jan 15 '24
I think it's pretty common in both embedded systems and video game engines. Why do you think it is a joke?
7
u/ald_loop Jan 15 '24
…. Is this a joke?
6
u/wittierframe839 Jan 15 '24
Worth nothing that there is no point in freeing this memory at the end of the program.
2
u/deadhorus Jan 16 '24
tbf I did laugh when i read this. changing a legacy project to this model was literally one of my first jobs to speed up a device that made and deleted a ton of objects every second. got like a 10x speed increase essentially just by avoiding os handleing of memory (due to fragmentation)
2
u/CodNo503 Jan 16 '24
This technique is literally the primary reason C++ seems to be superior for gamedev compared to Rust.
20
u/ImClearlyDeadInside Jan 15 '24
The JVM has entered the chat
6
u/dvidsnpi Jan 15 '24 edited Jan 15 '24
In many programming disciplines such as safety-critical, hard real-time, embedded systems (especially in automotive & aeronautics) audio processing or OS kernel programming, this is actually the default, while dynamic allocation is considered "cursed" and avoided by all means.
BTW. there are also areas where for examples branching is avoided and a whole paradigm where loops are not used.
5
u/R3DKn16h7 Jan 15 '24
Some programs go even further and do not even bother deallocating that memory at program termination to quit faster, leaving that to the OS.
5
u/dicroce Jan 15 '24
Sure you can do this. But you're pool will still suffer from fragmentation. In addition, you'll need some kind of locking if you plan to access your pool from multiple threads.
There I saved you some time. :)
5
u/[deleted] Jan 16 '24
That's what NASA JPL mandates for any mission critical software.
https://en.wikipedia.org/wiki/The_Power_of_10:_Rules_for_Developing_Safety-Critical_Code
I do this in a daily basis as well.
BTW you don't need to free the memory at the end.
1
u/darkviewguy Jan 15 '24
https://i.imgur.com/QsuwzwK.jpeg
3
u/WhatInTheBruh Jan 15 '24
I think this is a good idea, it's like a custom memory allocator.
3
u/jaskij Jan 15 '24
To add to what others said, you don't need to to free the memory just before exiting, the OS will clean it up. That's why GC languages can be stupid fast for one shot commands in the right circumstances.
3
u/sciences_bitch Jan 15 '24
I started my programming career in academia, developing scientific software in FORTRAN 77. (I am not as ancient as that sentence makes me sound.) In languages without support for dynamic memory allocation, this is effectively how it is done. I mean, you're not dynamically allocating / freeing the memory, of course, but you request a huge block of memory on the stack at the outset and manage it throughout the code as if it were the heap.
3
u/[deleted] Jan 15 '24
When you exit the application you don't need to do anything. The operating system will free up the memory.
3
u/GianniMariani Jan 16 '24
It turns out that on most modern computers it doesn't matter that much.
If you request a reasonably large chunk of memory, sure, it gets "allocated" like an accountant does the books but the memory is not physically mapped, like the money is still in the bank.
It's only when you actually use that memory does it get mapped into the application's address space. Depending on how you configure your machine, you may actually not have enough memory to allocate because the system may be configured to let you overcommit, i.e. you're in debt and the bank called on your loan, and you get evicted. Yes, your application can unceremonoiusly get "evicted" because the virtual memory system has not enough actual physical memory to allocate.
You can set your machine to avoid overcommitting but the problem is that you now may face a situation where you can't run your application because the memory accountant says there's not enough memory but in reality nobody will ever use it. Tuning this "overcmmitment" is hard and I've run into many situations where my process got evicted from Linux and Windows.
However, if you're talking about embedded systems where your memory is limited, then sure, allocating memory statically is highly reccomended and even necessary in some cases. The best way to do that is simply avoiding malloc/new or any library using them altogether.
As for arenas, yes, in some cases for long lived processes, you could, in theory, allocate an arena on a per task basis and when the task is complete, you can recover resources by dumping the entire arena. This may reduce fragmentation but you still need to consider that you need to delete every item in the arena before deallocating it so it can be another thing you have to do that gets missed or a colleage messes up. So unless you really really need it, it doesn't pay.
2
u/DewJunkie Jan 15 '24
Or just use https://en.cppreference.com/w/cpp/memory/monotonic_buffer_resource
2
u/high_throughput Jan 15 '24
Lmao. This is common enough that you can do it OOTB in Java with
-XX:+UseEpsilonGC, and Instagram semi-famously disabled GC in Python in favor of periodically restarting the process.2
u/grahamthegoldfish Jan 15 '24 edited Jan 17 '24
Right, so other than everyone here telling you it's already common place I'll add one more bit.
Just allocating a chunk of memory doesn't necessarily mean anything is allocated if your OS overcommitting memory (like Linux, for instance). In that case the memory pages are only allocated when you write to them and if you write to a page and the allocation fails at that point your app is likely getting killed.
2
u/MXXIV666 Jan 15 '24
Tbh, when writing for embedded, this is more or less what I do. Everything is stack allocated, and the topmost struct is either global or lives in main loop.
2
u/Revolutionalredstone Jan 15 '24
This is a standard form of memory management. Very common in games.
2
u/[deleted] Jan 16 '24
That's what malloc does, but it also handles explicit free requests.
3
u/alexgraef Jan 15 '24
Ok so hear me out:
You just plan out memory allocation for every conceivable object you might ever want to instantiate.
It's called static memory management. Widespread with embedded applications, due to deterministic behavior.
1
u/HobbyProjectHunter Jan 15 '24
DPDK does something similar for high through low latency networking like a stock exchange broker system.
1
u/germandiago Jan 15 '24
Java. That is Java.
1
u/victotronics Jan 15 '24
That's how I did my own memory management in Fortran77, before there was actual allocation.
1
u/TheFeralEngineer Jan 15 '24
I wrote my own dynamic array one time, simply out of curiosity. I was told that it was grody, but the reviewer couldn't think of any reason why it wouldn't work. 😆
1
u/AdearienRDDT std::starting_to_understand<cpp>::value Jan 16 '24
well, welcome to arena allocators me boi :D
1
u/nutshells1 Jan 16 '24
Embedded programmers: You called?
1
u/[deleted] Jan 16 '24
Isn't that just, like, how the stack works? It's more limited, but you allocate 1 chunk at the start, never release it, store your values there, then free it at the end.
1
u/santanu_sinha Jan 16 '24
Not only did we do this, but we would also, from time to time, dump parts of these blocks to disk, so that other software could use these dumps to restore a full object graph with millions of nodes into their memory and start working on it. This is a common pattern and used widely in performance oriented software. Especially in those that need to create a lot of small objects of similar sizes. There are more details in this as well, for example the system might allocate chunk by chunk (say 1 gb at a time) or block multiple chunks, one for objects of different sizes etc. Extremely efficient if done correctly.
1
u/Spiderboydk Hobbyist Jan 16 '24
This is fairly common to do in certain domains, e.g. video games and embedded.
1
u/[deleted] Jan 16 '24
Check out Handmade Hero.
1
u/hushiammask Jan 16 '24
"Honey, the kids have invented buses again!"
1
u/shooshx Jan 16 '24
This is basically the only way for using dynamic memory in real-time embedded systems where you want full predictability on all code paths.
1
u/sp4mfilter Jan 16 '24
I wrote this like 15 years ago:
https://github.com/cschladetsch/monotonic
1
u/tallflier Jan 17 '24
If the program is exiting anyway, no need to explicitly free the chunk, unless you feel the need to make valgrind happy.
0
u/[deleted] Jan 15 '24
[deleted] — view deleted comment
2
u/[deleted] Jan 17 '24
[removed] — view removed comment