Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(computed-artifact): support arbitrarily many inputs #2705

Merged
merged 3 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 92 additions & 6 deletions lighthouse-core/gather/computed/computed-artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class ComputedArtifact {
this._allComputedArtifacts = allComputedArtifacts;
}

get requiredNumberOfArtifacts() {
return 1;
}

/* eslint-disable no-unused-vars */

/**
Expand All @@ -31,21 +35,103 @@ class ComputedArtifact {
throw new Error('compute_() not implemented for computed artifact ' + this.name);
}

/**
* This is a helper function for performing cache operations and is responsible for maintaing the
* internal cache structure. This function accepts a path of artifacts, used to find the correct
* nested cache object, and an operation to perform on that cache with the final key.
*
* The cache is structured with the first argument occupying the keys of the toplevel cache that point
* to the Map of keys for the second argument and so forth until the 2nd to last argument's Map points
* to result values rather than further nesting. In the simplest case of a single argument, there
* is no nesting and the keys point directly to result values.
*
* Map(
* argument1A -> Map(
* argument2A -> result1A2A
* argument2B -> result1A2B
* )
* argument1B -> Map(
* argument2A -> result1B2A
* )
* )
*
* @param {!Array<*>} artifacts
* @param {function(!Map, *)} cacheOperation
*/
_performCacheOperation(artifacts, cacheOperation) {
artifacts = artifacts.slice();

let cache = this._cache;
while (artifacts.length > 1) {
const nextKey = artifacts.shift();
if (cache.has(nextKey)) {
cache = cache.get(nextKey);
} else {
const nextCache = new Map();
cache.set(nextKey, nextCache);
cache = nextCache;
}
}

return cacheOperation(cache, artifacts.shift());
}

/**
* Performs a cache.has operation, see _performCacheOperation for more.
* @param {!Array<*>} artifacts
* @return {boolean}
*/
_cacheHas(artifacts) {
return this._performCacheOperation(artifacts, (cache, key) => cache.has(key));
}

/**
* Performs a cache.get operation, see _performCacheOperation for more.
* @param {!Array<*>} artifacts
* @return {*}
*/
_cacheGet(artifacts) {
return this._performCacheOperation(artifacts, (cache, key) => cache.get(key));
}

/**
* Performs a cache.set operation, see _performCacheOperation for more.
* @param {!Array<*>} artifacts
* @param {*} result
*/
_cacheSet(artifacts, result) {
return this._performCacheOperation(artifacts, (cache, key) => cache.set(key, result));
}

/**
* Asserts that the length of the array is the same as the number of inputs the class expects
* @param {!Array<*>} artifacts
*/
_assertCorrectNumberOfArtifacts(artifacts) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arity testing feels like overkill here. It seems like any error would be caught as arity errors usually are in JS (the derived class's compute_() would throw). It also rules out optional arguments, which I believe the cache setup in this PR should handle transparently (absent this check)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional arguments won't work because it relies on artifacts array being the same length for every call

consider

artifacts.requestMyComputed(1)
artifacts.requestMyComputed(1, 2)

we get a cannot read property has of undefined error because the cache map was created expecting a nesting level of 1 but needed 2, it seems like a reasonable sanity check and keeps the internal structure simple. if a computed artifact wants to have something as optional it can support passing in undefined as the n-th argument instead, but it'll have to be explicitly passed for now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yeah, we'd have to move to storing a value and a Map per node, which isn't so bad. IMO requiredNumberOfArtifacts is a code smell (this is a cache, not a type system), but since we don't have any audits that even want to use multiple arguments yet, I'm ok with waiting for a user if you don't want to implement now.

const actual = artifacts.length;
const expected = this.requiredNumberOfArtifacts;
if (actual !== expected) {
const className = this.constructor.name;
throw new Error(`${className} requires ${expected} artifacts but ${actual} were given`);
}
}

/* eslint-enable no-unused-vars */

/**
* Request a computed artifact, caching the result on the input artifact.
* @param {*} artifact
* @param {...*} artifacts
* @return {!Promise<*>}
*/
request(artifact) {
if (this._cache.has(artifact)) {
return Promise.resolve(this._cache.get(artifact));
request(...artifacts) {
this._assertCorrectNumberOfArtifacts(artifacts);
if (this._cacheHas(artifacts)) {
return Promise.resolve(this._cacheGet(artifacts));
}

const artifactPromise = Promise.resolve()
.then(_ => this.compute_(artifact, this._allComputedArtifacts));
this._cache.set(artifact, artifactPromise);
.then(_ => this.compute_(...artifacts, this._allComputedArtifacts));
this._cacheSet(artifacts, artifactPromise);

return artifactPromise;
}
Expand Down
44 changes: 41 additions & 3 deletions lighthouse-core/test/gather/computed/computed-artifact-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,41 @@ const assert = require('assert');
const ComputedArtifact = require('../../../gather/computed/computed-artifact');

class TestComputedArtifact extends ComputedArtifact {
constructor() {
super();
constructor(...args) {
super(...args);

this.lastArguments = [];
this.computeCounter = 0;
}

get name() {
return 'TestComputedArtifact';
}

compute_(_) {
compute_(...args) {
this.lastArguments = args;
return this.computeCounter++;
}
}

class MultipleInputArtifact extends TestComputedArtifact {
get requiredNumberOfArtifacts() {
return 2;
}
}

describe('ComputedArtifact base class', () => {
it('tests correct number of inputs', () => {
const singleInputArtifact = new TestComputedArtifact();
const multiInputArtifact = new MultipleInputArtifact();

return Promise.resolve()
.then(_ => singleInputArtifact.request(1))
.then(_ => multiInputArtifact.request(1, 2))
.then(_ => assert.throws(() => singleInputArtifact.request(1, 2)))
.then(_ => assert.throws(() => multiInputArtifact.request(1)));
});

it('caches computed artifacts', () => {
const testComputedArtifact = new TestComputedArtifact();

Expand All @@ -44,4 +63,23 @@ describe('ComputedArtifact base class', () => {
assert.equal(result, 1);
});
});

it('caches multiple input arguments', () => {
const mockComputed = {computed: true};
const computedArtifact = new MultipleInputArtifact(mockComputed);

const obj0 = {value: 1};
const obj1 = {value: 2};
const obj2 = {value: 3};

return computedArtifact.request(obj0, obj1)
.then(result => assert.equal(result, 0))
.then(_ => assert.deepEqual(computedArtifact.lastArguments, [obj0, obj1, mockComputed]))
.then(_ => computedArtifact.request(obj1, obj2))
.then(result => assert.equal(result, 1))
.then(_ => assert.deepEqual(computedArtifact.lastArguments, [obj1, obj2, mockComputed]))
.then(_ => computedArtifact.request(obj0, obj1))
.then(result => assert.equal(result, 0))
.then(_ => assert.deepEqual(computedArtifact.lastArguments, [obj1, obj2, mockComputed]));
});
});