Recipe: How many classes are instantiated in Mozilla?

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 comments

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

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

  3. 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. Mark T. Kennedy

    whew :-).

  5. 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. 15k classes? You should really try cutting that number down by as much as possible.

    Oh, and do the search for template instantiations.