Skip to content

Commit 878f437

Browse files
zonemeenbrc-dd
andauthoredNov 5, 2023
perf: reduce duplicate rendering in localSearch (#3170)
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent 37e4ab9 commit 878f437

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed
 

‎src/client/theme-default/components/VPLocalSearchBox.vue

+6-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
import type { ModalTranslations } from '../../../../types/local-search'
3232
import { pathToFile } from '../../app/utils'
3333
import { useData } from '../composables/data'
34+
import { LRUCache } from '../support/lru'
3435
import { createTranslate } from '../support/translation'
3536
3637
const emit = defineEmits<{
@@ -142,6 +143,8 @@ const mark = computedAsync(async () => {
142143
return markRaw(new Mark(resultsEl.value))
143144
}, null)
144145
146+
const cache = new LRUCache<string, Map<string, string>>(16) // 16 files
147+
145148
debouncedWatch(
146149
() => [searchIndex.value, filterText.value, showDetailedList.value] as const,
147150
async ([index, filterTextValue, showDetailedListValue], old, onCleanup) => {
@@ -163,13 +166,12 @@ debouncedWatch(
163166
? await Promise.all(results.value.map((r) => fetchExcerpt(r.id)))
164167
: []
165168
if (canceled) return
166-
const c = new Map<string, Map<string, string>>()
167169
for (const { id, mod } of mods) {
168170
const mapId = id.slice(0, id.indexOf('#'))
169-
let map = c.get(mapId)
171+
let map = cache.get(mapId)
170172
if (map) continue
171173
map = new Map()
172-
c.set(mapId, map)
174+
cache.set(mapId, map)
173175
const comp = mod.default ?? mod
174176
if (comp?.render || comp?.setup) {
175177
const app = createApp(comp)
@@ -209,7 +211,7 @@ debouncedWatch(
209211
210212
results.value = results.value.map((r) => {
211213
const [id, anchor] = r.id.split('#')
212-
const map = c.get(id)
214+
const map = cache.get(id)
213215
const text = map?.get(anchor) ?? ''
214216
for (const term in r.match) {
215217
terms.add(term)
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// adapted from https://stackoverflow.com/a/46432113/11613622
2+
3+
export class LRUCache<K, V> {
4+
private max: number
5+
private cache: Map<K, V>
6+
7+
constructor(max: number = 10) {
8+
this.max = max
9+
this.cache = new Map<K, V>()
10+
}
11+
12+
get(key: K): V | undefined {
13+
let item = this.cache.get(key)
14+
if (item !== undefined) {
15+
// refresh key
16+
this.cache.delete(key)
17+
this.cache.set(key, item)
18+
}
19+
return item
20+
}
21+
22+
set(key: K, val: V): void {
23+
// refresh key
24+
if (this.cache.has(key)) this.cache.delete(key)
25+
// evict oldest
26+
else if (this.cache.size === this.max) this.cache.delete(this.first()!)
27+
this.cache.set(key, val)
28+
}
29+
30+
first(): K | undefined {
31+
return this.cache.keys().next().value
32+
}
33+
}

0 commit comments

Comments
 (0)
Please sign in to comment.