Implement caching mechanism to speed-up formatting of Elm blocks in markdown files #3
+1,769
−19
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.
Prettier parsers and printers work synchronously until prettier/prettier#4459 is discussed and resolved. This means that formatting a markdown file with a large number of Elm blocks takes a while, because for each block a new
elm-format
is spawned in a sequence.This PR introduces a simple LRU caching mechanism that works on top of the filesystem. The cache speeds-up consequent formatting of the same text, even between independent prettier executions. Occasionally, garbage collection is invoked to protect cache directory from growing indefinitely.
Each new version of
prettier-plugin-elm
will use its own cached results, because the value of the version frompackage.json
is mixed with the text to format when generating cache key.By default, the cache is stored in
$TMPDIR/prettier-plugin-elm
, but this location can be changed with the environment variable. In practice, this is only needed during testing, so caching can be considered as zero-config.Caching can be extracted into a separate NPM module as it's pretty generic and I'm happy to do so if someone is interested in reusing it (just ping me).