From cd2d5195f8a4a09f90feca016d7c87b9f4ef8c00 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Sat, 7 Mar 2026 16:17:46 +0100 Subject: [PATCH] [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 --- src/lib/native/NativeTree.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/lib/native/NativeTree.ts b/src/lib/native/NativeTree.ts index 984804e438..b05f00ff0b 100644 --- a/src/lib/native/NativeTree.ts +++ b/src/lib/native/NativeTree.ts @@ -3,7 +3,7 @@ import { Bookmark, Folder, ItemLocation, TItemLocation } from '../Tree' import Ordering from '../interfaces/Ordering' import CachingAdapter from '../adapters/Caching' import IAccountStorage from '../interfaces/AccountStorage' -import { BulkImportResource } from '../interfaces/Resource' +import { BulkImportResource, IHashSettings } from '../interfaces/Resource' export default class NativeTree extends CachingAdapter implements BulkImportResource { protected location: TItemLocation = ItemLocation.LOCAL @@ -11,24 +11,37 @@ export default class NativeTree extends CachingAdapter implements BulkImportReso private storage: IAccountStorage private readonly accountId: string private saveTimeout: any + private loaded = false constructor(storage:IAccountStorage) { super({}) this.storage = storage this.accountId = this.storage.accountId + this.loaded = false } async load():Promise { const {value: tree} = await Storage.get({key: `bookmarks[${this.accountId}].tree`}) const {value: highestId} = await Storage.get({key: `bookmarks[${this.accountId}].highestId`}) if (tree) { - const oldHash = this.bookmarksCache && await this.bookmarksCache.cloneWithLocation(false, this.location).hash(this.hashSettings) + // Make sure we use xxhash3 if we have to calculate hash for this + const hashSettings = { + preserveOrder: true, + hashFn: 'xxhash3', + } as IHashSettings + const oldHash = this.loaded && this.bookmarksCache && await this.bookmarksCache.cloneWithLocation(false, this.location).hash(hashSettings) this.bookmarksCache = Folder.hydrate(JSON.parse(tree)).copy(false) - const newHash = await this.bookmarksCache.hash(this.hashSettings) this.highestId = parseInt(highestId) - return oldHash && oldHash !== newHash + if (oldHash && this.loaded) { + const newHash = await this.bookmarksCache.hash(hashSettings) + return oldHash !== newHash + } else { + this.loaded = true + return false + } } else { await this.save() + this.loaded = true return false } }