-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): asset existence check is slow for many assets (#25866)
When an application has many assets (think ~100) the existence check which seeks to scrub already existing assets from the work graph is starting to take a significant amount of time. Do those checks in parallel. Also in this PR: - Improve the printing of the work graph a little to make it slightly less unreadable if the graph gets large. - Print the graphviz representation of the work graph to the trace log if the graph gets stuck, for debugging purposes. - In the work graph builder, only add dependencies on stacks. Previously, it used to add dependencies on assets as if they were stacks (those were later on removed, but we didn't need to add them in the first place). ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
4 changed files
with
105 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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,44 @@ | ||
/** | ||
* Run a number of promise generators with max parallelism | ||
* | ||
* Order is not maintained between the input and output. | ||
*/ | ||
export async function parallelPromises<A>(n: number, promises: Array<() => Promise<A>>): Promise<Array<A>> { | ||
const ret = new Array<A>(); | ||
let count = 0; | ||
let error: Error | undefined; | ||
const queue = [...promises]; | ||
|
||
return new Promise((ok, ko) => { | ||
tick(); | ||
|
||
function tick() { | ||
if (count === 0 && error) { | ||
ko(error); | ||
return; | ||
} | ||
if (count === 0 && queue.length === 0) { | ||
ok(ret); | ||
return; | ||
} | ||
|
||
while (count < n && queue.length > 0 && !error) { | ||
const next = queue.shift(); | ||
if (next !== undefined) { | ||
start(next); | ||
} | ||
} | ||
} | ||
|
||
function start(fn: () => Promise<A>) { | ||
count += 1; | ||
fn() | ||
.then((result) => { ret.push(result); }) | ||
.catch((e) => { error = e; }) | ||
.finally(() => { | ||
count -= 1; | ||
tick(); | ||
}); | ||
} | ||
}); | ||
} |
This file contains 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 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 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,33 @@ | ||
import { parallelPromises } from '../../lib/util/parallel'; | ||
import { sleep } from '../util'; | ||
|
||
test('parallelPromises', async () => { | ||
const N = 4; | ||
const J = 100; | ||
|
||
let jobsDone = 0; | ||
let concurrent = 0; | ||
let maxConcurrent = 0; | ||
|
||
const jobs = range(J).map(() => async () => { | ||
concurrent += 1; | ||
maxConcurrent = Math.max(concurrent, maxConcurrent); | ||
await sleep(Math.round(Math.random() * 100)); | ||
concurrent -= 1; | ||
jobsDone += 1; | ||
}); | ||
|
||
await parallelPromises(N, jobs); | ||
|
||
expect(maxConcurrent).toBeLessThanOrEqual(N); | ||
expect(maxConcurrent).toBeGreaterThan(1); | ||
expect(jobsDone).toEqual(J); | ||
}); | ||
|
||
function range(n: number) { | ||
const ret = new Array<number>(); | ||
for (let i = 0; i < n; i++) { | ||
ret.push(i); | ||
} | ||
return ret; | ||
} |