]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler-sfc): add cache for parsing sfc (#453)
authorQuincyChen <mail@quincychen.cn>
Mon, 18 Nov 2019 18:29:04 +0000 (02:29 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 18 Nov 2019 18:29:04 +0000 (13:29 -0500)
packages/compiler-sfc/package.json
packages/compiler-sfc/src/parse.ts
yarn.lock

index cf842880ce9f88061023d309d4bfc53ca9c429d4..013fc7e2c03212a1c781b81a47b6b972fb938020 100644 (file)
@@ -35,5 +35,8 @@
     "postcss": "^7.0.21",
     "postcss-selector-parser": "^6.0.2",
     "source-map": "^0.7.3"
+  },
+  "devDependencies": {
+    "@types/lru-cache": "^5.1.0"
   }
 }
index de8945ddaa1411075bf785784f1197b6143de8a0..7a5828d1277d5742b8037de468255e44c5c3478f 100644 (file)
@@ -7,6 +7,7 @@ import {
   SourceLocation
 } from '@vue/compiler-core'
 import { RawSourceMap } from 'source-map'
+import LRUCache from 'lru-cache'
 import { generateCodeFrame } from '@vue/shared'
 
 export interface SFCParseOptions {
@@ -48,6 +49,8 @@ export interface SFCDescriptor {
   customBlocks: SFCBlock[]
 }
 
+const SFC_CACHE_MAX_SIZE = 500
+const sourceToSFC = new LRUCache<string, SFCDescriptor>(SFC_CACHE_MAX_SIZE)
 export function parse(
   source: string,
   {
@@ -56,7 +59,11 @@ export function parse(
     sourceRoot = ''
   }: SFCParseOptions = {}
 ): SFCDescriptor {
-  // TODO check cache
+  const sourceKey = source + needMap + filename + sourceRoot
+  const cache = sourceToSFC.get(sourceKey)
+  if (cache) {
+    return cache
+  }
 
   const sfc: SFCDescriptor = {
     filename,
@@ -101,7 +108,7 @@ export function parse(
   if (needMap) {
     // TODO source map
   }
-  // TODO set cache
+  sourceToSFC.set(sourceKey, sfc)
 
   return sfc
 }
index 04b04a87750c0e3713b57b8155ed256f7bd91c57..7498feb023bf9eb01af69fa9760b4754d988d890 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
   dependencies:
     jest-diff "^24.3.0"
 
+"@types/lru-cache@^5.1.0":
+  version "5.1.0"
+  resolved "https://registry.yarnpkg.com/@types/lru-cache/-/lru-cache-5.1.0.tgz#57f228f2b80c046b4a1bd5cac031f81f207f4f03"
+  integrity sha512-RaE0B+14ToE4l6UqdarKPnXwVDuigfFv+5j9Dze/Nqr23yyuqdNvzcZi3xB+3Agvi5R4EOgAksfv3lXX4vBt9w==
+
 "@types/minimatch@*":
   version "3.0.3"
   resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"