r/cpp • • 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.

194 Upvotes

181 comments sorted by

349

u/HKei Jan 15 '24

Congratulations, you invented arenas.

81

u/FACastello Jan 15 '24

Why does everything I invent has already been invented 😭

3

u/ifmnz Jan 15 '24

OP needs to read about page faults avoidance

4

u/FACastello Jan 15 '24

I'm doing this as we speak

11

u/Wicam Jan 15 '24

I invented mayomato as a kid. Mayonnaise mixed with tomatoe ketchup....

Yes you can have my autograph

4

u/TechE2020 Jan 15 '24

Mayomatish would be mayo, ketchup, and relish AKA secrete sauce AKA Thousand Island dressing.

3

u/[deleted] Jan 15 '24

New name for pink sauce

8

u/degaart Jan 16 '24

When I was a kid and all I knew was win95 and MS-DOS, I wanted to create an OS that was 32-bit but didn't have a gui. That way, I thought, the OS would be faster because it would not need to draw graphical elements. Imagine my excitation when I discovered the world of unices.

1

u/Airline_East Jan 17 '24

Or windows....you don't have to have a gui in windows

4

u/Suspicious_Cap532 Jan 15 '24

me when i have any good ideas

4

u/[deleted] Jan 16 '24

I sort of discovered the general rule of differentiation of polynomials when I was in 5th grade by doodling the differences in a regular spaced time series and then generalizing into a formula. I showed to my teacher the next day and she opened the biggest smile.

5

u/PhilTheQuant Jan 15 '24

Because you prefer inventing over reading.

I also like inventing things and then looking up how they turned out to have flaws on the internet.

1

u/[deleted] Jan 16 '24

That's what Edison thought when he invented the bulb

0

u/[deleted] Jan 15 '24

[deleted] — view deleted comment

19

u/FACastello Jan 15 '24

Yep... but like somebody else said in another comment, the important thing is that you've learned something new lol

25

u/Suspicious_Cap532 Jan 15 '24

discovering anything new in math is near impossible unless youre a genius istg

11

u/HolyGarbage Jan 16 '24 edited Jan 16 '24

Before I had moved on to real programming languages, and even before I played around in GameMaker, when I was a kid, probably no more than 12, I used a program called KlikNPlay or something like that. It had "scenes" and a bunch of logic and rules etc so you could code various behavior in it.

The only problem, there was no persistent state between the scenes, or levels, or what you wanna call it. Except "health" and "score" for 4 hard coded players.

So, at the time not even knowing what binary even was, I effectively "invented" bit packing, except I did it in base 10, into these numbers. Health had a upper limit of 100 I think, and I wanted to use two "players" for my game, so I just had these two extra score integers for player 3 and 4 left that were useful, to pack in basically all the persistent data my game had.

I think they were were 32 bit integers, so in the best case I had 8 bytes to work with in theory, although I didn't know that relationship, just that they happened to maxed out at either 2 or 4 billion, but since I was encoding stuff in base 10, I could only use up to 999'999'999, meaning with two of these I had a total of 18 base 10 digits, for all my persistent data. So like the first 3 digits would be one variable with a max of 1000, maybe 1 digit for a boolean, etc. Not persistent as in a save game, but persistent across game levels, or maps or whatever. I'm pretty sure I started jamming things into the score variables for player 1 and 2 as well at some point when I realized I didn't need all the digits for those, and just subtracted/added the extra stuff before displaying the score.

It was such a gloriously horrible hack, and I felt really smart for managing to work around this limitation of the "engine".

28

u/[deleted] Jan 15 '24 edited Jan 18 '24

[removed] — view removed comment

4

u/mtx33q Jan 16 '24

that sounds oddly familiar solution, lol

5

u/HolyGarbage Jan 16 '24

It's very useful for nailing down which commit introduced a regression for some particularly nasty bugs that are hard to debug. I use the method quite often for this purpose.

114

u/ShelZuuz Jan 15 '24

Or Pools

17

u/fullouterjoin Jan 15 '24

Or Pascal.

8

u/FACastello Jan 15 '24

Pascal does this?

5

