4 ms·
C++ is the only language I'm aware of where every extension made to the language is careful to only affect compilation time/complexity, while preserving the spe
by derefr 16y ago
C++ is the only language I'm aware of where every extension made to the language is careful to only affect compilation time/complexity, while preserving the speed and operational semantics of the code at runtime. This makes it perfect for some domains, e.g. console game development, where the speeds required are only attainable by dicking around with pointers in C, but people want to use better abstractions than C is capable of. So no, continuing to add those extensions is not wasted effort; we'll still be learning and using them long into the future.
- wvenable 16y agoC++ takes a very simplistic view of runtime performance, however. Generics are implemented using templates; which instantiates the code for every type you use. Naively, this preserves the performance of simply writing the same code for each type (which is what you did before templates). However, that might not be the best performing way to implement generics at runtime. (Edit: Small change to make meaning clearer)
- johnny531 16y agoHuh? Templates in C++ are pay-for-what-you-use; only those instantiations required by your code are generated. Note also the ability to extern templates added in C++0x, which allow you to collapse common instantiations to a single object file.
- seabee 16y agoHow do those features impact runtime performance? One example of templates affecting runtime performance is poor use of the instruction cache due to code duplication in every instantiation of a template.
- corysama 16y agoIf you are instantiating equivalent functions (sorting foo* vs sorting bar* ) then the linker will merge their binaries -no dupes there. If you are generating different functions (sorting foo vs foo* ) then you are asking for different functions -no dupes there either. If you want to keep everything down to one function, qsort() didn't disappear when they added std::sort() and it is pretty close to what a dynamic language sort would do under the hood.
- lanstein 16y agoC++0B maybe?
- barrkel 16y agoThis is thing that annoys me about C++'s illusion of control: unless you have / take full responsibility over the compiler and standard library, the "control" that C++ gives you is actually an abnegation of choice which leaves you to reinvent the world if you want specific features. What if you want something like Smalltalk / Lisp images? Runtime code generation and optimization of virtual method calls? Precise garbage collection? C++ leaves you on your own if you want these things in your final executable, and you have to work very hard - to the point of implementing a DSL, or writing / modifying your own C++ compiler - if you do want them.
- blub 16y ago"What if you want something like Smalltalk / Lisp images? Runtime code generation and optimization of virtual method calls? Precise garbage collection?" What is the point of controlling all of those things? The GP was talking about speed - how are GC, JIT and images related to this?
- barrkel 16y agoPrecise GC, JIT and images all give you more speed. Precise GC is asymptotically more efficient than manual allocation; you need to use other approaches, like arenas, to get back that lost time, but they are not always applicable or easy to introduce. Optimizing virtual method calls at runtime (e.g. monomorphic or polymorphic inline caches) is rather awkward without JIT. Similarly, C++ templates cannot be instantiated at runtime, meaning less efficient, more general and indirected code needs to be used instead. Images optimize startup hugely: they completely remove the need for any initialization not directly related to acquiring external resources. For example, this is one of the biggest reasons Chrome is so quick to start up - it uses freeze-dried heaps in V8 (its Javascript engine).
- blub 16y agoOk, but: 1. GC is fast at the cost of using more memory, which it turn slows the entire app down. 2. JIT can do all those optimizations, but how many of them are done in practice? How fast is the software compared to a binary compiled in C or C++? 3. Can be useful, but the only software that I've seen having trouble with startup times in the first place is written in .NET and Java :) I guess that what I'm getting at is that C++ code is still faster than e.g .NET code even if C# has GC and JIT. In the end that's all that matter, not whether you can do optimization X.