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

core: computed artifact keys #13430

Merged
merged 8 commits into from
Nov 30, 2021
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
21 changes: 14 additions & 7 deletions lighthouse-core/computed/computed-artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,35 @@ const log = require('lighthouse-logger');
/**
* Decorate computableArtifact with a caching `request()` method which will
* automatically call `computableArtifact.compute_()` under the hood.
* @template {{name: string, compute_(artifacts: unknown, context: LH.Artifacts.ComputedContext): Promise<unknown>}} C
* @template {{name: string, compute_(dependencies: unknown, context: LH.Artifacts.ComputedContext): Promise<unknown>}} C
* @template {Array<keyof FirstParamType<C['compute_']>>} K
* @param {C} computableArtifact
* @param {(K & ([keyof FirstParamType<C['compute_']>] extends [K[number]] ? unknown : never)) | null} keys List of properties of `dependencies` used by `compute_`; other properties are filtered out. Use `null` to allow all properties. Ensures that only required properties are used for caching result.
*/
function makeComputedArtifact(computableArtifact) {
function makeComputedArtifact(computableArtifact, keys) {
adamraine marked this conversation as resolved.
Show resolved Hide resolved
// tsc (3.1) has more difficulty with template inter-references in jsdoc, so
// give types to params and return value the long way, essentially recreating
// polymorphic-this behavior for C.
/**
* Return an automatically cached result from the computed artifact.
* @param {FirstParamType<C['compute_']>} artifacts
* @param {FirstParamType<C['compute_']>} dependencies
* @param {LH.Artifacts.ComputedContext} context
* @return {ReturnType<C['compute_']>}
*/
const request = (artifacts, context) => {
const request = (dependencies, context) => {
const pickedDependencies = keys ?
Object.fromEntries(keys.map(key => [key, dependencies[key]])) :
dependencies;

// NOTE: break immutability solely for this caching-controller function.
const computedCache = /** @type {Map<string, ArbitraryEqualityMap>} */ (context.computedCache);
const computedName = computableArtifact.name;

const cache = computedCache.get(computedName) || new ArbitraryEqualityMap();
computedCache.set(computedName, cache);

const computed = /** @type {ReturnType<C['compute_']>|undefined} */ (cache.get(artifacts));
/** @type {ReturnType<C['compute_']>|undefined} */
const computed = cache.get(pickedDependencies);
if (computed) {
return computed;
}
Expand All @@ -41,8 +48,8 @@ function makeComputedArtifact(computableArtifact) {
log.time(status, 'verbose');

const artifactPromise = /** @type {ReturnType<C['compute_']>} */
(computableArtifact.compute_(artifacts, context));
cache.set(artifacts, artifactPromise);
(computableArtifact.compute_(pickedDependencies, context));
cache.set(pickedDependencies, artifactPromise);

artifactPromise.then(() => log.timeEnd(status)).catch(() => log.timeEnd(status));

Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/computed/critical-request-chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ class CriticalRequestChains {
}
}

module.exports = makeComputedArtifact(CriticalRequestChains);
module.exports = makeComputedArtifact(CriticalRequestChains, ['URL', 'devtoolsLog', 'trace']);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/image-records.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ class ImageRecords {
}
}

module.exports = makeComputedArtifact(ImageRecords);
module.exports = makeComputedArtifact(ImageRecords, ['ImageElements', 'networkRecords']);

2 changes: 1 addition & 1 deletion lighthouse-core/computed/js-bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ class JSBundles {
}
}

module.exports = makeComputedArtifact(JSBundles);
module.exports = makeComputedArtifact(JSBundles, ['ScriptElements', 'SourceMaps']);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/load-simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ class LoadSimulator {
}
}

module.exports = makeComputedArtifact(LoadSimulator);
module.exports = makeComputedArtifact(LoadSimulator, ['devtoolsLog', 'settings']);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/main-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ class MainResource {
}
}

module.exports = makeComputedArtifact(MainResource);
module.exports = makeComputedArtifact(MainResource, ['URL', 'devtoolsLog']);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/main-thread-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ class MainThreadTasks {
}
}