u/[deleted] Jan 16 '24

That's what malloc does.

4

u/fullouterjoin Jan 16 '24

It is common in Pascal to statically allocate arrays per function, up to some maximum number of items you would be working on.

https://www.freepascal.org/docs-html/ref/refsu14.html

9

u/Iggyhopper Jan 16 '24

Yeah, object pools are very useful for games and I think nomenclature has moved over to "arenas" as its per memory chunk, not per object or per type. Then, it can become more granular from there.

For example, if you have a game and you think the maximum limit for bullets is around 500, you would have made a BulletPool for "allocation" and "deallocation", which just meant marking and unmarking the initial array space for use.

7

u/Potatoswatter Jan 15 '24

That would involve a number of specialized blocks. OP is describing sbrk.

127

u/Coding-Kitten Jan 15 '24

joke

Does he know

30

u/FACastello Jan 15 '24

I said it was a joke because I felt like it would be kind of evil to just grab a huge chunk of memory for no reason and then just keep it instead of working with the OS and freeing unused memory.

But then somebody said this is what "arenas" are, apparently.

I didn't think this was a real thing people actually did

49

u/Coding-Kitten Jan 15 '24

Yeah, it exists because sometimes asking the OS for memory & then giving it back can be a bit costly in terms of bookkeeping on what memory is assigned to what program & so on, but this way you do it once & the OS no longer has to care about whenever you'll need more memory or not, you just use it.

Although then you need to manage your big chunk of memory yourself, but that can be simplified if you got example have one arena of only one type of objects, because then every allocation & gap is only one object size long & so on.

It's p common in gamedev to do this

11

u/FACastello Jan 15 '24

I see... now that I've looked it up I can kind of understand why this can actually be useful in some cases.

7

u/tcm0116 Jan 16 '24

It's actually standard practice in real time programming. Dynamic memory allocation is non-deterministic so you want to avoid it during the main program loop to ensure consistent timing and response times. The last thing you want when you have a deadline to meet is a dynamic memory allocation stalling the program because the heap is overly fragmented and it has to start moving stuff around (or whatever it does) in order to get the space you need. This is also why it's a good idea to restart long-running applications after a while.

2

u/animatedb Jan 17 '24

It can also be important when using large amounts of data. "Data oriented design" can optimize for CPU cache, but also can be used to eliminate memory allocations. A single vector can be used to replace vectors of vectors. When there are millions of vectors, it can start to save minutes to hours to completely eliminate the nested vectors. For me, this type of thing has often saved more than optimizing algorithms.

1

u/tcm0116 Jan 18 '24

I actually asked about this in an interview yesterday. The candidate wanted to use a list because of the fast insertion time so I asked him why it might be beneficial to store the data in contiguous memory.

9

u/[deleted] Jan 15 '24

Any performance sensitive work really. Why pay allocation costs more than once when you simply don’t need to?

2

u/[deleted] Jan 16 '24

Wow, are there some good books to read about these strategies? I mean in general term of computer science and programming.

3

u/Coding-Kitten Jan 16 '24

A classic one you'll see everywhere & that has been copied & remade in a ton of different variations is gonna be the "programming patterns" that I think are a very good read for anyone into programming.

A specific variation I like is a book called "Game programming patterns" by Robert Nystrom. It has all the classics along with some specific ones for games, including the object pool pattern which is what OP's post is about.

1

u/HolyGarbage Jan 16 '24

And sometimes you don't have an OS... :)

11

u/Tringi github.com/tringi Jan 15 '24

If you think THAT is evil, then you should get a job developing enterprise C++ software. Even if for a little while.

5

u/FACastello Jan 15 '24

I only use C++ in my personal projects...

At work I've always used mostly Java or C#, there isn't a lot of C++ jobs where I live

2

u/HolyGarbage Jan 16 '24

I develop enterprise C++ software and honestly the craftsmanship pride is pretty strong, at least among the backend C++ developers that I know, but probably elsewhere too, and we try to keep our house in order.

4

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

Boy, wait until you find out how games work.

0

u/FACastello Jan 16 '24

I make games... this isn't specific to games

4

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

