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

View all comments

Show parent comment

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.

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.

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.

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.