Skip to content

perf(compiler-sfc): add cache for parsing sfc #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/compiler-sfc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
11 changes: 9 additions & 2 deletions packages/compiler-sfc/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
{
Expand All @@ -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,
Expand Down Expand Up @@ -101,7 +108,7 @@ export function parse(
if (needMap) {
// TODO source map
}
// TODO set cache
sourceToSFC.set(sourceKey, sfc)

return sfc
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,11 @@
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"
Expand Down