From a896c60258fa42ecbf19cf08ba79766071580c60 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 13 Mar 2021 18:37:13 +0100 Subject: [PATCH 1/3] feat: support _cacheGroup --- src/lib/setupContext.js | 16 ++++++++++++++-- src/lib/sharedState.js | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib/setupContext.js b/src/lib/setupContext.js index 2e5cf1f..71af8fc 100644 --- a/src/lib/setupContext.js +++ b/src/lib/setupContext.js @@ -25,6 +25,7 @@ const { isBuffer } = require('util') let contextMap = sharedState.contextMap let configContextMap = sharedState.configContextMap let contextSourcesMap = sharedState.contextSourcesMap +let configHashMap = sharedState.configHashMap let env = sharedState.env // Earmarks a directory for our touch files. @@ -629,6 +630,8 @@ function setupContext(configOrPath) { let [tailwindConfig, userConfigPath, tailwindConfigHash] = getTailwindConfig(configOrPath) let isConfigFile = userConfigPath !== null + const { _cacheGroup = 'default', _hash } = tailwindConfig + let contextDependencies = new Set() // If there are no @tailwind rules, we don't consider this CSS file or it's dependencies @@ -656,8 +659,13 @@ function setupContext(configOrPath) { if (!contextDependenciesChanged) { // If this file already has a context in the cache and we don't need to // reset the context, return the cached context. - if (isConfigFile && contextMap.has(sourcePath)) { - return contextMap.get(sourcePath) + if (contextMap.has(sourcePath)) { + if ( + isConfigFile || + (_hash && configHashMap.has(_cacheGroup) && _hash === configHashMap.get(_cacheGroup)) + ) { + return contextMap.get(sourcePath) + } } // If the config used already exists in the cache, return that. @@ -669,6 +677,10 @@ function setupContext(configOrPath) { } } + if (_hash) { + configHashMap.set(_cacheGroup, _hash) + } + // If this source is in the context map, get the old context. // Remove this source from the context sources for the old context, // and clean up that context if no one else is using it. This can be diff --git a/src/lib/sharedState.js b/src/lib/sharedState.js index 51f4f61..dcce859 100644 --- a/src/lib/sharedState.js +++ b/src/lib/sharedState.js @@ -9,5 +9,6 @@ module.exports = { contextMap: new Map(), configContextMap: new Map(), contextSourcesMap: new Map(), + configHashMap: new Map(), contentMatchCache: new LRU({ maxSize: 25000 }), } From 8e12394239e9e01d0955c853eb9475c37624d8c2 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 13 Mar 2021 19:20:04 +0100 Subject: [PATCH 2/3] cache getTailwindConfig too --- src/lib/setupContext.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/lib/setupContext.js b/src/lib/setupContext.js index 71af8fc..e2a0c4c 100644 --- a/src/lib/setupContext.js +++ b/src/lib/setupContext.js @@ -147,6 +147,7 @@ function resolveConfigPath(pathOrConfig) { } let configPathCache = new LRU({ maxSize: 100 }) +let configObjCache = new LRU({ maxSize: 100 }) // Get the config object based on a path function getTailwindConfig(configOrPath) { @@ -171,11 +172,22 @@ function getTailwindConfig(configOrPath) { } // It's a plain object, not a path - let newConfig = resolveConfig( - configOrPath.config === undefined ? configOrPath : configOrPath.config - ) + const inputConfig = configOrPath.config === undefined ? configOrPath : configOrPath.config + const { _cacheKey, _cacheVersion } = inputConfig - return [newConfig, null, hash(newConfig)] + if (_cacheKey && _cacheVersion) { + const cached = configObjCache.get(_cacheKey) + if (cached && cached[0] === _cacheVersion) { + return cached[1] + } + } + + let newConfig = resolveConfig(inputConfig) + const res = [newConfig, null, hash(newConfig)] + if (_cacheKey && _cacheVersion) { + configObjCache.set(_cacheKey, [_cacheVersion, res]) + } + return res } let fileModifiedMap = new Map() @@ -630,7 +642,7 @@ function setupContext(configOrPath) { let [tailwindConfig, userConfigPath, tailwindConfigHash] = getTailwindConfig(configOrPath) let isConfigFile = userConfigPath !== null - const { _cacheGroup = 'default', _hash } = tailwindConfig + const { _cacheKey, _cacheVersion } = tailwindConfig let contextDependencies = new Set() @@ -662,11 +674,13 @@ function setupContext(configOrPath) { if (contextMap.has(sourcePath)) { if ( isConfigFile || - (_hash && configHashMap.has(_cacheGroup) && _hash === configHashMap.get(_cacheGroup)) + (_cacheKey && configHashMap.get(_cacheKey) === _cacheVersion) ) { + console.log('>> HIT') return contextMap.get(sourcePath) } } + console.log('>> MISS') // If the config used already exists in the cache, return that. if (configContextMap.has(tailwindConfigHash)) { @@ -677,8 +691,8 @@ function setupContext(configOrPath) { } } - if (_hash) { - configHashMap.set(_cacheGroup, _hash) + if (_cacheKey && _cacheVersion) { + configHashMap.set(_cacheKey, _cacheVersion) } // If this source is in the context map, get the old context. From c876037d09f5c33d6b7ec9a568594111773831de Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Mon, 22 Mar 2021 11:54:42 +0100 Subject: [PATCH 3/3] remove debug logs --- src/lib/setupContext.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib/setupContext.js b/src/lib/setupContext.js index e2a0c4c..d9a4803 100644 --- a/src/lib/setupContext.js +++ b/src/lib/setupContext.js @@ -676,11 +676,9 @@ function setupContext(configOrPath) { isConfigFile || (_cacheKey && configHashMap.get(_cacheKey) === _cacheVersion) ) { - console.log('>> HIT') return contextMap.get(sourcePath) } } - console.log('>> MISS') // If the config used already exists in the cache, return that. if (configContextMap.has(tailwindConfigHash)) {