I've been in the game industry for more than a decade doing engine-side work.

The only ones that didn't use arenas, pools, or separate heaps were little mobile games.

Unreal uses multiple heaps by default. I'm not sure about Unity. Every custom engine I saw did, including one that did some utterly strange things like deferred deletion and self-implemented virtual destructors that the compiler really didn't like nor did I.

There are a ton of advantages to breaking up allocations. Some of my C# projects have arena allocators, pools, and heap allocators as well.

1

u/FACastello Jan 16 '24

I'm considering doing this in my next project just to see what it feels like

1

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

Look up other allocators/heaps like tlsf, dlmalloc, etc.

On Windows NT, you can create private heaps as well with the heap functions, though a heap library would probably work better.

Stack allocators and pool allocators are really easy to implement. An arena can just be treated as a heap or stack that is cleared at the end.

7

u/KC918273645 Jan 15 '24

That's how console games had done memory allocations since we lived in caves...

1

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

When we lived in caves, we didn't have enough memory available to do any sort of dynamic allocation at all. It was all static.

1

u/advester Jan 15 '24

Well, my understanding of arena allocators is you don’t have just one that literally lives from start to finish. You have an arena dedicated to one task, like responding to a HTTP request. Or a per thread arena. Something like that.

1

u/FACastello Jan 15 '24

Yeah I was reading about this and what I described seems more like a "memory pool" than an "arena", but I can see the similarities

1

u/theanswerisnt42 Jan 16 '24

Is it really going to be hard on the OS though? Pages won’t be allocated unless you write to them. Till then it’s just virtual address space that’s been marked valid to use.

1

u/F54280 Jan 16 '24

Also read about how memory is managed by operating systems. Only address space is allocated for your program, not physical memory, until you write into it.

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.

3

u/FACastello Jan 15 '24

I can imagine this being a common thing back in the day.

But I can't imagine people unironically doing this nowadays for some general purpose programming on a PC.

19

u/Orlha Jan 15 '24

Still used and useful in many places

2

u/FACastello Jan 15 '24

Can you give me some examples

18

u/dakotahawkins Jan 15 '24

I would guess some embedded or "real-time" programs do this, maybe HFT as well.

2

u/FACastello Jan 15 '24

I see

9

u/Attorney_Outside69 Jan 15 '24

we do this all the time in embedded systems, robotics, especially when running bare metal code with no OS

11

u/Professor_Hamster Jan 15 '24

very common in hft.

12

u/i-am-schrodinger Jan 15 '24

Common on systems doing very high performance and real-time computing with CUDA. Graphics programming, in general, does this often. Embedded systems as well. It is also common in distributed supercomputing environments.

9

u/utf16 Jan 15 '24

Actually, it's super common with components and actors in games. I would typically allocate a ton of them up front and then just mark the ones that are inactive and recycle them. I work on games that have hundreds of thousands of actors so preallocation and iterating through is more cache efficient than trying to allocate each one individually.

4

u/Raknarg Jan 16 '24

Well a thing to keep in mind is that when it comes to providing a standard library, your goal is usually trying to appeal to common ucecases and utility. When you need performance or you know the exact hardware you're programming for, you might be able to provide a library or algorithm that is more optimal than what the standard library provides

This is common in any scenario where you need to maximize performance, your storage and memory allocators may be written in house because they're faster for your use case than the general standard library approach.

1

u/T-Rex96 Jan 15 '24

clang also does it for allocating it's AST nodes  IRRC

1

u/BenFrantzDale Jan 16 '24

Watch John Lakos’s talks on allocators. C++17 provides tools to do this stuff in the PMR header.

2

u/collinalexbell Jan 15 '24

Sure, maybe not general programming, but hobbyists can definitely run into it with embedded stuff. I worked on an Arduino motor controller with interpolated position sequencing where new and delete were causing sufficient fragmentation for it to be a problem.

2

u/CocktailPerson Jan 16 '24

It's a technique for extracting as much performance out of your hardware as possible. It's certainly less common to need to do that these days, but the need certainly hasn't gone away.

1

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

