Performance: Parallelize recursive directory copying - #2409
Conversation
b56e9a9 to
05becb2
Compare
fredrikekelund
left a comment
There was a problem hiding this comment.
Clever solution, @ivan-ottinger 👍 We should add p-limit@^3.1.0 to our package.json file before landing this PR, but I'll still approve it now to unblock you.
I went on a tangent exploring how we might use the limit.map API which is available in more recent version of p-limit, but ended up finding that it actually breaks in several ways (see my review comment).
There was a problem hiding this comment.
I was going to suggest we do something like my suggestion below, but apparently, there are several issues with this solution (see bottom):
const recursiveCopyLimiter = pLimit( 100 );
export async function recursiveCopyDirectory(
source: string,
destination: string
): Promise< void > {
await fsPromises.mkdir( destination, { recursive: true } );
const entries = await fsPromises.readdir( source, { withFileTypes: true } );
await recursiveCopyLimiter.map( entries, async ( entry ) => {
const sourcePath = path.join( source, entry.name );
const destinationPath = path.join( destination, entry.name );
if ( entry.isDirectory() ) {
await recursiveCopyDirectory( sourcePath, destinationPath );
} else if ( entry.isFile() ) {
await fsPromises.copyFile( sourcePath, destinationPath );
}
} );
}- We should add
p-limitto package.json, but something with how the more recent versions use ESM doesn't play nicely with our Vite config, and I end up getting errors like "pLimit is not a function" when testing my suggestion. Frustrating… - I thought that moving the limiter definition outside the function to control the overall parallelism was smart, but that won't work (see Recursive use of p-limit may cause silent task loss when inner tasks depend on limited concurrency sindresorhus/p-limit#91)
The solution to these problems, as I see it, is to leave the code as is and add p-limit@^3.1.0 to our package.json file.
|
Out of curiosity, did you compare performance between this approach and using the We're using that API elsewhere in our codebase. Might be nice to standardize on the most performant approach. |
c04a487 to
f185857
Compare
📊 Performance Test ResultsComparing d13094b vs trunk site-editor
site-startup
Results are median values from multiple test runs. Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change |
Good point! I have compared all three approaches on macOS. Using current → I have made the change in fbb9352. |
f6c54ec to
d13094b
Compare
fredrikekelund
left a comment
There was a problem hiding this comment.
LGTM 👍 Thanks for taking another look and improving consistency.
gcsecsey
left a comment
There was a problem hiding this comment.
Thanks @ivan-ottinger for improving the performance! The changes LGTM, and I found no regressions when testing both on Mac and Windows. 👍
|
Looking at the graphs in codevitals, this PR made the site creation a little bit slower it seems. |
If I'm not wrong, I initially had the metrics job run on each PR and you can dive into the logs to check. We do this in Gutenberg and else, if we accept a few more minutes to wait before PR merges |
|
@ivan-ottinger, the job which runs performance metrics on branch posts a GH comment with results. The one under this PR shows ~1s decrease for site creation. |
This reverts the changes from PR #2409 which replaced the custom recursiveCopyDirectory function with fs-extra's copy. Brings back the sequential recursive copy implementation.
This reverts the changes from PR #2409 which replaced the custom recursiveCopyDirectory function with fs-extra's copy. Brings back the sequential recursive copy implementation.
…2520) * Revert "Performance: Parallelize recursive directory copying (#2409)" This reverts the changes from PR #2409 which replaced the custom recursiveCopyDirectory function with fs-extra's copy. Brings back the sequential recursive copy implementation. * Fix formatting * Preserve always-copy behavior for sqlite-command from #2521 Keep the fix from PR #2521 that always copies sqlite-command files on startup to ensure files are complete, but use recursiveCopyDirectory instead of fs.copy as part of the revert.

Related issues
Proposed Changes
Compared to the current
trunkI have measured about 16% speed-up of the site creation process on macOS (about 1.2 s saving). On Windows the percentage is smaller (about 6%) - as the overall site creation process takes longer there.Testing Instructions
npm install && npm start.Pre-merge Checklist