-
-
Notifications
You must be signed in to change notification settings - Fork 292
[native] perf(NativeTree): Don't calculate hash upon loading tree #2195
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,32 +3,45 @@ 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<typeof ItemLocation.LOCAL> { | ||||||||||||||||||||||||||||||||||||||
| protected location: TItemLocation = ItemLocation.LOCAL | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| 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<boolean> { | ||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+28
to
+31
|
||||||||||||||||||||||||||||||||||||||
| const hashSettings = { | |
| preserveOrder: true, | |
| hashFn: 'xxhash3', | |
| } as IHashSettings | |
| const hashSettings: IHashSettings = { | |
| ...(this.hashSettings ?? {}), | |
| // Fall back to previous default when not specified, but respect negotiated settings if present | |
| preserveOrder: this.hashSettings?.preserveOrder ?? true, | |
| hashFn: 'xxhash3', | |
| } |
Copilot
AI
Mar 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using a type assertion (} as IHashSettings), prefer a typed constant (const hashSettings: IHashSettings = ...). This preserves compile-time checking (e.g., if THashFunction changes) and avoids masking missing/extra fields.
| const hashSettings = { | |
| preserveOrder: true, | |
| hashFn: 'xxhash3', | |
| } as IHashSettings | |
| const hashSettings: IHashSettings = { | |
| preserveOrder: true, | |
| hashFn: 'xxhash3', | |
| } |
Copilot
AI
Mar 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parseInt(highestId) will produce NaN when the preference key is missing (Capacitor Preferences returns null) and also relies on implicit radix. Using a safe default (e.g., '0') and an explicit base 10 avoids corrupting this.highestId (which would break future ID generation).
| this.highestId = parseInt(highestId) | |
| const parsedHighestId = parseInt(highestId ?? '0', 10) | |
| this.highestId = Number.isNaN(parsedHighestId) ? 0 : parsedHighestId |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loadedis initialized inline (private loaded = false) and then immediately set tofalseagain in the constructor. The constructor assignment is redundant and can be removed to reduce noise.