As you’re probably aware – there ain’t such thing as “too much memory”. It doesn’t matter how much there is, it’s never enough. Even if we fit, it still pays off to use the smallest structures possible, as they’re more cache friendly. The more we can squeeze them (without wasting too much time for depacking of course) – the better. Recently, I’ve been optimizing some X360 code and one of main problems (as usually with consoles) were related to cache-usage. There were some often used structures, that could probably be at least laid out more efficiently. At first, I’d examine them manually, but it quickly became too mundane. I thought of writing a simple tool that’d dump basic info about used types for me.
I already had experience with using DIA SDK (MemTracer and some of my other projects, that I hope to finish one day, including reflection system), so it seemed easy enough – I could extract all the information I needed from PDB (MSVC only, but that’s fine for my situation). After few hours I had my initial C++ commandline application. It worked nice, I could either display layout of specified type or make it list top X most “wasteful” structures. However, as soon as I finished, I thought it would be more comfortable to use if I could analyze all the structures, sort them by size/padding, see their details (… that should teach me to think about usage cases before actually starting to code, but I’m not sure if I’m not beyond salvation already). It’s rather trivial to use DIA from C# (actually, in many aspects it’s way cleaner than C++ version), so version 0.2 was actually even easier to code. Meet Cruncher# (stupid name, I know, but Sizer and Crinkler were already taken). It’s very simple, but works very well for my (limited) purposes. Source code is available, so feel free to hack until it works for you. Known bugs/limitations:
- it gets a little confused when encountering unions/bitfields,
- it only cares about UDTs (User Defined Types).
- there’s no information about vtable, base classes, etc.
I also briefly considered coding analyzer that would generate optimal class layout to minimize padding. In the end, didn’t decide to bother. Without knowing access patterns in many cases it could do more harm than good (splitting variables that are always accessed together for example). Besides, the problem itself wouldn’t be trivial, so not the best bang-for-the-buck ratio.
Final source code and executable can be downloaded here.
After starting the application, simply load PDB file (Ctrl+O). It’ll display all the UDTs in left panel and details of selected type in the right one, should be pretty self-explanatory, sample screenshot above (it’s from version 0.1b, later I added possibility of filtering symbols, it follows standard C# filter syntax, so for example to see everything related to namespace rde, type “*rde::*”).
PS. Freakin’ Norwegian is going to cancel Warsaw-Stockholm connection in August :/. Was fun while it lasted (700SEK for both way ticket? Yes, please).
[Edit] Uploaded new, fixed version, thanks to hcpizzi for reporting a bug (see comments for details).