4 ms·
The author points out the extra complexity that C++'s programmable value semantics have added to the language without also mentioning the important benefits. F
by cageface 13y ago
The author points out the extra complexity that C++'s programmable value semantics have added to the language without also mentioning the important benefits.
First, you can create a lot of objects on the stack, which avoids any dynamic allocation at all and is far, far faster.
Second, you can precisely control the layout in memory of your data structures. For example, you can represent a set of vertices in an OpenGL scene as a contiguous array of structs. This gives you much, much better cache performance and also makes it easy to hand data directly off to hardware components like graphics cards that need input in memory formatted in specific ways. In languages like Java you have to resort to awkward, inefficient hacks like FloatBuffers to do the same thing.
- thebear 13y agoAuthor of blog post here: It's true, I haven't done a good job of mentioning the benefits that you get in return for the complexity of C++, especially as far as value semantics is concerned. Thanks for setting that right.
- cageface 13y agoI haven't done much C# programming but it looks to me from studying language tutorials that you can have the best of both worlds in many cases in C#. You can have value objects and densely packed arrays of structs if you need them but Java-esque reference semantics everywhere else.
- kgabis 13y agoActualy it might be better (cache-wise) to declare a struct of arrays (SOA) rather than an array of structs (AOS).[1] [1] http://www.intel.com/content/dam/www/public/us/en/documents/technology-briefs/intel-labs-closing-ninja-gap-paper.pdf http://www.intel.com/content/dam/www/public/us/en/documents/...