module.exports = makeComputedArtifact(MainThreadTasks);
module.exports = makeComputedArtifact(MainThreadTasks, null);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/manifest-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ class ManifestValues {
}
}

module.exports = makeComputedArtifact(ManifestValues);
module.exports = makeComputedArtifact(ManifestValues, ['InstallabilityErrors', 'WebAppManifest']);
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ class CumulativeLayoutShift {
}
}

module.exports = makeComputedArtifact(CumulativeLayoutShift);
module.exports = makeComputedArtifact(CumulativeLayoutShift, null);
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ class FirstContentfulPaintAllFrames extends NavigationMetric {
}
}

module.exports = makeComputedArtifact(FirstContentfulPaintAllFrames);
module.exports = makeComputedArtifact(
FirstContentfulPaintAllFrames,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/metrics/first-contentful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ class FirstContentfulPaint extends NavigationMetric {
}
}

module.exports = makeComputedArtifact(FirstContentfulPaint);
module.exports = makeComputedArtifact(
FirstContentfulPaint,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/metrics/first-meaningful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ class FirstMeaningfulPaint extends NavigationMetric {
}
}

module.exports = makeComputedArtifact(FirstMeaningfulPaint);
module.exports = makeComputedArtifact(
FirstMeaningfulPaint,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/metrics/interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ class Interactive extends NavigationMetric {
}
}

module.exports = makeComputedArtifact(Interactive);
module.exports = makeComputedArtifact(
Interactive,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);

/**
* @typedef TimePeriod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,7 @@ class LanternFirstContentfulPaint extends LanternMetric {
}
}

module.exports = makeComputedArtifact(LanternFirstContentfulPaint);
module.exports = makeComputedArtifact(
LanternFirstContentfulPaint,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@ class LanternFirstMeaningfulPaint extends LanternMetric {
}
}

module.exports = makeComputedArtifact(LanternFirstMeaningfulPaint);
module.exports = makeComputedArtifact(
LanternFirstMeaningfulPaint,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/metrics/lantern-interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,7 @@ class LanternInteractive extends LanternMetric {
}
}

module.exports = makeComputedArtifact(LanternInteractive);
module.exports = makeComputedArtifact(
LanternInteractive,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@ class LanternLargestContentfulPaint extends LanternMetric {
}
}

module.exports = makeComputedArtifact(LanternLargestContentfulPaint);
module.exports = makeComputedArtifact(
LanternLargestContentfulPaint,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,7 @@ class LanternMaxPotentialFID extends LanternMetricArtifact {
}
}

module.exports = makeComputedArtifact(LanternMaxPotentialFID);
module.exports = makeComputedArtifact(
LanternMaxPotentialFID,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/metrics/lantern-speed-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,7 @@ class LanternSpeedIndex extends LanternMetric {
}
}

module.exports = makeComputedArtifact(LanternSpeedIndex);
module.exports = makeComputedArtifact(
LanternSpeedIndex,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@ class LanternTotalBlockingTime extends LanternMetric {
}
}

module.exports = makeComputedArtifact(LanternTotalBlockingTime);
module.exports = makeComputedArtifact(
LanternTotalBlockingTime,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ class LargestContentfulPaintAllFrames extends NavigationMetric {
}
}

module.exports = makeComputedArtifact(LargestContentfulPaintAllFrames);
module.exports = makeComputedArtifact(
LargestContentfulPaintAllFrames,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/metrics/largest-contentful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ class LargestContentfulPaint extends NavigationMetric {
}
}

module.exports = makeComputedArtifact(LargestContentfulPaint);
module.exports = makeComputedArtifact(
LargestContentfulPaint,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/metrics/max-potential-fid.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ class MaxPotentialFID extends NavigationMetric {
}
}

module.exports = makeComputedArtifact(MaxPotentialFID);
module.exports = makeComputedArtifact(
MaxPotentialFID,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/metrics/speed-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ class SpeedIndex extends NavigationMetric {
}
}

module.exports = makeComputedArtifact(SpeedIndex);
module.exports = makeComputedArtifact(
SpeedIndex,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/metrics/timing-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,7 @@ class TimingSummary {
}
}

module.exports = makeComputedArtifact(TimingSummary);
module.exports = makeComputedArtifact(
TimingSummary,
['devtoolsLog', 'gatherContext', 'settings', 'trace']
);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/metrics/total-blocking-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,7 @@ class TotalBlockingTime extends ComputedMetric {
}
}

module.exports = makeComputedArtifact(TotalBlockingTime);
module.exports = makeComputedArtifact(
TotalBlockingTime,
['devtoolsLog', 'gatherContext', 'settings', 'simulator', 'trace']
);
4 changes: 2 additions & 2 deletions lighthouse-core/computed/module-duplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ModuleDuplication {
}

/**
* @param {LH.Artifacts} artifacts
* @param {Pick<LH.Artifacts, 'ScriptElements'|'SourceMaps'>} artifacts
* @param {LH.Artifacts.ComputedContext} context
*/
static async compute_(artifacts, context) {
Expand Down Expand Up @@ -133,4 +133,4 @@ class ModuleDuplication {
}
}

