-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,10 @@ class ComputedArtifact { | |
this._allComputedArtifacts = allComputedArtifacts; | ||
} | ||
|
||
get requiredNumberOfArtifacts() { | ||
return 1; | ||
} | ||
|
||
/* eslint-disable no-unused-vars */ | ||
|
||
/** | ||
|
@@ -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) { | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update jsdoc ("on the input artifacts.", There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* @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; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,22 +12,43 @@ 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the file is already using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while we're on this topic, why is that style a lot of places haha? is it the extra character? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
it predates the use of eslint on lighthouse #8 :) I think the original conversation was that it's nice to indicate that an argument is passed in but is ignored (borrowed from several other languages) and got stretched to cover even when there are no parameters (presumably because it's one fewer characters). Mocha does mess with that a bit, though. |
||
.then(() => multiInputArtifact.request(1, 2)) | ||
.then(() => { | ||
assert.throws(() => singleInputArtifact.request(1, 2)); | ||
assert.throws(() => multiInputArtifact.request(1)); | ||
}); | ||
}); | ||
|
||
it('caches computed artifacts', () => { | ||
const testComputedArtifact = new TestComputedArtifact(); | ||
|
||
|
@@ -44,4 +65,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])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does testing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nothing has been testing that compute actually received the proper arguments, this is a pretty critical thing considering leaving the original line untouched (aside from the variable rename) would have totally borked every computed artifact There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
well, the computed artifact subclass tests that don't take shortcuts are :) Anyway, that's fine. |
||
.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])); | ||
}); | ||
}); |
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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
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 inundefined
as the n-th argument instead, but it'll have to be explicitly passed for nowThere was a problem hiding this comment.
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.