2.24.2010

Finding Unused Classes with MXMLC

I came up with a quick and dirty method for finding unused AS3 clsses if you compile with MXMLC. The trick is to use a hidden compiler flag that I added while at Adobe: --keep-generated-signatures (and -incremental must be on too). This outputs a generated-signatures directory in your source tree filled with .sig files (named using the package structure, delimited by underscores: mx_core_Foo.sig). These are all the AS files that were compiled and/or synthesized in-memory (e.g. from [Embed] metadata) by MXMLC.

So, now you have a list of AS files that were compiled. Now we need a list of files that we have. cd to your source directory (if you have more than one, you're on your own):
$ cd src/
$ diff <(find . -name "*.as"|sed 's@\./@@'|sort) <(find generated-signatures -name "*.sig"|sed 's@generated-signatures/@@'|sed 's@_@/@g'|sed 's@\.sig@.as@'|sort)|grep "<"|sed 's@< @@'


This should give you a list of files that do not have signatures in the compiler, and should be safe to remove.


For the curious: .sig files are public class signatures -- they are a canonically ordered list of every function, field, import, metadata, etc., that could potentially have a dependency, in either direction, on another class being compiled. One way I used this was for an optimization to determine if a class' dependencies need to be recompiled or not -- if the signature didn't change, they don't. Every AS class gets a signature generated -- on the extremely rare case where a signature can't be generated (due to unforseen syntax or something), a warning is printed with mini-stacktrace to the console -- if you don't see that, you can be sure it's complete.

4 comments:

efnx said...

Awesome. Thanks. I'll be turning this process into a TextMate command soon.

Jono Spiro said...

Please share it here when you do! :)

newtriks said...

Yeh ditto on that one @efnx. Thanks @jono thats a tasty post you done right there :L)

Kecman said...

Thank you so much for this! Saved me hours and hours of work!