Skip to content

Commit

Permalink
use Map for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Oct 27, 2023
1 parent ec249f8 commit 00e4637
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/core/src/blockchain/storage-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class RemoteStorageLayer implements StorageLayerProvider {
}

export class StorageLayer implements StorageLayerProvider {
readonly #store: Record<string, StorageValue | Promise<StorageValue>> = {}
readonly #store: Map<string, StorageValue | Promise<StorageValue>> = new Map()
readonly #keys: string[] = []
readonly #deletedPrefix: string[] = []
#parent?: StorageLayerProvider
Expand All @@ -134,8 +134,8 @@ export class StorageLayer implements StorageLayerProvider {
}

async get(key: string, cache: boolean): Promise<StorageValue | undefined> {
if (key in this.#store) {
return this.#store[key]
if (this.#store.has(key)) {
return this.#store.get(key)
}

if (this.#deletedPrefix.some((dp) => key.startsWith(dp))) {
Expand All @@ -145,7 +145,7 @@ export class StorageLayer implements StorageLayerProvider {
if (this.#parent) {
const val = this.#parent.get(key, false)
if (cache) {
this.#store[key] = val
this.#store.set(key, val)
}
return val
}
Expand All @@ -156,24 +156,24 @@ export class StorageLayer implements StorageLayerProvider {
set(key: string, value: StorageValue): void {
switch (value) {
case StorageValueKind.Deleted:
this.#store[key] = StorageValueKind.Deleted
this.#store.set(key, StorageValueKind.Deleted)
this.#removeKey(key)
break
case StorageValueKind.DeletedPrefix:
this.#deletedPrefix.push(key)
for (const k of this.#keys) {
if (k.startsWith(key)) {
this.#store[k] = StorageValueKind.Deleted
this.#store.set(k, StorageValueKind.Deleted)
this.#removeKey(k)
}
}
break
case undefined:
delete this.#store[key]
this.#store.delete(key)
this.#removeKey(key)
break
default:
this.#store[key] = value
this.#store.set(key, value)
this.#addKey(key)
break
}
Expand All @@ -195,7 +195,7 @@ export class StorageLayer implements StorageLayerProvider {
into.set(deletedPrefix, StorageValueKind.DeletedPrefix)
}

for (const [key, value] of Object.entries(this.#store)) {
for (const [key, value] of this.#store) {
into.set(key, await value)
}

Expand All @@ -212,10 +212,10 @@ export class StorageLayer implements StorageLayerProvider {
if (!this.#deletedPrefix.some((dp) => startKey.startsWith(dp))) {
const remote = (await this.#parent?.getKeysPaged(prefix, pageSize, startKey)) ?? []
for (const key of remote) {
if (this.#deletedPrefix.some((dp) => key.startsWith(dp))) {
if (this.#store.get(key) === StorageValueKind.Deleted) {
continue
}
if (this.#store[key] === StorageValueKind.Deleted) {
if (this.#deletedPrefix.some((dp) => key.startsWith(dp))) {
continue
}
this.#addKey(key)
Expand All @@ -242,7 +242,7 @@ export class StorageLayer implements StorageLayerProvider {
* Merge the storage layer into the given object, can be used to get sotrage diff.
*/
async mergeInto(into: Record<string, string | null>) {
for (const [key, maybeValue] of Object.entries(this.#store)) {
for (const [key, maybeValue] of this.#store) {
const value = await maybeValue
if (value === StorageValueKind.Deleted) {
into[key] = null
Expand Down

0 comments on commit 00e4637

Please sign in to comment.