Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
falsandtru committed Feb 28, 2024
1 parent 764cf41 commit 0d99641
Show file tree
Hide file tree
Showing 13 changed files with 494 additions and 132 deletions.
192 changes: 96 additions & 96 deletions benchmark/cache.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { benchmark } from './benchmark';
import { Clock } from '../src/clock';
import { LRU } from '../src/lru';
import { TLRU as TRCC } from '../src/tlru.clock';
import { LRU as TRCC } from '../src/lru2';
import { TLRU as TRCL } from '../src/tlru.lru';
import { Cache } from '../src/cache';
import { LRUCache } from 'lru-cache';
import { xorshift } from '../src/random';
import { captureTimers } from '../src/timer';

describe('Benchmark:', function () {
describe.only('Benchmark:', function () {
describe('Cache', function () {
it('Clock new', function (done) {
benchmark('Clock new', () => new Clock(10000), done);
Expand All @@ -34,6 +34,88 @@ describe('Benchmark:', function () {
benchmark('DWC new', () => new Cache(10000), done);
});

for (const size of [1e2, 1e3, 1e4, 1e5, 1e6]) {
const pzipf = (capacity: number, rng: () => number) => () =>
Math.floor((rng() * capacity) ** 2 / (85 * capacity / 100 | 0));
it(`Clock simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new Clock<number, object>(size);
const random = pzipf(Math.ceil(size / 32) * 32, xorshift.random(1));
for (let i = 0; i < Math.ceil(size / 32) * 32 * 10; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`Clock simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
}, done);
});

it(`ILRU simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new LRUCache<number, object>({ max: size });
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.set(key, {});
}
benchmark(`ILRU simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.set(key, {});
}, done);
});

it(`LRU simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new LRU<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`LRU simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
}, done);
});

it(`TRC-C simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new TRCC<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`TRC-C simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
}, done);
});

it(`TRC-L simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new TRCL<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`TRC-L simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
}, done);
});

it(`DWC simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new Cache<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`DWC simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
}, done);
});
}

