mw.loader: Remove Deferred overhead from execute() hot code path
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 14 Sep 2017 09:52:52 +0000 (11:52 +0200)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 14 Sep 2017 09:52:52 +0000 (11:52 +0200)
This was introduced by 0ac4f99804c for the concept of globally
implied legacy dependencies.

Commit ba257035b07 then re-purposed this for the site/user module
dependency.

The legacy wait was removed in 0ac6076b4c0, thus leaving only the
site/user code.

This commit now simplifies the code back to how it was before 0ac4f99804c
(but keeping the improved error handling from ba257035b07).

Change-Id: I2eae3b78fbe4f03a7a098d8a6233bdc2b79171b8

resources/src/mediawiki/mediawiki.js

index fc930ca..0ae45b5 100644 (file)
                                registry[ module ].state = 'executing';
 
                                runScript = function () {
-                                       var script, markModuleReady, nestedAddScript, implicitDependencies, implicitWait;
+                                       var script, markModuleReady, nestedAddScript;
 
                                        script = registry[ module ].script;
                                        markModuleReady = function () {
                                                } );
                                        };
 
-                                       implicitDependencies = [];
-
-                                       if ( module === 'user' ) {
-                                               // Implicit dependency on the site module. Not real dependency because
-                                               // it should run after 'site' regardless of whether it succeeds or fails.
-                                               implicitDependencies.push( 'site' );
-                                       }
-
-                                       implicitWait = implicitDependencies.length ?
-                                               mw.loader.using( implicitDependencies ) :
-                                               $.Deferred().resolve();
-
-                                       implicitWait.always( function () {
-                                               try {
-                                                       if ( Array.isArray( script ) ) {
-                                                               nestedAddScript( script, markModuleReady, 0 );
-                                                       } else if ( typeof script === 'function' ) {
-                                                               // Pass jQuery twice so that the signature of the closure which wraps
-                                                               // the script can bind both '$' and 'jQuery'.
-                                                               script( $, $, mw.loader.require, registry[ module ].module );
-                                                               markModuleReady();
-
-                                                       } else if ( typeof script === 'string' ) {
-                                                               // Site and user modules are legacy scripts that run in the global scope.
-                                                               // This is transported as a string instead of a function to avoid needing
-                                                               // to use string manipulation to undo the function wrapper.
-                                                               $.globalEval( script );
-                                                               markModuleReady();
+                                       try {
+                                               if ( Array.isArray( script ) ) {
+                                                       nestedAddScript( script, markModuleReady, 0 );
+                                               } else if ( typeof script === 'function' ) {
+                                                       // Pass jQuery twice so that the signature of the closure which wraps
+                                                       // the script can bind both '$' and 'jQuery'.
+                                                       script( $, $, mw.loader.require, registry[ module ].module );
+                                                       markModuleReady();
+
+                                               } else if ( typeof script === 'string' ) {
+                                                       // Site and user modules are legacy scripts that run in the global scope.
+                                                       // This is transported as a string instead of a function to avoid needing
+                                                       // to use string manipulation to undo the function wrapper.
+                                                       $.globalEval( script );
+                                                       markModuleReady();
 
-                                                       } else {
-                                                               // Module without script
-                                                               markModuleReady();
-                                                       }
-                                               } catch ( e ) {
-                                                       // Use mw.track instead of mw.log because these errors are common in production mode
-                                                       // (e.g. undefined variable), and mw.log is only enabled in debug mode.
-                                                       registry[ module ].state = 'error';
-                                                       mw.track( 'resourceloader.exception', { exception: e, module: module, source: 'module-execute' } );
-                                                       handlePending( module );
+                                               } else {
+                                                       // Module without script
+                                                       markModuleReady();
                                                }
-                                       } );
+                                       } catch ( e ) {
+                                               // Use mw.track instead of mw.log because these errors are common in production mode
+                                               // (e.g. undefined variable), and mw.log is only enabled in debug mode.
+                                               registry[ module ].state = 'error';
+                                               mw.track( 'resourceloader.exception', { exception: e, module: module, source: 'module-execute' } );
+                                               handlePending( module );
+                                       }
                                };
 
                                // Add localizations to message system
                                                // cssHandlesRegistered ensures we don't take off too soon, e.g. when
                                                // one of the cssHandles is fired while we're still creating more handles.
                                                if ( cssHandlesRegistered && pending === 0 && runScript ) {
-                                                       runScript();
+                                                       if ( module === 'user' ) {
+                                                               // Implicit dependency on the site module. Not real dependency because
+                                                               // it should run after 'site' regardless of whether it succeeds or fails.
+                                                               mw.loader.using( [ 'site' ] ).always( runScript );
+                                                       } else {
+                                                               runScript();
+                                                       }
                                                        runScript = undefined; // Revoke
                                                }
                                        };