{"id":291,"date":"2014-01-29T17:15:17","date_gmt":"2014-01-29T22:15:17","guid":{"rendered":"http:\/\/blog.mozilla.org\/nfroyd\/?p=291"},"modified":"2014-01-29T17:15:17","modified_gmt":"2014-01-29T22:15:17","slug":"space-saving-miscellany","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/nfroyd\/2014\/01\/29\/space-saving-miscellany\/","title":{"rendered":"space saving miscellany"},"content":{"rendered":"<p>Yesterday&#8217;s <a title=\"finding space savings\" href=\"http:\/\/blog.mozilla.org\/nfroyd\/2014\/01\/28\/finding-space-savings\/\">post on space saving techniques<\/a> generated a few comments.\u00a0 It seemed worthwhile to highlight a few of the comments for a wider audience.<\/p>\n<ul>\n<li>Various people have pointed out that clang and GCC support a <tt>-Wpadded<\/tt> option to warn when padding is necessary inside of a structure.\u00a0 Visual C++ supports <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/t7khkyth.aspx\">warning C4280<\/a> that does the same thing.\u00a0 You can enable this warning in Visual C++ by passing <tt>\/we4280<\/tt> on the compiler command line.\u00a0 I&#8217;m fairly certain this warning would generate a lot of output, but it might be worthwhile to comb through the output and see if anything interesting turns up.<\/li>\n<li>David Major pointed out <a href=\"http:\/\/ofekshilon.com\/2010\/11\/07\/d1reportallclasslayout-dumping-object-memory-layout\/\">the <tt>\/d1reportAllClassLayout<\/tt> switch for Visual C++<\/a>, which prints the class layout of all the classes in the compilation unit.\u00a0 If you&#8217;re only interested in a single class, you can use <tt>\/d1reportSingleClass$NAME<\/tt> to narrow the report down to the class with $NAME. GCC used to have something similar in <tt>-fdump-class-hierarchy<\/tt>, but that option has been removed.<\/li>\n<li><a href=\"https:\/\/benoitgirard.wordpress.com\/\">Benoit Girard<\/a> asked if he could see a list of the 50 largest things on a Linux build.\u00a0 Forthwith, I present <a href=\"http:\/\/people.mozilla.com\/~nfroyd\/big-objects.txt\">the 50 largest objects<\/a> and <a href=\"http:\/\/people.mozilla.com\/~nfroyd\/big-functions.txt\">the 50 largest functions<\/a> in <tt>libxul<\/tt> for an x86-64 Linux optimized build.\u00a0 One thing to note about the objects is that they&#8217;re not all in the same section; the seventh field in <tt>readelf<\/tt> output tells you the section index.\u00a0 So for the linked list of objects above, section 15 is <tt>.rodata<\/tt> (read-only, shareable data), section 22 is <tt>.data<\/tt> (read-write non-shareable data), section 27 is <tt>.data.rel.ro<\/tt> (data that needs to have relocations applied at load time, but can be read-only thereafter, e.g. virtual function tables), and section 29 is <tt>.bss<\/tt> (zero-initialized memory).\u00a0 Unsurprisingly, string encoding\/decoding tables are the bulk of the large objects, with various bits from WebRTC, JavaScript, and the Gecko profiler also making an appearance.\u00a0 Media codec-related functions appear to take up a large amount of space, along with some JavaScript functions, and a few things related to the HTML parser.<\/li>\n<li>A commenter by the name of &#8220;AnimalFriend&#8221; correctly pointed out that what you really want to know is which structures both have a lot of instances hanging around and have holes that you could fill.\u00a0 I don&#8217;t know of a good way to answer the first part without adding a lot of instrumentation (though perhaps you could catch a lot of cases by augmenting the <tt><a href=\"http:\/\/mxr.mozilla.org\/mozilla-central\/source\/xpcom\/glue\/nsTraceRefcnt.h#18\">MOZ_COUNT_CTOR<\/a><\/tt> macro to tell you which structures get allocated a lot). The second part can be answered by something like pahole.<\/li>\n<li>Alternatively, you could use something like <a href=\"https:\/\/github.com\/arvidn\/access_profiler\">access_profiler<\/a> to tell you what fields in your objects get accessed and how often, then <a href=\"http:\/\/blog.libtorrent.org\/2013\/12\/memory-cache-optimizations\/\">carefully packing those fields into the same cache line<\/a>.\u00a0 The techniques <tt>access_profiler<\/tt> uses are also applicable to counting allocations of individual objects.\u00a0 Maybe we should start using something more <tt>access_profiler<\/tt>-like instead of <tt>MOZ_COUNT_CTOR<\/tt> and friends!\u00a0 Definitely more C++-ish, more flexible, and it eliminates the need to write the corresponding <tt>MOZ_COUNT_DTOR<\/tt>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday&#8217;s post on space saving techniques generated a few comments.\u00a0 It seemed worthwhile to highlight a few of the comments for a wider audience. Various people have pointed out that clang and GCC support a -Wpadded option to warn when padding is necessary inside of a structure.\u00a0 Visual C++ supports warning C4280 that does the [&hellip;]<\/p>\n","protected":false},"author":320,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts\/291"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/users\/320"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/comments?post=291"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts\/291\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/media?parent=291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/categories?post=291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/tags?post=291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}