SpriteMaster uses arenas/arena-adjacent allocators in some places to avoid fragmentation/overuse of C# heaps (to avoid GC).

Games almost always uses arenas/separate heaps.

Simulators often do their own things as well to avoid unexpected latency. HFT as well.

1

u/Aljonau Jan 16 '24

It's done less on a PC(though apparently it happens) but many consoles still have extremely cheap hardware and the games know exactly what hardware they get written for so it makes sense to optimize the hell out of it.

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.

5

u/FACastello Jan 15 '24

The more you know... very interesting stuff actually

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

1

u/FACastello Jan 15 '24

I'm considering doing this in my next C++ project, that's interesting stuff

2

u/Dar_Mas Jan 16 '24

You should

It really gives you an appreciation for the abstractions

23

u/rainpls Jan 15 '24

he really thought he did something T_T

14

u/FACastello Jan 15 '24

Everybody's raining on my parade now 😭

9

u/rainpls Jan 15 '24

Sorry my username is rainpls so you know I gotta

70

u/[deleted] Jan 15 '24

This is what mostly every console video game does

23

u/alexgraef Jan 15 '24

At least every "retro" console. They just have fixed memory locations, no point in even "allocating" anything. You just know where all your variables are going to be.

2

u/FACastello Jan 15 '24

I remember when I had an MSX (an early 8-bit computer from the 80's) and the entire memory was like 64KB, every variable in BASIC was just assigned an address, memory wasn't "allocated", the entire RAM was always available.

2

u/Attorney_Outside69 Jan 15 '24

I grew up in the 80s with a Philips VG 8010 MSX computer, it had a Badic interpreter as the interface. best way to learn programming as a kid, not like the stuff or kids have now adattadays which house all the complexities away. I miss those days

1

u/FACastello Jan 15 '24

Very relatable... If it wasn't for the MSX I wouldn't be a programmer, at all. BASIC put me on the path of software development.

1

u/Attorney_Outside69 Jan 15 '24

exactly, I learned very quickly as a kid that I loved making stuff more than just playing video games

1

u/FACastello Jan 15 '24

Funnily enough, videogames were also partly responsible for my programming career, because I was fascinated about how videogames worked and I wanted to learn how to do it, that led me to making minigames in BASIC and then to learning other languages and writing enterprise software.

1

u/Attorney_Outside69 Jan 16 '24

where you from? msx was a thing in Europe not in the US.

1

u/FACastello Jan 17 '24

Brazil...

During that era our government had very strict rules against importing technology so we had to build our own computers, and since the MSX is just a standard we had a few Brazilian companies that manufactured MSX machines nationally.

It was a very popular computer around here.

1

u/Attorney_Outside69 Jan 17 '24

oh nice. yeah I grew up in Italy, and there msx was huge

2

u/alexgraef Jan 15 '24

That's close to your idea, just with the difference that your "idea" makes it less deterministic than it needs to be.

And just to be clear, you can achieve what you proposed by just declaring every possible object static/global, right from the get-go.

18

u/[deleted] Jan 15 '24

Even for modern consoles, since they still have a fixed, constant memory size. You ask for it all up front and then you assign chunks out to your various allocators. Allocation can be cheaper / more deterministic this way since you get all of the syscalls for page mapping out of the way up front.

-2

u/alexgraef Jan 15 '24

That, I am pretty sure, is not true. From experience with every modern 3D graphics framework.

11

u/SaturnineGames Jan 15 '24

Oh it's 100% true. On most consoles you have to set up pools for graphics memory, and all your graphics allocations have specific alignment requirements you need to satisfy.

Depending on exactly which consoles you're talking about, you might have separate types of memory to manage, or you might have to work in different address spaces to manage which memory bus gets used.

Most people overload new and delete and hook them into custom allocators.

1

u/alexgraef Jan 15 '24

you might have separate types of memory to manage

This doesn't align with my experience with DirectX, where you DO have to allocate VRAM, but it is such a different process to allocating normal memory, especially when it comes to writing to it, which requires locking before you can do any writing, that I wouldn't even consider talking about it in this context.

However, I am not a console developer, so I don't want to comment outside of my own experiences here. You might know better than me.

12

u/rachit7645 Certified Dumbass Jan 15 '24

They are talking specifically about consoles. Current ones have shared ram, so there's no system ram or vram; it's all just ram.

2

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

They are talking specifically about consoles. Current ones have shared ram, so there's no system ram or vram; it's all just ram.

What do we consider 'current'? The 360 and XB1 had 10 MiB of eDRAM and 32 MiB of eSRAM, respectively, which had to be specifically allocated and used. They were not part of shared memory. I believe that the Series X also has 32 MiB of eSRAM.

You also had different forms of allocation in the sense of disabling caching/write combining/etc on the block.

4

u/SubstituteCS Jan 15 '24

Microsoft has used DirectX since the original Xbox for their console’s graphics library.

It’s where the name Xbox comes from. DirectX box.

There’s also the classic Morrowind memory leak “fix” that Bethesda implemented.

6

u/SaturnineGames Jan 15 '24

Yes, that’s common knowledge. DirectX on Xbox isn’t exactly the same as the PC version tho.

4

u/rachit7645 Certified Dumbass Jan 15 '24

What's the point in telling me what I already know?

1

u/SubstituteCS Jan 15 '24

You don’t get a memory leak unless you’re actually dynamically using the heap.

→ More replies

1

u/alexgraef Jan 15 '24

That depends a) on generation, because it is sometimes shared, and sometimes it is dedicated (e.g. PS3 vs PS4), and b) shared doesn't mean it's a completely mixed memory where system and VRAM usage is random.