module.exports = makeComputedArtifact(ModuleDuplication);
module.exports = makeComputedArtifact(ModuleDuplication, ['ScriptElements', 'SourceMaps']);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/network-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ class NetworkAnalysis {
}
}

module.exports = makeComputedArtifact(NetworkAnalysis);
module.exports = makeComputedArtifact(NetworkAnalysis, null);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/network-records.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class NetworkRecords {
}
}

module.exports = makeComputedArtifact(NetworkRecords);
module.exports = makeComputedArtifact(NetworkRecords, null);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/page-dependency-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ class PageDependencyGraph {
}
}

module.exports = makeComputedArtifact(PageDependencyGraph);
module.exports = makeComputedArtifact(PageDependencyGraph, ['devtoolsLog', 'trace']);

/**
* @typedef {Object} NetworkNodeOutput
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/computed/processed-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class ProcessedNavigation {
}
}

module.exports = makeComputedArtifact(ProcessedNavigation);
module.exports = makeComputedArtifact(ProcessedNavigation, null);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/processed-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class ProcessedTrace {
}
}

module.exports = makeComputedArtifact(ProcessedTrace);
module.exports = makeComputedArtifact(ProcessedTrace, null);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/resource-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ class ResourceSummary {
}
}

module.exports = makeComputedArtifact(ResourceSummary);
module.exports = makeComputedArtifact(ResourceSummary, ['URL', 'devtoolsLog', 'budgets']);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/screenshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ class Screenshots {
}
}

module.exports = makeComputedArtifact(Screenshots);
module.exports = makeComputedArtifact(Screenshots, null);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/speedline.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ class Speedline {
}
}

module.exports = makeComputedArtifact(Speedline);
module.exports = makeComputedArtifact(Speedline, null);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/trace-of-tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ class TraceOfTab {
}

log.warn(`trace-of-tab`, `trace-of-tab is deprecated, use processed-trace / processed-navigation instead`); // eslint-disable-line max-len
module.exports = makeComputedArtifact(TraceOfTab);
module.exports = makeComputedArtifact(TraceOfTab, null);

2 changes: 1 addition & 1 deletion lighthouse-core/computed/unused-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,4 @@ class UnusedCSS {
}
}

module.exports = makeComputedArtifact(UnusedCSS);
module.exports = makeComputedArtifact(UnusedCSS, ['CSSUsage', 'URL', 'devtoolsLog']);
5 changes: 4 additions & 1 deletion lighthouse-core/computed/unused-javascript-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,7 @@ class UnusedJavascriptSummary {
}
}

module.exports = makeComputedArtifact(UnusedJavascriptSummary);
module.exports = makeComputedArtifact(
UnusedJavascriptSummary,
['bundle', 'scriptCoverages', 'url']
);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/user-timings.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ class UserTimings {
}
}

module.exports = makeComputedArtifact(UserTimings);
module.exports = makeComputedArtifact(UserTimings, null);
2 changes: 1 addition & 1 deletion lighthouse-core/computed/viewport-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ViewportMeta {
}
}

module.exports = makeComputedArtifact(ViewportMeta);
module.exports = makeComputedArtifact(ViewportMeta, null);

/**
* @typedef {object} ViewportMetaResult
Expand Down
Loading