Conversation
every emitted file paid its own recursive mkdir, so a site that emits a page, an og image and an asset per note does thousands of redundant syscalls per build. the output dir is cleaned exactly once per process in buildQuartz, so once a directory has been made it stays made. caches the in-flight promise rather than a boolean so concurrent writes into the same new directory can't race a half-finished mkdir. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
built with Refined Cloudflare Pages Action⚡ Cloudflare Pages Deployment
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces redundant filesystem work during builds by memoizing directory creation in the shared write() helper used by emitters, avoiding repeated mkdir(..., { recursive: true }) calls for directories that have already been created within the process.
Changes:
- Add a module-level cache (
ensuredDirs) that stores in-flight/completedmkdirpromises by directory path. - Update
write()to reuse the cached promise and await it before writing output files.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+21
to
+25
| let ensured = ensuredDirs.get(dir) | ||
| if (!ensured) { | ||
| ensured = fs.promises.mkdir(dir, { recursive: true }) | ||
| ensuredDirs.set(dir, ensured) | ||
| } |
This branch was successfully deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
write()inquartz/plugins/emitters/helpers.tsdoes a recursivemkdirfor every file it writes:Every emitter funnels through this. A site that emits a page, an og image and an asset per note does thousands of redundant
mkdirsyscalls per build, nearly all of them on directories that already exist.Change
Cache the directories we've already made. The output directory is cleaned exactly once per process —
buildQuartzdoesrm(output, ...)at line 79 and nothing else removes directories during a build — so once a directory has been made it stays made for the life of the process. Watch mode re-imports the whole build module on a source change, which resets the map along with everything else.The map holds the in-flight promise rather than a boolean, so two concurrent writes into the same new directory can't race a half-finished
mkdir.Verification
tsc --noEmitcleannpx quartz build -d docssucceeds, 313 files emittedindex.xml,sitemap.xml,plugins/encryptedpages-demo.html,static/encryptedContentIndex.json— but a control run of two builds of unmodified code differs in exactly the same four, so that's build timestamps and the per-build encryption salt, not this change.This is small on its own; it matters more once emitters write concurrently (see quartz-community/og-image#2).
Co-Authored-By: Claude Opus 5 (1M context) noreply@anthropic.com