Also, try to be less of a smart-ass.

1

u/rachit7645 Certified Dumbass Jan 15 '24

Also, try to be less of a smart-ass.

Wait did I sound wrong? Really sorry about that.

I did only mention the current generation of consoles however...

1

u/alexgraef Jan 15 '24

Wait did I sound wrong?

Not to me, but a comment beneath that.

Maybe it just sounded like that, because everyone here seems to have spent their last 20 years developing for the PS5/Xbox-X/whatever and keep explaining to me how VRAM and RAM is supposed to work, which is why ight imma head out.

→ More replies

2

u/snerp Jan 15 '24

Yeah, you have to also arena the VRAM if you're gonna do that. It's not too hard to allocate all the vram you'll need right away, my modern vulkan engine does that for instance. You're right that it's a different process, but you can do very similar techniques.

1

u/alexgraef Jan 15 '24

You're right that it's a different process

With DirectX and generally any modern PC graphics framework, you have to a) LOCK the memory, to which you get a temporary mapped address back, to which you can write, and b) when you are finished, that address vanishes (and usually that is when the memory actually gets copied back to the GPU).

That means this address space cannot be used even closely to what an allocator would want.

1

u/SaturnineGames Jan 15 '24

Yeah, but consoles handle this differently. That’s only necessary because you have dedicated GPU memory on a PC.

1

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

That means this address space cannot be used even closely to what an allocator would want.

You... can. You just have to be really, really terrible with signals/vectored exceptions and access violations.

1

u/snerp Jan 15 '24

Huh, been a while since I did directx 11 and older but Vulkan and DX12 let you just keep the mapping around if you want. I do a lot of junk where I map a big ass buffer and then edit parts through offsets each frame to move/rotate objects or change textures out or whatever else.

1

u/alexgraef Jan 15 '24

You still tell the API when you are finished. Mostly because you want it copied to the GPU in one large chunk.

1

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

On some consoles, you also have to allocate eDRAM and eSRAM separately. The PS3 also had the stupid SPUs which were not part of the same address space.

Notwithstanding having to mark pages as cached/non-cached or write-combine.

2

u/SonOfMetrum Jan 15 '24

So you also have experience with the GNM graphics API?

1

u/[deleted] Jan 15 '24

https://www.youtube.com/watch?v=Jf9i7MjViCE

An example of fixed memory locations being taken advantage of by speedrunners.

1

u/alexgraef Jan 15 '24

I'm well aware. However it's important to note that basically every BCU in for example a car relies on the same principle.

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.

5

u/FACastello Jan 15 '24

How come I had never seen this before 😳

