Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
falsandtru committed Feb 29, 2024
1 parent 79ef60d commit af6048b
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 66 deletions.
110 changes: 88 additions & 22 deletions benchmark/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ describe('Benchmark:', function () {
it(`Clock simulation ${size.toLocaleString('en')} 10%`, 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 < size * 10; ++i) cache.set(random(), {});
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')} 10%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
Expand All @@ -235,7 +238,10 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.set(key, {});
}
benchmark(`ILRU simulation ${size.toLocaleString('en')} 10%`, () => {
const key = random();
cache.get(key) ?? cache.set(key, {});
Expand All @@ -245,7 +251,10 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`LRU simulation ${size.toLocaleString('en')} 10%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
Expand All @@ -255,7 +264,10 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`TRC-C simulation ${size.toLocaleString('en')} 10%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
Expand All @@ -265,7 +277,10 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`TRC-L simulation ${size.toLocaleString('en')} 10%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
Expand All @@ -275,7 +290,10 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`DWC simulation ${size.toLocaleString('en')} 10%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
Expand All @@ -289,7 +307,10 @@ describe('Benchmark:', function () {
it(`Clock simulation ${size.toLocaleString('en')} 50%`, 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 < size * 10; ++i) cache.set(random(), {});
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')} 50%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
Expand All @@ -299,7 +320,10 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.set(key, {});
}
benchmark(`ILRU simulation ${size.toLocaleString('en')} 50%`, () => {
const key = random();
cache.get(key) ?? cache.set(key, {});
Expand All @@ -309,7 +333,10 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`LRU simulation ${size.toLocaleString('en')} 50%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
Expand All @@ -319,7 +346,10 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`TRC-C simulation ${size.toLocaleString('en')} 50%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
Expand All @@ -329,7 +359,10 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`TRC-L simulation ${size.toLocaleString('en')} 50%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
Expand All @@ -339,7 +372,10 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 8; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
benchmark(`DWC simulation ${size.toLocaleString('en')} 50%`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {});
Expand All @@ -353,7 +389,10 @@ describe('Benchmark:', function () {
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 < size * 10; ++i) cache.set(random(), {});
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, {});
Expand All @@ -363,7 +402,10 @@ describe('Benchmark:', function () {
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) cache.set(random(), {});
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, {});
Expand All @@ -373,7 +415,10 @@ describe('Benchmark:', function () {
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) cache.set(random(), {});
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, {});
Expand All @@ -383,7 +428,10 @@ describe('Benchmark:', function () {
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) cache.set(random(), {});
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, {});
Expand All @@ -393,7 +441,10 @@ describe('Benchmark:', function () {
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) cache.set(random(), {});
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, {});
Expand All @@ -403,7 +454,10 @@ describe('Benchmark:', function () {
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) cache.set(random(), {});
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, {});
Expand All @@ -418,8 +472,14 @@ describe('Benchmark:', function () {
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) cache.set(random(), {});
for (let i = 0; i < size * 1; ++i) cache.set(random(), {}, { ttl: age });
for (let i = 0; i < size * 7; ++i) {
const key = random();
cache.get(key) ?? cache.set(key, {});
}
for (let i = 0; i < size * 1; ++i) {
const key = random();
cache.get(key) ?? cache.set(key, {}, { ttl: age });
}
benchmark(`ILRU simulation ${size.toLocaleString('en')} 90% expire`, () => {
const key = random();
cache.get(key) ?? cache.set(key, {}, { ttl: age });
Expand All @@ -429,8 +489,14 @@ 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) cache.set(random(), {});
for (let i = 0; i < size * 1; ++i) cache.set(random(), {}, { age: age });
for (let i = 0; i < size * 7; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {});
}
for (let i = 0; i < size * 1; ++i) {
const key = random();
cache.get(key) ?? cache.add(key, {}, { age: age });
}
benchmark(`DWC simulation ${size.toLocaleString('en')} 90% expire`, () => {
const key = random();
cache.get(key) ?? cache.add(key, {}, { age: age });
Expand Down
2 changes: 1 addition & 1 deletion benchmark/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { benchmark } from './benchmark';
import { List as IxList } from '../src/ixlist';
import { List } from '../src/list';
import { List } from '../src/clist';
import Yallist from 'yallist';

class Node {
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 af6048b

Please sign in to comment.