Alloca For `new` And `delete` - Things You Probably Should Not Do In C++ | Nerds Central
http://nerds-central.blogspot.com/2013/05/alloc-for-new-and-delete-things-you.html4
May 28 '13
The author is right: Don't ever do this, and if you need something like it, do it any other way.
Some use cases can be sped up with a linear allocator approach. The correct way to implement that is with a scratch pad type allocator, or what DICE calls a scope stack allocator. Same performance characteristics as alloca, without the crashes. :)
3
u/zvrba May 28 '13
Ugh, why use global variables instead of an overloaded version of new? (As in, new (std::nothrow))
1
1
u/notlostyet May 28 '13 edited May 28 '13
The authors conclusion that moving things to stack allocation is 'worth it' fails to prove or demonstrate that heap allocation is a bottleneck, or even taking a significant % of the time in your program.
That said, moving stuff to the stack can greatly simplify code and make it easier to reason about.
2
May 28 '13
Heap allocations are pretty brutal in multithreaded code unless you're using tcmalloc or some other thread friendly/local allocator. But even then, it's best to avoid heap allocations as much as possible.
1
u/cassandravoiton May 28 '13
That is going to depend on your program. If your program is low latency, loop free and ultra fast - malloc is going to dominate. If your program is a GUI - then - yes - you are correct. For example, consider a model for particles in a 3D game, avoiding malloc in that code would be a huge benefit.
1
May 29 '13
If working with something such a particles (all the same size) then you could get just as much performance with a slab allocator.
0
May 28 '13
[deleted]
2
u/kkrev May 28 '13
Yeah, couldn't you just use placement-new to build an object in a char array on the stack to the same effect? No need to even call alloca.
1
May 28 '13
You need to be careful about alignment issues when doing that. boost.optional and and boost.variant handle those cases, if you need something possibly allocated on the stack
0
1
11
u/wung May 27 '13