From 0d7ef032b71e9eaef02489e70f443f9776b2bebf Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Thu, 19 Nov 2015 21:44:36 +0000 Subject: [PATCH] resourceloader: Don't create redundant timers in addEmbeddedCSS() There's is typically two ways one might implement an eventloop-based timeout buffer in JavaScript. Note that "tick" refers to an iteration of the eventloop. Each iteration starts after all other sychronous code has finished in the current script execution. 1. Early-bird The first call to 'addBuffer' schedules a flush in the next tick. Any subsequent call (before the next tick) keeps adding to same buffer. As scheduled, in the next 'tick' all buffered items will be flushed. The next call to 'addBuffer' will a new cycle again. Buffered items are flushed within the next tick. 2. Idle delay Calls to 'addBuffer' always schedule a new flush. One cancels any existing flush that may've been scheduled. This effectively keeps pushing the flush to a future tick until a tick does not buffer new items. Buffered items may remain in buffer for a long time. For the CSS buffer in ResourceLoader we want the first model. And while the implementation already behaves like that, it was creating new timers in each call regardless. This resulted in 10 buffered items creating 10 timers. The first timer flushes the buffer. And the remaining 9 timers all try to flush an empty buffer. Change-Id: I49c64fedb0ad0362e91bb782fecf3cdda7dc61fc --- resources/src/mediawiki/mediawiki.js | 50 ++++++++++++---------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/resources/src/mediawiki/mediawiki.js b/resources/src/mediawiki/mediawiki.js index b58ce493d7..293fd589a4 100644 --- a/resources/src/mediawiki/mediawiki.js +++ b/resources/src/mediawiki/mediawiki.js @@ -800,10 +800,9 @@ // Selector cache for the marker element. Use getMarker() to get/use the marker! $marker = null, - // Buffer for #addEmbeddedCSS + // For #addEmbeddedCSS cssBuffer = '', - - // Callbacks for #addEmbeddedCSS + cssBufferTimer = null, cssCallbacks = $.Callbacks(); function getMarker() { @@ -872,40 +871,35 @@ cssCallbacks.add( callback ); } - // Yield once before inserting the