Skip to content

Commit

Permalink
migrated binaryMap base from map to js objects, to fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiCraft committed Sep 18, 2024
1 parent fdbca7b commit 3a5d60b
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/binary/map.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BasicUintConstructable, Uint } from "./uint.js";
import { Dict } from "../utils/dataUtils.js";

abstract class BMapIteratorLike<T, M> implements IterableIterator<T> {

constructor(protected mapEntries: IterableIterator<M>) {}

public [Symbol.iterator]() { return this; }

public next(): IteratorResult<T> {
Expand All @@ -24,7 +26,7 @@ abstract class BMapIteratorLike<T, M> implements IterableIterator<T> {
}

class BMapEntriesIterator<K extends Uint, V> extends BMapIteratorLike<[K, V], [string, any]> {
constructor ( protected CLS: BasicUintConstructable<K>, mapEntries: IterableIterator<[string, V]>) {
constructor(protected CLS: BasicUintConstructable<K>, mapEntries: IterableIterator<[string, V]>) {
super(mapEntries);
}
protected _next(value: [string, V]): [K, V] {
Expand All @@ -33,7 +35,7 @@ class BMapEntriesIterator<K extends Uint, V> extends BMapIteratorLike<[K, V], [s
}

class BMapKeysIterator<K extends Uint> extends BMapIteratorLike<K, string> {
constructor (protected CLS: BasicUintConstructable<K>, mapEntries: IterableIterator<string>) {
constructor(protected CLS: BasicUintConstructable<K>, mapEntries: IterableIterator<string>) {
super(mapEntries);
}
protected _next(value: string): K {
Expand All @@ -42,7 +44,7 @@ class BMapKeysIterator<K extends Uint> extends BMapIteratorLike<K, string> {
}

class BMapValuesIterator<V> extends BMapIteratorLike<V, V> {
constructor (mapEntries: IterableIterator<V>) {
constructor(mapEntries: IterableIterator<V>) {
super(mapEntries);
}
protected _next(value: V): V {
Expand All @@ -58,13 +60,14 @@ export {
}


export abstract class AbstractBinaryMap<K extends Uint, V> extends Map<any, V> {
export abstract class AbstractBinaryMap<K extends Uint, V> {

private readonly store: Dict<V> = {};

constructor(
protected readonly CLS: BasicUintConstructable<K>,
entries?: readonly (readonly [K, V])[] | null
entries?: readonly (readonly [K, V])[]
) {
super();
if (entries) {
for (const [key, value] of entries) {
this.set(key, value);
Expand All @@ -73,38 +76,38 @@ export abstract class AbstractBinaryMap<K extends Uint, V> extends Map<any, V> {
}

public get(key: K) {
return super.get(key.toHex());
return this.store[key.toHex()];
}

public set(key: K, value: V) {
return super.set(key.toHex(), value);
return this.store[key.toHex()] = value;
}

public delete(key: K) {
return super.delete(key.toHex());
return delete this.store[key.toHex()];
}

public has(key: K) {
return super.has(key.toHex());
return key.toHex() in this.store;
}

public [Symbol.iterator]() {
return this.entries();
}
public entries() {
return new BMapEntriesIterator(this.CLS, super.entries());
return new BMapEntriesIterator(this.CLS, Object.entries(this.store).values());
}
public keys() {
return new BMapKeysIterator(this.CLS, super.keys());
return new BMapKeysIterator(this.CLS, Object.keys(this.store).values());
}
public values() {
return new BMapValuesIterator(super.values());
return new BMapValuesIterator(Object.values(this.store).values());
}

public forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any) {
super.forEach((value, key) => {
callbackfn.call(thisArg, value, this.CLS.from(key), this);
});
public forEach(callbackfn: (value: V, key: K) => void, thisArg?: any) {
for (const [key, value] of this.entries()) {
callbackfn.call(thisArg, value, key);
};
}

public get [Symbol.toStringTag]() {
Expand All @@ -113,7 +116,7 @@ export abstract class AbstractBinaryMap<K extends Uint, V> extends Map<any, V> {
}

export class UintMap<V> extends AbstractBinaryMap<Uint, V> {
constructor(entries?: readonly (readonly [Uint, V])[] | null) {
constructor(entries?: readonly (readonly [Uint, V])[]) {
super(Uint, entries);
}
}
Expand Down

0 comments on commit 3a5d60b

Please sign in to comment.