Tools for Analyzing C++ Class Code Generation?
Milo_Mindbender submits this query: "I've got a midsize Linux project which uses a lot of STL and other C++ template code. Even considering this, I end up with a lot bigger text (generated code) segment than expected. I know the information about the amount of code generated for each class is in the objdump, but prying it out by hand is a problem when you get five line long template invocations and hundreds of methods to wade through. Can anyone can recommend some tools that analyze binary or objdump output and summarize the amount of code generated for each class, including each unique template or STL class?"
"Why you shouldn't use OOP for anything important."
I've had enough abrasive sigs. Kittens are cute and fuzzy.
I want to write an insanely clever and informative post and answer Milo's question in a way which would cause the calculating hordes of Slashdot moderaters to moderate my post to Score:5, Astounding!
Unfortunately, I've never used C++ before in my life. (except for a "Hello World!" back in High School)
I can feel my sanity, beyond my reach and slipping...
Our large corporation also has a project, a client has requested that we implement BubbleSort, QuickSort, ShellSort, and one other sort that we learnt in cl^H^H^H^H^H^H^H^H^H err, discussed in a business meeting. The err, client also requests a 1 page analysis comparing and contrasting the differences between the searches.
Can code generation do this? It could really increase my chances of a good mar^H^H^H err profit yeah.
For each additional programmer involved in the project, double the contents by stirring in a random different kind of Visualization Tool.
If you're targeting multiple architectures with different STL implementations, the Visualization Tool can be enhanced by having the lead of each platform boil their own pot of Visualization Tool, and then throwing the still-boiling contents at the other chefs^W developers.
If you aren't clear as to where I'm headed, I'll next recommend a more painful exercise: Go ahead and try debugging somebody else's template-heavy code.
Use a map or a hash. Oh, and lastly, move all the code in headers out into the .cxx/.cpp file, your code will compile faster, and probably run nearly as fast, with the exception of a handful of performance critical classes (finding them is important).
Kirby
For others learning the language I found these books helpful for C++:
Effective C++ by Meyers
Effective STL by Meyers
and STL C++ Standard Library by Josuttis
Huh? What did you expect? Templates lead to bloat per definition. That's what they do: a lot of binary code is generated by a small amount of typing. If bloat becomes a problem: redesign.
I know the information about the amount of code generated for each class is in the objdump, but prying it out by hand is a problem when you get five line long template invocations and hundreds of methods to wade through. Can anyone can recommend some tools that analyze binary or objdump output and summarize the amount of code generated for each class, including each unique template or STL class?
I wish I did. But most likely, I think you are fucked. Without an understanding of the code, it's hard to debug template problems. Find out where the bloat is by analysing the source code, not the binary.
Search at source forge for Open C++ or OpenCPP, not sure how it is written.
That is a programmable source to source transformer for C++.
It spits out one singel file of c++ for every compiled C++ unit, removing the includes, by expanding them into the output.
That way yo can source level debug the processed c++ code, not exactly what you want but likely more powerfull in the long run anyway.
Your request varies far to heavy from compiler to compiler.
angel'o'sphere
Cost free eBook I read (by iBook/Kobo/Amazon/ObookO/Gutenberg etc.): "The Green Odyssey" by Philip Jose Farmer.
If you do not already have a copy of Large-Scale C++ Software Design by John Lakos, get it. I realize this isn't a tool per se, but he has a terrific section in chapter 10 on Using C++ Templates in Large Projects.
This is more of a suggestion than a debugging tool but...
You didn't say which compiler you were using but, for example, with a recent g++, try turning off some of the inlining and turn on -frepo. This will make sure that, for example, there is only ever one copy of map::find.
I don't know; maybe the latest g++ does a good job of this already.
It is kinda ugly for what it does but it works. Only one instantiation of std::vector is made, it is void*. Putting the member function definitions in the header file ensures they will be inlined if necessary.
I think Scott Meyers came up with this tip first.
Yes it was Scott Meyers and it works great!
...
{} - The empty sig
Having used C++ templates extensively, I can say that compiler-generated code is a bad idea. A compiler should generate assembly, not other code. That said, I really like the *idea* of C++ templates, yet they fail in a number of subtle ways, not the least of which is that if the implementer of the STL construct screws up, you're screwed. Personally, I rather like keeping my own classes (containers and all) under my own control, so, when (not if) there's a bug, I can put in my own debug statements to track it down.
:-)
/.'ers who would like such a tool (I haven't even named it yet), keep a look out in my journal for the big announcement.
:-)
Note that C++ is not the only language that screws up the manifestation of so-called generics. Java and C# don't even have them (yet), and my experience with software of any kind of complexity that has major features added after the initial design and implementation is that they are bug-ridden POSs (and that ain't point-of-sale
The problem (IMO) is that there should be two phases between the translation of the program description to the final executable. The first phase should be a code generation phase where meta-level programming constructs are expanded into their final program description, and then, two, your standard compilation phase. C++ already jumbles the two and what you get is - guess what - jumbled. If the first phase produced specific source *files* in your project then you would have greater control.
Now for the blatant self-promotion. I am about to release a nifty piece of shareware that will generate such source files for you. While based on relatively simple text replacement, it gives the user the ability to generate collection classes for C#, Java, C, and C++, and (arg!) VB in a way more powerful than C++ templates, which it is loosely based upon.
The purpose for my writing the software was to automatically generate type-safe containers in C#, but with the ability to extend each container instance with its own functionality (not dissimilar in the end result from subclassing). Basically, it works as a parameterized code definition mechanism and the proof that it works is that I am using six auto-generated collection classes in the software itself (who else here likes the work of M.C.Escher?).
Anyhow, in answer to the initial question, your problem will never be gotten under control until you write your own containers (thereby permitting testing & debugging like any other code you write).
For any
Thanks slashdot, I now feel like a dirty whore. But just like a *good* whore, I *am* providing a much-needed service
Peace & blessings,
bmac
Just wanted to bring your kind attention to this amazing fact: everyone tries to handle STL code bloat, is looking for a (inexistent) STL debugging tool, suggests to completely re-design compiling principles just to make STL compile a bit better, writes books about good STL coding (how to make your binary 3 times smaller), rends the air by saying "do you know what 'S' means in STL????" (one of slashdot postings)...
So why use it at all? Just because it is standard? Then go program with Fortran or Ada, they are pretty standardized, yet almost completely useless/impractical. And so STL.
(Ah... forgot this: IMO, IMO, IMHO, IMVHO, IMVVHO)
Huh? No?
Yes, you can use simple templates with procedural programming. In that sense, knowledge of OOP is not needed to use templates. But, there are ways in which templates make OOP easier.
Templates can be combined nicely with several design patterns, such as observer, visitor, factory, and iterator. Template base classes for each of these design patterns can provide the common behaviors and implementations of these design patterns.
In addition to augmenting traditional OOP, templated classes can still have a lot of OOP-like properties. The classic examples here are in the STL. If we list the basic features of OOP, our list would include: Abstraction, Polymorphism, Inheritance, and Encapsulation.
The STL classes provide abstract interfaces into containers, algorithms, and iterators. The functors do provide a type of polymorphism through both templates. Several containers provide polymorphism through operator overloading. You can write classes to inherit from C++ containers and iterators, but that has various problems and issues with it. And they encapsulate the implementation details quite well. (e.g. - You don't have to worry about the mechanics of balancing red-black trees when you use std::map and std::set.)
Many of the STL classes do this already...it's called pointer specialization.
I think Visual Studio .NET includes a tool that does exactly what you want. Of course, once you've got it, you'll probably be too busy porting your program to C# to take advantage of the full power of the .NET framework, but its there if you need it.
I totally agree with the poster. C++ (and most other programming languages) are implementation languages. Most programmers are not aware of the fact that the main reason why we use implementation languages lies in the fact that we have to produce highly optimized code to make computers perform. Specification languages are hardly used, because we lack the proper tools to transform (formal) specification into implementation code. The reason behind this is that much of the engineering in "software engineering" lies in choosing the right implementation, e.g. it is not (yet) possible to automatically convert high-level specifications into executable code. I think it is really sad that after more than twenty years of software engineering, we are still working like craftsman, and not like engineers. In this sense software engineering hardly can be called an engineering science. For some more ideas, read The Art of Programming.
A lot of them are popping up in the embedded world (A must if you are using C++ in small memory spaces).
These guys are doing a bit more than just template reduction here
These is the point where teory and practice met. And in theory STL does everything right and good once for all. But in practice there is MFC, Qt, wxWin, and so on for real programs, because STL is still a teoric tool unless you are the writer of the STL itself and know how to debug templates.
I believe that mastering templates is more complex and long than mastering everything else in the C++ language. And that's their main pitfall, despite being absolutely powerfull and even turing complete (just see some entries in ioccc).
We are Turing O-Machines. The Oracle is out there.
I've seen template bloat in a lot of places, but MI bloat?
Just because something can be done in the language doesn't mean everybody just goes and uses it. You're right that MI is too prone to mistakes, and it can be replaced most of the time by java-like interfaces.
I use C++ because is deterministic, can be used to really big proyects, when it runs is fast as hell (can be faster than C when using inline functions), and because I don't use every shitty feature of the language just because it's there. The right tool for the job can be a subset of C++.
We are Turing O-Machines. The Oracle is out there.