for (const size of [1e2, 1e3, 1e4, 1e5, 1e6]) {
it(`Clock set miss ${size.toLocaleString('en')}`, function (done) {
const cache = new Clock<number, object>(size);
Expand Down Expand Up @@ -238,7 +320,7 @@ describe('Benchmark:', function () {
it(`ILRU simulation ${size.toLocaleString('en')} 10%`, function (done) {
const cache = new LRUCache<number, object>({ max: size });
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.set(key, {});
}
Expand All @@ -251,7 +333,7 @@ describe('Benchmark:', function () {
it(`LRU simulation ${size.toLocaleString('en')} 10%`, function (done) {
const cache = new LRU<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
Expand All @@ -264,7 +346,7 @@ describe('Benchmark:', function () {
it(`TRC-C simulation ${size.toLocaleString('en')} 10%`, function (done) {
const cache = new TRCC<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
Expand All @@ -277,7 +359,7 @@ describe('Benchmark:', function () {
it(`TRC-L simulation ${size.toLocaleString('en')} 10%`, function (done) {
const cache = new TRCL<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
Expand All @@ -290,7 +372,7 @@ describe('Benchmark:', function () {
it(`DWC simulation ${size.toLocaleString('en')} 10%`, function (done) {
const cache = new Cache<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
Expand Down Expand Up @@ -320,7 +402,7 @@ describe('Benchmark:', function () {
it(`ILRU simulation ${size.toLocaleString('en')} 50%`, function (done) {
const cache = new LRUCache<number, object>({ max: size });
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.set(key, {});
}
Expand All @@ -333,7 +415,7 @@ describe('Benchmark:', function () {
it(`LRU simulation ${size.toLocaleString('en')} 50%`, function (done) {
const cache = new LRU<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
Expand All @@ -346,7 +428,7 @@ describe('Benchmark:', function () {
it(`TRC-C simulation ${size.toLocaleString('en')} 50%`, function (done) {
const cache = new TRCC<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
Expand All @@ -359,7 +441,7 @@ describe('Benchmark:', function () {
it(`TRC-L simulation ${size.toLocaleString('en')} 50%`, function (done) {
const cache = new TRCL<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
Expand All @@ -372,7 +454,7 @@ describe('Benchmark:', function () {
it(`DWC simulation ${size.toLocaleString('en')} 50%`, function (done) {
const cache = new Cache<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
Expand All @@ -383,96 +465,14 @@ describe('Benchmark:', function () {
});
}

for (const size of [1e2, 1e3, 1e4, 1e5, 1e6]) {
const pzipf = (capacity: number, rng: () => number) => () =>
Math.floor((rng() * capacity) ** 2 / (85 * capacity / 100 | 0));
it(`Clock simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new Clock<number, object>(size);
const random = pzipf(Math.ceil(size / 32) * 32, xorshift.random(1));
for (let i = 0; i < Math.ceil(size / 32) * 32 * 10; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`Clock simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
}, done);
});

it(`ILRU simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new LRUCache<number, object>({ max: size });
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
const key = random();
cache.get(key) ?? cache.set(key, {});
}
benchmark(`ILRU simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.set(key, {});
}, done);
});

it(`LRU simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new LRU<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`LRU simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
}, done);
});

it(`TRC-C simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new TRCC<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`TRC-C simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
}, done);
});

it(`TRC-L simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new TRCL<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`TRC-L simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
}, done);
});

it(`DWC simulation ${size.toLocaleString('en')} 90%`, function (done) {
const cache = new Cache<number, object>(size);
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 10; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`DWC simulation ${size.toLocaleString('en')} 90%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
}, done);
});
}

for (const size of [1e2, 1e3, 1e4, 1e5, 1e6]) {
const pzipf = (capacity: number, rng: () => number) => () =>
Math.floor((rng() * capacity) ** 2 / (85 * capacity / 100 | 0));
const age = 1000;
it(`ILRU simulation ${size.toLocaleString('en')} 90% expire`, captureTimers(function (done) {
const cache = new LRUCache<number, object>({ max: size, ttlAutopurge: true });
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 9; ++i) {
for (let i = 0; i < size * 7; ++i) {
const key = random();
cache.get(key) ?? cache.set(key, {});
}
Expand All @@ -489,7 +489,7 @@ describe('Benchmark:', function () {
it(`DWC simulation ${size.toLocaleString('en')} 90% expire`, function (done) {
const cache = new Cache<number, object>(size, { eagerExpiration: true });
const random = pzipf(size, xorshift.random(1));
for (let i = 0; i < size * 9; ++i) {
for (let i = 0; i < size * 7; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
Expand Down
2 changes: 1 addition & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { max, min } from './alias';
import { now } from './chrono';
import { IterableDict } from './dict';
import { List } from './list';
import { List } from './clist';
import { Heap } from './heap';
import { extend } from './assign';

Expand Down
1 change: 1 addition & 0 deletions src/clist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './list/clist';
44 changes: 44 additions & 0 deletions src/list/clist.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { List } from './clist';

describe('Unit: lib/clist', () => {
describe('List', () => {
it('', () => {
class Node<T> {
constructor(public value: T) {
}
public next?: this;
public prev?: this;
}
const list = new List<Node<number>>();

assert(list.length === 0);
assert(list.shift() === undefined);
assert(list.pop() === undefined);
assert(list.length === 0);
assert(list.head === undefined);
assert(list.length === 0);

list.unshift(new Node(1));
assert(list.length === 1);
assert(list.shift()?.value === 1);
assert(list.length === 0);
assert(list.shift() === undefined);
assert(list.length === 0);

list.push(new Node(1));
assert(list.length === 1);
assert(list.pop()?.value === 1);
assert(list.length === 0);
assert(list.pop() === undefined);
assert(list.length === 0);

list.push(new Node(1));
list.unshift(new Node(0));
list.push(new Node(2));
assert(list.length === 3);
assert(list.shift()?.value === 0);
assert(list.pop()?.value === 2);
assert(list.length === 1);
});
});
});
Loading

0 comments on commit 0d99641

Please sign in to comment.