Shrink Silverlight Apps with XapOptimizer
XapOptimizer can substantially shrink the size of your Silverlight XAP files, giving your users faster downloads. While the result is almost magical, there are reasons to be wary.
XapOptimizer performs two operations to shrink your code. Most of the shrinkage comes through pruning dead code from your application: XapOptimizer looks for classes and resources not being used and eliminates them. XapOptimizer also obfuscates your code by replacing meaningful variable names with shorter names.
The first project I aimed XapOptimizer at did, in fact, shrink by almost 70 percent. XapOptimizer removed the classes that I didn't need from libraries I had to add to my project, and there was more deadwood than I realized -- it just wasn't mine.
It's hard to project whether my results will reflect yours. Very large applications may shrink less than you expect because the base libraries will form a smaller percentage of the application. On the other hand, larger projects may also include more libraries, which would increase opportunities for XapOptimizer to work its magic. But even with those caveats, the product's impact is impressive, substantially shrinking every one of the XAP files I tested against.
XapOptimizer doesn't integrate with Visual Studio. Instead, you start up XapOptimizer as a standalone application and create a project by adding a reference to your XAP file. Once you've created the project, a single click optimizes your code and generates a report on what was done to your XAP file. Options on your XapOptimizer project let you enable or disable obfuscation and back up your file before shrinking it. XapOptimizer removes any digital signatures from your code, but you have the option of letting the product sign the assemblies in your XAP file as part of shrinking it.
[Click on image for larger view.]
|Figure 1. It's hard to imagine a cleaner UI than XapOptimizer: Set the XAP file and click the Optimize button.|
Wrinkles and Workarounds
There are some dangers here. For instance, XapOptimizer can't spot that a class called through reflection is being used and may prune it from your application. To prevent this, you can pin files in your application to make them immune from pruning. XapOptimizer looks for modules with reflection code and flags them to warn you of potential problems. You, however, have to determine what classes to pin.
If your app doesn't work after optimizing (and error messages don't tell you what's missing), ComponentOne recommends pinning all of your project's components and then unpinning them one-by-one until you find the problem component.
If you're using a third-party control with a signed assembly, XapOptimizer will remove the signature. After XapOptimizer has finished shrinking your project, you'll need to open your XAP file and reinsert the original signed assembly (which will add back any classes XapOptimizer pruned from that assembly). You'll need to disable assembly cache support, because XapOptimizer doesn't support it.
XapOptimizer includes a command-line tool that you can incorporate into a batch build process. Given the uncertainties involved, it would be prudent to download the free trial to see if XapOptimizer works with your applications before purchasing the full product.
Quick Facts: Shrinks XAP files by eliminating unused code and resources
Pros: Does what it says -- reduces XAP file size by 30 percent to 70 percent
Cons: No Visual Studio integration; some debugging may be required to ensure you end up with a functioning application
Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.