Skip to content

Commit

Permalink
[BUGFIX lts] Avoid excessively calling Glimmer AST transforms.
Browse files Browse the repository at this point in the history
During the Ember 2.15 cycle glimmer-vm was updated and contained a new
format for AST plugins.  This required that all "legacy" plugins be
wrapped (to avoid breakage of this intimate API). When the wrapping code
was implemented two distinct bugs were introduced that would have caused
custom AST transforms to be called many more times than in Ember < 2.15.

Bug #1: Accruing AST Transforms via re-registration
---------------------------------------------------

ember-cli-htmlbars allows addons to register AST plugins via the normal
ember-cli preprocessor registry system. When this support was added it
was not possible to provide plugins during a specific compilation, and
therefore all plugins are registered globally (even by current
ember-cli-htmlbars versions).

Due to the mechanism that ember-cli uses to build up the list of addons
and dependencies for use at build time ember-cli-htmlbars ends up
requiring the template compiler many times and subsequently registering
any AST transform plugins that are present using the exported
`registerPlugin` API. Since this tracks plugins in module state it is
possible to have `registerPlugin` called many times with the same
plugin. ember-cli-htmlbars attempts to mitigate the polution of plugins
by forcing the require.cache of the template compiler module to be
flushed. Unfortuneately, there are circumstances where this cache
invalidation doesn't work and we therefore grow the number of registered
AST plugins _very_ quickly (once for each nested addon / engine / app
that is in the project).

During the 2.15 glimmer-vm upgrade the previous deduping logic was
removed (due to the fact that "legacy" AST transforms were always
generating unique plugin instances). When combined with situations where
ember-cli-htmlbars was not properly clearing the require cache the
resulting build times of complex applications using AST plugins grew >
double.

Bug #2: Legacy AST Transform Compat Bug
---------------------------------------------------

In order to avoid breakage in addons leveraging the intimate AST plugin
APIs a wrapper layer was added. Unfortunately, the wrapper layer
assumed that there would only be a single `Program` node. However, in
glimmers AST every `BlockStatement` has a `Program` node (and _may_
have an inverse with another `Program` node). This mistake meant that
the legacy "wrapped" AST transforms would be instantiated and ran _many_
times per-template which generates quite a bit of wasted work.

Fixes Included
--------------

* Bring back the deduplication code ensuring that a given AST plugin is
only registered once.
* Fix the legacy AST transform wrapper code to _only_ instantiate and
invoke the legacy AST transform once for the outermost `Program` node.
  • Loading branch information
rwjblue committed Feb 13, 2018
1 parent 1ae2e5c commit a95e314
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
23 changes: 19 additions & 4 deletions packages/ember-template-compiler/lib/system/compile-options.js
Expand Up @@ -30,20 +30,28 @@ function wrapLegacyPluginIfNeeded(_plugin) {
let plugin = _plugin;
if (_plugin.prototype && _plugin.prototype.transform) {
plugin = (env) => {
let pluginInstantiated = false;

return {
name: _plugin.constructor && _plugin.constructor.name,

visitor: {
Program(node) {
let plugin = new _plugin(env);
if (!pluginInstantiated) {

pluginInstantiated = true;
let plugin = new _plugin(env);

plugin.syntax = env.syntax;
plugin.syntax = env.syntax;

return plugin.transform(node);
return plugin.transform(node);
}
}
}
};
};

plugin.__raw = _plugin;
}

return plugin;
Expand All @@ -54,6 +62,13 @@ export function registerPlugin(type, _plugin) {
throw new Error(`Attempting to register ${_plugin} as "${type}" which is not a valid Glimmer plugin type.`);
}

for (let i = 0; i < USER_PLUGINS.length; i++) {
let PLUGIN = USER_PLUGINS[i];
if (PLUGIN === _plugin || PLUGIN.__raw === _plugin) {
return;
}
}

let plugin = wrapLegacyPluginIfNeeded(_plugin);

USER_PLUGINS = [plugin, ...USER_PLUGINS];
Expand All @@ -64,5 +79,5 @@ export function unregisterPlugin(type, PluginClass) {
throw new Error(`Attempting to unregister ${PluginClass} as "${type}" which is not a valid Glimmer plugin type.`);
}

USER_PLUGINS = USER_PLUGINS.filter((plugin) => plugin !== PluginClass);
USER_PLUGINS = USER_PLUGINS.filter((plugin) => plugin !== PluginClass && plugin.__raw !== PluginClass);
}
Expand Up @@ -18,8 +18,10 @@ moduleFor('ember-template-compiler: default compile options', class extends Abst
}
});

let customTransformCounter = 0;
class CustomTransform {
constructor(options) {
customTransformCounter++;
this.options = options;
this.syntax = null;
}
Expand All @@ -45,13 +47,10 @@ class CustomTransform {
}
}

moduleFor('ember-template-compiler: registerPlugin with a custom plugins', class extends RenderingTestCase {
beforeEach() {
registerPlugin('ast', CustomTransform);
}

class CustomPluginsTests extends RenderingTestCase {
afterEach() {
unregisterPlugin('ast', CustomTransform);
customTransformCounter = 0;
return super.afterEach();
}

['@test custom plugins can be used']() {
Expand All @@ -62,6 +61,28 @@ moduleFor('ember-template-compiler: registerPlugin with a custom plugins', class
content: ''
});
}

['@test wrapped plugins are only invoked once per template'](assert) {
this.render('<div>{{#if falsey}}nope{{/if}}</div>');
assert.equal(customTransformCounter, 1, 'transform should only be instantiated once');
}
}

moduleFor('ember-template-compiler: registerPlugin with a custom plugins', class extends CustomPluginsTests {
beforeEach() {
registerPlugin('ast', CustomTransform);
}

afterEach() {
unregisterPlugin('ast', CustomTransform);
return super.afterEach();
}

['@test custom registered plugins are deduplicated'](assert) {
registerPlugin('ast', CustomTransform);
this.registerTemplate('application', '<div data-test="foo" data-blah="derp" class="hahaha"></div>');
assert.equal(customTransformCounter, 1, 'transform should only be instantiated once');
}
});

moduleFor('ember-template-compiler: custom plugins passed to compile', class extends RenderingTestCase {
Expand All @@ -73,13 +94,4 @@ moduleFor('ember-template-compiler: custom plugins passed to compile', class ext
}
});
}

['@test custom plugins can be used']() {
this.render('<div data-test="foo" data-blah="derp" class="hahaha"></div>');
this.assertElement(this.firstChild, {
tagName: 'div',
attrs: { class: 'hahaha', 'data-blah': 'derp' },
content: ''
});
}
});

0 comments on commit a95e314

Please sign in to comment.