Requests for comment/Minifier

From mediawiki.org

This RFC aims to improve the minification ratio of MediaWiki's JavaScript resources.

Proposal[edit]

Our current minification library, JavaScriptMinifier is actually quite nice, as it combines a decent minification ratio with a very good performance, however we can do better, and two areas are especially important: mobile where every byte counts and VisualEditor which delivers a load of scripts.

I propose to replace standard minification with an UglifyJS based service on WMF.

Comparison of minification[edit]

All sizes are in kilobytes
Resource Source JavaScriptMinifier UglifyJS
raw gzipped raw gzipped raw gzipped
mediawiki 67.4 19.5 19.4 5.9 14.1 5.3
jquery 269.1 77.6 140.9 40.6 90.8 32.6
ext.visualEditor.base|ext.visualEditor.core 1178 227.2 548.2 106.5 438.2 90.2

Proof of concept[edit]

Code lives in Extension:Minifier and requires a core change to work.

How it will work[edit]

1 or more servers running the Node.js service with LVS and Varnish in front of them. Varnish would use consistent hashing to make sure that requests for minification of the same data will end up on the same server (minification requests contain MD5 hashes of data in their URLs). The minifier service itself maintains a queue, makes sure that simultaneous requests for minification of the same JS will result in work being done only once and has an LRU cache.

Improving performance[edit]

UglifyJS is slower than JavaScriptMinifier (minifying jQuery core takes 2.1s instead of 1.5 on my machine) so some measures might be needed to improve cache hit rate:

  • Minifier server will have its cache shared between all wikis.
  • Make memcached/APC cache keys global?
    • Already the case since MediaWiki 1.26.
  • Minify every module separately to avoid minifying all possible combinations of modules?
    • Already the case since MediaWiki 1.27alpha (september 2016, phab:T107377).
  • Minify only module JS, not the messages. (To avoid cache invalidation upon localisation changes. This will also make minification of some modules a bit faster.)
    • Already the case since MediaWiki 1.26.