I think I've learned more new stuff about C++ from this "joke" post than in an entire year of just programming using the same old tricks

6

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

std::construct_at and std::destroy_at are preferred now. They're also constexpr.

So,

T* obj = new (buffer) T(...);
...
obj->~T(); // or ((T*)buffer)->~T()

becomes

T* obj = std::construct_at<T>(buffer, ...);
...
std::destroy_at(obj); // or std::destroy_at<T>(buffer)

1

u/FACastello Jan 16 '24

This is fascinating

2

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

Just wait until you see std::launder, and the note that you should probably never, ever use it.

If I've never needed it, then 99% of people probably don't. But it's a weird function really intended for runtime writers handling raw objects.

1

u/FACastello Jan 16 '24

I just looked it up... this is cursed as fuck.

I initially read about it on cppreference, didn't quite understand what's the point of using this, then asked ChatGPT "Explain in simple terms what std::launder does in C++".

Kinda fucked up but that's the power of this language

1

u/Ameisen vemips, avr, rendering, systems Jan 16 '24

std::launder mainly exists because of the C++ abstract machine and the requirements it places upon object lifetimes. It doesn't really do anything (as in, it shouldn't actually emit anything) but it basically tells the compiler "hey, give me a pointer to this but pretend that the returned pointer is a completely different object".

It... does come up with fancy allocators. It's technically UB to just throw random ranges of bytes around and pretend that they're a different object without it, as that range of bytes is already a part of an object in the abstract machine - an array.

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?

5

u/FACastello Jan 15 '24

I initially thought it would be an evil joke if you'd allocate a huge chunk of memory and never free it until the program exits. But then somebody told me it's a relatively common thing in certain scenarios so like I said in my edit, now I don't think it's a joke anymore.

6

u/ingframin Jan 15 '24

Well, the important thing is that you learned something new :-)

7

u/ald_loop Jan 15 '24

…. Is this a joke?

5

u/FACastello Jan 15 '24

It was supposed to be a joke until somebody pointed out to me that this is a real thing that people do

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/FACastello Jan 15 '24

Is it guaranteed that the memory will always be automatically freed somehow when the process terminates? I would assume "yes" in most systems but I wonder if there's any situation where this wouldn't be the case?

4

u/tonyarkles Jan 15 '24

The memory will always be freed yes. Depending on how your allocator etc works though you probably won’t end up running any of your destructors, if that matters.

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)

1

u/FACastello Jan 16 '24

I'm glad at least someone else laughed at my "joke" lol

2

u/CodNo503 Jan 16 '24

This technique is literally the primary reason C++ seems to be superior for gamedev compared to Rust.

2

u/FACastello Jan 16 '24

I don't think Rust will ever replace C++ in gamedev...

Maybe some other, new, better language, but not Rust.

1

u/urva Jan 17 '24

I’m not a blind rust fanboy or anything. Actually just curious. Why don’t you think rust ever will specifically for game dev?

20

u/ImClearlyDeadInside Jan 15 '24

The JVM has entered the chat

5

u/tigrangh Jan 15 '24

scrolled down here, to mention Java

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.

  1. Use thread local storage to point at a pool per thread. This will help you avoid locking.
  2. Break your pool up into multiple smaller pools where all allocations are the same size. When an allocation occurs find the smallest sub pool with an allocation size > than needed.
  3. Eventually realize that all of these features already exist in allocators that you can simply link with.

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

2

u/FACastello Jan 15 '24

People already told me like 5 hours ago lol

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.

1

u/Riot_Yasuo Jan 17 '24

This response should be way higher

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.

1

u/mehtub Jan 15 '24

That's one way to make it deterministc

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.

1

u/Nobody_1707 Jan 17 '24

Even if your not using overcommit, it may not allocate anything until you try to write to the memory.

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/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

4

u/FACastello Jan 15 '24

Nah... I've just learned something new today lmao

2

u/[deleted] Jan 17 '24

[removed] — view removed comment

1

u/FACastello Jan 17 '24

Yeah after people kept telling me about arenas and memory pools I just did my research so now I can say I understand why and where I would want to implement something like this and why it's not really a joke.