Skip to content

Commit

Permalink
perf: improve performance by preventing unnecessary head data storage…
Browse files Browse the repository at this point in the history
… attempts
  • Loading branch information
izure1 committed Nov 19, 2024
1 parent 7bdf9f5 commit 547725d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@izure/serializable-bptree",
"version": "5.0.0",
"version": "5.0.1",
"description": "Store the B+tree flexibly, not only in-memory.",
"author": "izure1 <[email protected]>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serializable-bptree",
"version": "5.0.0",
"version": "5.0.1",
"description": "Store the B+tree flexibly, not only in-memory.",
"types": "./dist/types/index.d.ts",
"main": "./dist/cjs/index.cjs",
Expand Down
7 changes: 7 additions & 0 deletions src/BPTreeAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export class BPTreeAsync<K, V> extends BPTree<K, V> {
this.root = await this.getNode(keys[0])
this.root.parent = null
this.strategy.head.root = this.root.id
this._strategyDirty = true
this.bufferForNodeUpdate(this.root)
return
}
Expand Down Expand Up @@ -369,6 +370,7 @@ export class BPTreeAsync<K, V> extends BPTree<K, V> {
const root = await this._createNode(false, [node.id, pointer.id], [value])
this.root = root
this.strategy.head.root = root.id
this._strategyDirty = true
node.parent = root.id
pointer.parent = root.id
this.bufferForNodeCreate(root)
Expand Down Expand Up @@ -480,6 +482,10 @@ export class BPTreeAsync<K, V> extends BPTree<K, V> {
}

protected async commitHeadBuffer(): Promise<void> {
if (!this._strategyDirty) {
return
}
this._strategyDirty = false
await this.strategy.writeHead(this.strategy.head)
}

Expand Down Expand Up @@ -639,6 +645,7 @@ export class BPTreeAsync<K, V> extends BPTree<K, V> {

public async setHeadData(data: SerializableData): Promise<void> {
this.strategy.head.data = data
this._strategyDirty = true
await this.commitHeadBuffer()
}

Expand Down
7 changes: 7 additions & 0 deletions src/BPTreeSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export class BPTreeSync<K, V> extends BPTree<K, V> {
this.root = this.getNode(keys[0])
this.root.parent = null
this.strategy.head.root = this.root.id
this._strategyDirty = true
this.bufferForNodeUpdate(this.root)
return
}
Expand Down Expand Up @@ -369,6 +370,7 @@ export class BPTreeSync<K, V> extends BPTree<K, V> {
const root = this._createNode(false, [node.id, pointer.id], [value])
this.root = root
this.strategy.head.root = root.id
this._strategyDirty = true
node.parent = root.id
pointer.parent = root.id
this.bufferForNodeCreate(root)
Expand Down Expand Up @@ -480,6 +482,10 @@ export class BPTreeSync<K, V> extends BPTree<K, V> {
}

protected commitHeadBuffer(): void {
if (!this._strategyDirty) {
return
}
this._strategyDirty = false
this.strategy.writeHead(this.strategy.head)
}

Expand Down Expand Up @@ -639,6 +645,7 @@ export class BPTreeSync<K, V> extends BPTree<K, V> {

public setHeadData(data: SerializableData): void {
this.strategy.head.data = data
this._strategyDirty = true
this.commitHeadBuffer()
}

Expand Down
5 changes: 4 additions & 1 deletion src/base/BPTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ export abstract class BPTree<K, V> {
protected readonly nodes: InvertedWeakMap<string, BPTreeUnknownNode<K, V>>
protected order!: number
protected root!: BPTreeUnknownNode<K, V>


protected _strategyDirty: boolean
protected readonly _nodeCreateBuffer: Map<string, BPTreeUnknownNode<K, V>>
protected readonly _nodeUpdateBuffer: Map<string, BPTreeUnknownNode<K, V>>
protected readonly _nodeDeleteBuffer: Map<string, BPTreeUnknownNode<K, V>>


protected readonly verifierMap: Record<
keyof BPTreeCondition<V>,
(nodeValue: V, value: V) => boolean
Expand Down Expand Up @@ -117,6 +119,7 @@ export abstract class BPTree<K, V> {
}

protected constructor(strategy: SerializeStrategy<K, V>, comparator: ValueComparator<V>) {
this._strategyDirty = false
this._cachedRegexp = new InvertedWeakMap()
this._nodeCreateBuffer = new Map()
this._nodeUpdateBuffer = new Map()
Expand Down

0 comments on commit 547725d

Please sign in to comment.