Feed on
Posts
Comments

I got this question in the mail today.

Seems like a simple enough question, but grep won’t provide that answer :) It also happens to be an excellent usecase for Dehydra.

My script:

var classes = []
function process_type (c) {
if (!/class|struct/(c.kind)) return
classes.push (c.name)
}


function input_end() {
var f = this.aux_base_name + ".counter"
print(f)
write_file (f, classes.join ("\n"))
}

process_type is called every time GCC hits a class declaration or a template is instantiated(also for enums and unions, but those get ignored with the .kind check). Then input_end is called when GCC is done processing the file. this.aux_base_name is the input filename.

I hooked up this script to the mozilla build by adding the following to .mozconfig:

export CXX=$HOME/gcc/bin/g++
export CXXFLAGS="-fplugin=$HOME/work/gccplugin/gcc_dehydra.so -fplugin-arg=$HOME/work/gccplugin/test/count_classes.js"

Then I built:

make -f client.mk build WARNINGS_AS_ERRORS=

Count:

find -name \*.counter|xargs cat |sort |uniq > /tmp/classes.txt
wc /tmp/classes.txt

Answer: 15001

There are a million other trivial queries that could be accomplished in a similar manner that weren’t easy or possible before.

Update: Fixed typo, had an extra zero in the answer

6 Responses to “Recipe: How many classes are instantiated in Mozilla?”

  1. on 12 Mar 2008 at 12:04 pm Robert

    ohhh run and remove one class so it is exactly 150000 :-)

  2. on 12 Mar 2008 at 12:09 pm Robert

    haha fixed typo, had an extra zero in the comment => 15000

  3. on 12 Mar 2008 at 12:19 pm Mark T. Kennedy

    it would be interesting to “trim” template instantiations (report them as just the template name, not the instantiated type). it would be an interesting measure of the “type complexity” of the beast.

  4. on 12 Mar 2008 at 12:20 pm Mark T. Kennedy

    whew :-) .

  5. on 13 Mar 2008 at 4:13 am Håkan W

    So a simple modification of this example, and we can find out all classes that are never used – right? That would be pretty neat, to see if there’s any low-hanging bloat we can cut.

    /Håkan

  6. on 13 Mar 2008 at 7:33 pm Havvy

    15k classes? You should really try cutting that number down by as much as possible.

    Oh, and do the search for template instantiations.

Trackback URI | Comments RSS

Leave a Reply