Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
falsandtru committed Jun 28, 2024
1 parent 9ade9c5 commit 160b3e1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ assert(ratio2(10, [2, 0], [3, 0], 0) === 4000);
// @ts-ignore
class TLRU<T extends Entry<K, V>> {
constructor(
private readonly step: number = 2,
private readonly demotion: number = 2,
private readonly window: number = 0,
private readonly retrial: boolean = true,
) {
Expand All @@ -881,10 +881,10 @@ class TLRU<T extends Entry<K, V>> {
private extend(): void {
const { list } = this;
this.count = -max(
//list.length * this.step / 100 / max(this.count / list.length * this.step, 1) | 0,
(list.length - this.count) * this.step / 100 | 0,
//list.length * this.demotion / 100 / max(this.count / list.length * this.demotion, 1) | 0,
(list.length - this.count) * this.demotion / 100 | 0,
list.length * this.window / 100 - this.count | 0,
this.step && 1);
this.demotion && 1);
assert(this.count <= 0);
}
public unshift(entry: T): void {
Expand Down
4 changes: 2 additions & 2 deletions src/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Clock<K, V> implements IterableDict<K, V> {
// Capacity is rounded up to multiples of 32.
constructor(
private readonly capacity: number,
private readonly step: number = 8,
private readonly demotion: number = 8,
) {
assert(capacity > 0);
this.capacity = ((capacity - 1 | MASK) >>> 0) + 1;
Expand All @@ -29,7 +29,7 @@ export class Clock<K, V> implements IterableDict<K, V> {
private refs: Int32Array;
private hand = 0;
private stock = 0;
private readonly threshold = 100 / this.step | 0;
private readonly threshold = 100 / this.demotion | 0;
private $count = 0;
public get count(): number {
return this.$count;
Expand Down
10 changes: 5 additions & 5 deletions src/tlru.clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Entry<K, V> implements List.Node {
export class TLRU<K, V> implements IterableDict<K, V> {
constructor(
private readonly capacity: number,
private readonly step: number = 2,
private readonly demotion: number = 2,
private readonly window: number = 0,
private readonly retrial: boolean = true,
// ヒットにより前方が増えるためstep=100では不足する。
private readonly pure: boolean = step >= 100,
private readonly pure: boolean = demotion >= 100,
) {
assert(capacity > 0);
}
Expand All @@ -39,10 +39,10 @@ export class TLRU<K, V> implements IterableDict<K, V> {
// 1周できる
assert(this.count <= this.capacity);
this.count = -max(
//list.length * this.step / 100 / max(this.count / list.length * this.step, 1) | 0,
(list.length - this.count) * this.step / 100 | 0,
//list.length * this.demotion / 100 / max(this.count / list.length * this.demotion, 1) | 0,
(list.length - this.count) * this.demotion / 100 | 0,
list.length * this.window / 100 - this.count | 0,
this.step && 1);
this.demotion && 1);
assert(this.count <= 0);
}
private replace(key: K, value: V): void {
Expand Down
6 changes: 3 additions & 3 deletions src/tlru.lru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Entry<K, V> implements List.Node {
export class TLRU<K, V> implements IterableDict<K, V> {
constructor(
private readonly capacity: number,
private readonly step: number = 2,
private readonly demotion: number = 2,
private readonly window: number = 0,
private readonly retrial: boolean = true,
) {
Expand All @@ -42,8 +42,8 @@ export class TLRU<K, V> implements IterableDict<K, V> {
// 先頭の次からなので1周-1
assert(this.count < this.capacity);
this.count = -max(
//list.length * this.step / 100 / max(this.count / list.length * this.step, 1) | 0,
(list.length - this.count) * this.step / 100 | 0,
//list.length * this.demotion / 100 / max(this.count / list.length * this.demotion, 1) | 0,
(list.length - this.count) * this.demotion / 100 | 0,
list.length * this.window / 100 - this.count | 0,
1) - 1;
assert(this.count < 0);
Expand Down
2 changes: 1 addition & 1 deletion src/tlru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LRUは有無参照間の最近性を喪失しClockは有参照間の最近性を
TLRUはすべての最近性を保持する。
パラメータを調整しやすく用途に合わせてヒット率を上げやすい。
stepパラメータはヒットエントリを重み付けおよび保護しており
step=100で重み付けと保護なしの純粋なTLRUを設定できる。
demotion=100で重み付けと保護なしの純粋なTLRUを設定できる。
windowパラメータでSLRU同様捕捉可能最小再利用距離を設定できるが
降格区間内では捕捉可能再利用距離が半減しSLRUより短くなる。
DWCより高速かつ堅牢でアプリケーションのインメモリキャッシュなどの
Expand Down

0 comments on commit 160b3e1

Please sign in to comment.