From 4ad8da2fcdf280d662ca242f6f9ea33f62d5896a Mon Sep 17 00:00:00 2001 From: Yaroslav Surilov Date: Fri, 15 Dec 2023 01:04:33 +0200 Subject: [PATCH] fix(libs): fix OHW decorator counter, refactor --- fifo-cache/spec.ts | 3 ++- libs/decorators/detect-one-hit-wonder.ts | 34 +++++++++++++----------- libs/get-typed-array.ts | 2 +- rr-cache/spec.ts | 3 ++- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/fifo-cache/spec.ts b/fifo-cache/spec.ts index 37b5143..206daa6 100644 --- a/fifo-cache/spec.ts +++ b/fifo-cache/spec.ts @@ -1,5 +1,6 @@ import { describe, it } from 'node:test'; -import assert from 'node:assert'; +import assert from 'node:assert/strict'; +import FifoCache from './index'; describe('Test FIFO cache API', () => { it('Create FIFO cache instance', () => { diff --git a/libs/decorators/detect-one-hit-wonder.ts b/libs/decorators/detect-one-hit-wonder.ts index 0acb032..9f835e4 100644 --- a/libs/decorators/detect-one-hit-wonder.ts +++ b/libs/decorators/detect-one-hit-wonder.ts @@ -12,32 +12,34 @@ const TYPE_FILTER_CUCKOO = Symbol('TYPE_FILTER_CUCKOO'); const detectOneHitWonder = (() => { let filter; - return (threshold: number = 1, filterType: symbol = TYPE_FILTER_MAP) => { + return (filterType: symbol = TYPE_FILTER_MAP) => { return function (originalMethod: any, _context: ClassMethodDecoratorContext) { function replacementMethod(this: any, key: any, value?: any) { - const { capacity } = this.stats; + if (!filter) { + const { capacity } = this.stats; - switch (filterType) { - case TYPE_FILTER_MAP: - filter = new Map(); - break; - case TYPE_FILTER_BLOOM: - filter = new BloomFilter(capacity); - break; - case TYPE_FILTER_CUCKOO: - filter = new CuckooFilter(capacity); - break; + switch (filterType) { + case TYPE_FILTER_MAP: + filter = new Map(); + break; + case TYPE_FILTER_BLOOM: + filter = new BloomFilter(capacity); + break; + case TYPE_FILTER_CUCKOO: + filter = new CuckooFilter(capacity); + break; + } } - let n = filter.get(key) ?? 0; - - filter.set(key, ++n); switch (originalMethod.name) { case 'read': + filter.set(key, 1); return originalMethod.call(this, key); case 'add': - if (n > threshold) { + if (filter.get(key)) { return originalMethod.call(this, key, value); + } else { + filter.set(key, 1); } } } diff --git a/libs/get-typed-array.ts b/libs/get-typed-array.ts index bb74e7a..81ed707 100644 --- a/libs/get-typed-array.ts +++ b/libs/get-typed-array.ts @@ -11,7 +11,7 @@ function getTypedArray (n: number) { case n - 1 <= MAX_32BIT_UINT: return new Uint32Array(n); default: - return new Array(n); + return new Float64Array(n); } } diff --git a/rr-cache/spec.ts b/rr-cache/spec.ts index cc3372e..d904920 100644 --- a/rr-cache/spec.ts +++ b/rr-cache/spec.ts @@ -1,5 +1,6 @@ import { describe, it } from 'node:test'; -import assert from 'node:assert'; +import assert from 'node:assert/strict'; +import RRCache from './index'; describe('Test RR cache API', () => { it('Create RR cache instance', () => {