Repository navigation
Share transform results between graphs #2239
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c2a9742
Share transform results between graphs
robhogan 9927748
Share transform results when cacheStores is empty
robhogan 380e4b8
Describe releases during a build in SharedTransformResults
robhogan 72b3ab6
Describe entries that are never released in SharedTransformResults
robhogan b4ab254
Remove stale cacheStores note from unstable_shareTransformResults typ…
robhogan a1c7268
Release shared results of virtual modules
robhogan 2e76f50
Describe store-declined results and failed updates in SharedTransform…
robhogan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
packages/metro/src/DeltaBundler/SharedTransformResults.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @flow strict-local | ||
| * @format | ||
| * @oncall react_native | ||
| */ | ||
|
|
||
| import type {ReleasedModules, TransformResult} from './types'; | ||
|
|
||
| export type Computed<T> = Readonly<{ | ||
| // The key the result belongs to, which differs from the requested key if | ||
| // the file changed between hashing and transforming it. | ||
| key: Buffer, | ||
| result: TransformResult<T>, | ||
| }>; | ||
|
|
||
| type Entry<T> = Readonly<{ | ||
| ...Computed<T>, | ||
| keyString: string, | ||
| path: string, | ||
| }>; | ||
|
|
||
| /** | ||
| * Shares transform results in memory between callers that request the same | ||
| * transform cache key, i.e. between graphs with equal transform options. | ||
| * | ||
| * Entries use the cache stores' key, so they can only be stale where a store | ||
| * hit would be, and need no invalidation of their own. Each module, i.e. each | ||
| * file and set of transform options, holds only its latest result, so editing | ||
| * a file replaces its entry. `release` drops the entries at released paths | ||
| * whose output no live graph holds, and the next request for a dropped entry | ||
| * reads the cache stores or transforms again. A graph that is still building is | ||
| * not yet live, so a release during its build can drop an entry that it is | ||
| * about to hold. Later graphs then get a second copy of that module, until the | ||
| * module changes or the building graph ends. | ||
| * | ||
| * Only graphs that `DeltaBundler` registers release their paths. Entries added | ||
| * by graphs it never registers, i.e. those built by `getDependencies` and a | ||
| * first build that fails, stay until a later result for the same module | ||
| * replaces them or a live graph releases their path. This keeps prepended | ||
| * scripts, which every graph's revision holds, shared between graphs. | ||
| * | ||
| * Callers must treat `output` and `dependencies` as read-only, as their types | ||
| * declare, since every graph holding a result sees a write to it. | ||
| */ | ||
| export default class SharedTransformResults<T> { | ||
| #inFlight: Map<string, Promise<Computed<T>>> = new Map(); | ||
| #settled: Map<string, Entry<T>> = new Map(); | ||
| // Module keys by path, so a release only checks the released modules. | ||
| #moduleKeysByPath: Map<string, Set<string>> = new Map(); | ||
|
|
||
| /** | ||
| * `moduleKey` identifies the module, i.e. every input to the transform | ||
| * except the file's contents, `key` is the full transform cache key, and | ||
| * `path` is the module's path in graphs. | ||
| */ | ||
| async getOrCompute( | ||
| { | ||
| key, | ||
| moduleKey, | ||
| path, | ||
| }: Readonly<{key: Buffer, moduleKey: Buffer, path: string}>, | ||
| compute: () => Promise<Computed<T>>, | ||
| ): Promise<Computed<T>> { | ||
| const keyString = toKeyString(key); | ||
| const moduleKeyString = toKeyString(moduleKey); | ||
| const entry = this.#settled.get(moduleKeyString); | ||
| if (entry != null && entry.keyString === keyString) { | ||
| return {key: entry.key, result: entry.result}; | ||
| } | ||
|
|
||
| const pending = this.#inFlight.get(keyString); | ||
| if (pending != null) { | ||
| return pending; | ||
| } | ||
|
|
||
| const promise = compute().then( | ||
| computed => { | ||
| this.#inFlight.delete(keyString); | ||
| const result = { | ||
| dependencies: computed.result.dependencies, | ||
| output: computed.result.output, | ||
| }; | ||
| this.#settled.set(moduleKeyString, { | ||
| key: computed.key, | ||
| keyString: toKeyString(computed.key), | ||
| path, | ||
| result, | ||
| }); | ||
| let moduleKeys = this.#moduleKeysByPath.get(path); | ||
| if (moduleKeys == null) { | ||
| moduleKeys = new Set(); | ||
| this.#moduleKeysByPath.set(path, moduleKeys); | ||
| } | ||
| moduleKeys.add(moduleKeyString); | ||
| return {key: computed.key, result}; | ||
| }, | ||
| error => { | ||
| this.#inFlight.delete(keyString); | ||
| throw error; | ||
| }, | ||
| ); | ||
| this.#inFlight.set(keyString, promise); | ||
| return promise; | ||
| } | ||
|
|
||
| /** | ||
| * Drops the entries at `paths`, or every entry if `paths` is null, whose | ||
| * output isn't in `heldOutputs`. Results still in flight are unaffected. | ||
| */ | ||
| release({paths, heldOutputs}: ReleasedModules<T>): void { | ||
| const moduleKeyStrings = | ||
| paths == null | ||
| ? Array.from(this.#settled.keys()) | ||
| : paths.flatMap(path => | ||
| Array.from(this.#moduleKeysByPath.get(path) ?? []), | ||
| ); | ||
| for (const moduleKeyString of moduleKeyStrings) { | ||
| const entry = this.#settled.get(moduleKeyString); | ||
| if (entry != null && !heldOutputs.has(entry.result.output)) { | ||
| this.#settled.delete(moduleKeyString); | ||
| const moduleKeys = this.#moduleKeysByPath.get(entry.path); | ||
| moduleKeys?.delete(moduleKeyString); | ||
| if (moduleKeys?.size === 0) { | ||
| this.#moduleKeysByPath.delete(entry.path); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| countEntries(): number { | ||
| return this.#settled.size; | ||
| } | ||
| } | ||
|
|
||
| // 'binary' (latin1) is a lossless one-character-per-byte encoding. | ||
| function toKeyString(key: Buffer): string { | ||
| return key.toString('binary'); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit · Config type comment still says sharing is off when
cacheStoresis emptyCommit 9927748 made sharing independent of
cacheStores:sharesTransformResultsnow returns onlyconfig.transformer.unstable_shareTransformResults, and there's a new Transformer test for sharing withcacheStores: []. The comment onunstable_shareTransformResultsinTransformerConfigTstill says "Off whencacheStoresis empty". Anyone who reads it and setscacheStores: []to turn sharing off would still get sharing. Suggest removing that sentence, which would also matchdocs/Configuration.md.Evidence:
grep cacheStoreson the head:types.js:170is the only remaining reference to the old gating.Transformer.jssharesTransformResultsdoesn't readcacheStores, and the test 'with transformer.unstable_shareTransformResults and empty cacheStores, transforms once and shares the result' passes. · ida3f0bb8e