-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paths3-fifo.ts
More file actions
145 lines (143 loc) · 3.38 KB
/
Copy paths3-fifo.ts
File metadata and controls
145 lines (143 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// https://jasony.me/publication/sosp23-s3fifo.pdf
import { min } from './alias';
import { Queue } from './queue';
class Node<K, V> {
constructor(
public key: K,
public value: V,
) {
}
public resident = true;
public freq = 0;
}
export class S3FIFO<K, V> {
constructor(
private readonly capacity: number,
) {
assert(capacity > 0);
}
private readonly capS = this.capacity * 0.1 >>> 0;
private readonly capM = this.capacity - this.capS;
private readonly dict = new Map<K, Node<K, V>>();
private readonly fifoS = new Queue<Node<K, V>>();
private readonly fifoM = new Queue<Node<K, V>>();
private readonly fifoG = new Queue<Node<K, undefined>>();
public get length(): number {
return this.fifoS.length + this.fifoM.length;
}
public get size(): number {
return this.fifoS.length + this.fifoM.length;
}
private read(x: Node<K, V>, counting = true): void {
if (x.resident) {
if (!counting) return;
x.freq = min(x.freq + 1, 3);
}
else {
this.insert(x);
x.freq = 0;
}
}
private insert(x: Node<K, V>): void {
if (this.length === this.capacity) {
this.evict();
}
if (x.resident) {
this.fifoS.push(x);
}
else {
x.resident = true;
this.fifoM.push(x);
}
}
private evict(): void {
if (this.fifoS.length >= this.capS) {
this.evictS();
}
else {
this.evictM();
}
}
private evictS(): void {
while (!this.fifoS.isEmpty()) {
const t = this.fifoS.pop()!;
if (t.freq > 1) {
this.fifoM.push(t);
if (this.fifoM.length >= this.capM) {
this.evictM();
}
}
else {
t.resident = false;
// @ts-expect-error
t.value = undefined;
if (this.fifoG.length >= this.capM) {
this.evictG();
}
this.fifoG.push(t as Node<K, undefined>);
return;
}
}
}
private evictM(): void {
while (!this.fifoM.isEmpty()) {
const t = this.fifoM.pop()!;
if (t.freq > 0) {
this.fifoM.push(t);
t.freq = t.freq - 1;
}
else {
this.dict.delete(t.key);
return;
}
}
}
private evictG(): void {
while (!this.fifoG.isEmpty()) {
const t = this.fifoG.pop()!;
if (!t.resident) {
this.dict.delete(t.key);
}
return;
}
}
public set(key: K, value: V): this {
const node = this.dict.get(key);
if (node === undefined) {
const node = new Node(key, value);
this.insert(node);
this.dict.set(key, node);
}
else {
node.value = value;
this.read(node, false);
}
assert(this.dict.size <= this.capacity + this.capM);
assert(this.length <= this.capacity);
assert(this.fifoG.length <= this.capM);
return this;
}
public get(key: K): V | undefined {
const { dict } = this;
const node = dict.get(key);
if (node === undefined || !node.resident) return;
this.read(node);
return node.value;
}
public has(key: K): boolean {
return this.dict.get(key)?.resident === true;
}
public clear(): void {
this.dict.clear();
this.fifoS.clear();
this.fifoM.clear();
this.fifoG.clear();
}
public *[Symbol.iterator](): Iterator<[K, V], undefined, undefined> {
for (const fifo of [this.fifoS, this.fifoM]) {
for (const { key, value } of fifo) {
yield [key, value];
}
}
}
}