perf(md): limit the compile cache's memory usage

add maxSize of 64MB to the markdown compile result LRUCache, such that
large pages do not grow this cache unboundedly.

change the cache key from JSON.stringify({ src, ts, relativePath })
(which not only included the entire source of each file in each key, but
also instantiated an object and called JSON.stringify), to a simpler
string concatenation with the sha256 hash of src.

Co-authored-by: Calum H. (IMB11) <contact@cal.engineer>
Based-on-patch-by: Calum H. (IMB11) <contact@cal.engineer>
pull/5342/head
Miroma 3 days ago
parent 3c5c0fdac4
commit f53a09739e
No known key found for this signature in database

@ -1,5 +1,6 @@
import { resolveTitleFromToken } from '@mdit-vue/shared'
import { LRUCache } from 'lru-cache'
import { hash } from 'node:crypto'
import fs from 'node:fs'
import path from 'node:path'
import { createDebug } from 'obug'
@ -24,7 +25,12 @@ import { getGitTimestamp } from './utils/getGitTimestamp'
import { processIncludes } from './utils/processIncludes'
const debug = createDebug('vitepress:md')
const cache = new LRUCache<string, MarkdownCompileResult>({ max: 1024 })
const cache = new LRUCache<string, MarkdownCompileResult>({
maxSize: 64 * 1024 * 1024,
sizeCalculation(value, key) {
return Math.max(1, 2 * (key.length + value.vueSrc.length))
}
})
const scriptRE = /<\/script>/
const scriptLangTsRE = /<\s*script[^>]*\blang=['"]ts['"][^>]*/
@ -51,8 +57,7 @@ export function clearCache(relativePath?: string) {
return
}
relativePath = JSON.stringify({ relativePath }).slice(1)
cache.find((_, key) => key.endsWith(relativePath!) && cache.delete(key))
cache.find((_, key) => key.endsWith(`:${relativePath}`) && cache.delete(key))
}
function normalizeDriveLetter(file: string) {
@ -122,7 +127,8 @@ export async function createMarkdownToVueRenderFn(
file = rewrites.get(normalizeDriveLetter(file)) || file
const relativePath = slash(path.relative(srcDir, file))
const cacheKey = JSON.stringify({ src, ts, relativePath })
const srcHash = hash('sha256', src, 'base64url')
const cacheKey = `${srcHash}:${ts}:${relativePath}`
if (options.cache !== false) {
const cached = cache.get(cacheKey)
if (cached) {

Loading…
Cancel
Save