Skip to content

Commit b7cba58

Browse files
committed
[native] perf(NativeTree): Don't calculate hash upon loading tree
and if we have to, make sure we use xxhash3 Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent 1df944f commit b7cba58

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/lib/native/NativeTree.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { Bookmark, Folder, ItemLocation, TItemLocation } from '../Tree'
33
import Ordering from '../interfaces/Ordering'
44
import CachingAdapter from '../adapters/Caching'
55
import IAccountStorage from '../interfaces/AccountStorage'
6-
import { BulkImportResource } from '../interfaces/Resource'
6+
import { BulkImportResource, IHashSettings } from '../interfaces/Resource'
77

88
export default class NativeTree extends CachingAdapter implements BulkImportResource<typeof ItemLocation.LOCAL> {
99
protected location: TItemLocation = ItemLocation.LOCAL
1010

1111
private storage: IAccountStorage
1212
private readonly accountId: string
1313
private saveTimeout: any
14+
private loaded = false
1415

1516
constructor(storage:IAccountStorage) {
1617
super({})
@@ -22,13 +23,28 @@ export default class NativeTree extends CachingAdapter implements BulkImportReso
2223
const {value: tree} = await Storage.get({key: `bookmarks[${this.accountId}].tree`})
2324
const {value: highestId} = await Storage.get({key: `bookmarks[${this.accountId}].highestId`})
2425
if (tree) {
25-
const oldHash = this.bookmarksCache && await this.bookmarksCache.cloneWithLocation(false, this.location).hash(this.hashSettings)
26+
// Make sure we use xxhash3 if we have to calculate hash for this
27+
const hashSettings: IHashSettings = {
28+
preserveOrder: true,
29+
hashFn: 'xxhash3',
30+
}
31+
let oldHash
32+
if (this.loaded && this.bookmarksCache) {
33+
oldHash = await this.bookmarksCache.cloneWithLocation(false, this.location).hash(hashSettings)
34+
}
2635
this.bookmarksCache = Folder.hydrate(JSON.parse(tree)).copy(false)
27-
const newHash = await this.bookmarksCache.hash(this.hashSettings)
28-
this.highestId = parseInt(highestId)
29-
return oldHash && oldHash !== newHash
36+
const parsedHighestId = parseInt(highestId ?? '0', 10)
37+
this.highestId = Number.isNaN(parsedHighestId) ? 0 : parsedHighestId
38+
if (oldHash && this.loaded) {
39+
const newHash = await this.bookmarksCache.hash(hashSettings)
40+
return oldHash !== newHash
41+
} else {
42+
this.loaded = true
43+
return false
44+
}
3045
} else {
3146
await this.save()
47+
this.loaded = true
3248
return false
3349
}
3450
}

0 commit comments

Comments
 (0)