@the-w1zard/singularity 1.0.11 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.html +558 -4
- package/README.md +664 -6
- package/dist/comps/DISRUPTOR_CRYPTO.js +293 -0
- package/dist/comps/binary.js +611 -0
- package/dist/comps/byte_id.js +40 -0
- package/dist/comps/cell-worker.js +120 -0
- package/dist/comps/cell.js +2478 -0
- package/dist/comps/create_live_self.js +22 -0
- package/dist/comps/queue.js +532 -0
- package/dist/comps/vm-types.js +1 -0
- package/dist/db/client/kv_idb.js +590 -0
- package/dist/db/server/bench.js +28 -0
- package/dist/db/server/binary.js +43 -0
- package/dist/db/server/constants.js +8 -0
- package/dist/db/server/crc.js +15 -0
- package/dist/db/server/db.js +1475 -0
- package/dist/db/server/manifest.js +34 -0
- package/dist/db/server/pages.js +100 -0
- package/dist/db/server/storage-worker.js +152 -0
- package/dist/db/server/types.js +14 -0
- package/dist/db/server/wal.js +81 -0
- package/dist/index.js +1 -0
- package/dist/signals.js +18435 -0
- package/dist/y.js +1324 -0
- package/package.json +11 -6
package/dist/y.js
ADDED
|
@@ -0,0 +1,1324 @@
|
|
|
1
|
+
// bus-bench.ts
|
|
2
|
+
// FIGHT=... bun --expose-gc bus-bench.ts
|
|
3
|
+
// import { SERVER_KVDB } from "./db/server/db";
|
|
4
|
+
import { darkState, root } from "./signals";
|
|
5
|
+
const CAPACITY = 1 << 18;
|
|
6
|
+
const DURATION = 12;
|
|
7
|
+
const REPORT_MS = 12;
|
|
8
|
+
const BATCH_SIZE = 100_000; // 500_000
|
|
9
|
+
const TOTAL_BATCHES = 100; // 500
|
|
10
|
+
const RUN_BARE_METAL_CPU_TEST = true;
|
|
11
|
+
const pid = 1;
|
|
12
|
+
let seq = 0;
|
|
13
|
+
const v = `${++seq}`;
|
|
14
|
+
const FIGHT = process.env.FIGHT ?? "bare";
|
|
15
|
+
const r = "THE_WIZARD_ROOT_REG";
|
|
16
|
+
const m = "SAURON_MASTER_REG";
|
|
17
|
+
const s = "SAURON_SLAVE_REG";
|
|
18
|
+
const k = "__SAURON__";
|
|
19
|
+
const handler = (p) => p.p + 1;
|
|
20
|
+
const handle2 = (p) => p.p + 1;
|
|
21
|
+
const handleFlat = (p) => p.p + 1;
|
|
22
|
+
// ─────────────────────────────────────────────────────────────
|
|
23
|
+
// BENCH
|
|
24
|
+
// ─────────────────────────────────────────────────────────────
|
|
25
|
+
const dur = 1000 * DURATION;
|
|
26
|
+
const report = 1000 * REPORT_MS;
|
|
27
|
+
function BARE_METAL_CPU(name, write) {
|
|
28
|
+
const batchTimes = new Float64Array(TOTAL_BATCHES);
|
|
29
|
+
for (let i = 0; i < 2_000_000; i++)
|
|
30
|
+
write();
|
|
31
|
+
const globalStart = process.hrtime.bigint();
|
|
32
|
+
for (let b = 0; b < TOTAL_BATCHES; b++) {
|
|
33
|
+
const batchStart = process.hrtime.bigint();
|
|
34
|
+
for (let i = 0; i < BATCH_SIZE; i++)
|
|
35
|
+
write();
|
|
36
|
+
batchTimes[b] = Number(process.hrtime.bigint() - batchStart);
|
|
37
|
+
}
|
|
38
|
+
const globalEnd = process.hrtime.bigint();
|
|
39
|
+
const totalOps = BATCH_SIZE * TOTAL_BATCHES;
|
|
40
|
+
let bestOpsPerSec = 0;
|
|
41
|
+
for (let b = 0; b < TOTAL_BATCHES; b++) {
|
|
42
|
+
const ops = BATCH_SIZE / (batchTimes[b] / 1e9);
|
|
43
|
+
if (ops > bestOpsPerSec)
|
|
44
|
+
bestOpsPerSec = ops;
|
|
45
|
+
}
|
|
46
|
+
const avg = totalOps / (Number(globalEnd - globalStart) / 1e9);
|
|
47
|
+
console.log(`------------ ${name} ---------------`);
|
|
48
|
+
console.log(`Peak (Best Batch): ${(bestOpsPerSec / 1e6).toFixed(2)} M ops/sec`);
|
|
49
|
+
console.log(`Average: ${(avg / 1e6).toFixed(2)} M ops/sec`);
|
|
50
|
+
console.log(`---------------------------------------------------------`);
|
|
51
|
+
}
|
|
52
|
+
3;
|
|
53
|
+
function BENCH(name, write) {
|
|
54
|
+
let windowOps = 0, totalOps = 0, best = 0, elapsedTotal = 0;
|
|
55
|
+
let start = process.hrtime.bigint();
|
|
56
|
+
let windowStart = start;
|
|
57
|
+
let cpuStart = process.cpuUsage();
|
|
58
|
+
console.log(`\n${name}`);
|
|
59
|
+
console.log("sec | M writes/s | best M/s | avg M/s | CPU %");
|
|
60
|
+
while (elapsedTotal < dur) {
|
|
61
|
+
for (let i = 0; i < 100_000; i++)
|
|
62
|
+
write();
|
|
63
|
+
windowOps += 100_000;
|
|
64
|
+
totalOps += 100_000;
|
|
65
|
+
const now = process.hrtime.bigint();
|
|
66
|
+
const elapsedUs = Number(now - windowStart) / 1_000;
|
|
67
|
+
elapsedTotal = Number(now - start) / 1e6;
|
|
68
|
+
if (elapsedUs >= report * 1_000) {
|
|
69
|
+
const mp = windowOps / elapsedUs;
|
|
70
|
+
const avg = totalOps / (Number(now - start) / 1e9) / 1e6;
|
|
71
|
+
const cpuNow = process.cpuUsage();
|
|
72
|
+
const cpuUs = cpuNow.user - cpuStart.user + cpuNow.system - cpuStart.system;
|
|
73
|
+
best = Math.max(best, mp);
|
|
74
|
+
console.log(String(Math.floor(elapsedTotal / 1_000)).padStart(3), "|", mp.toFixed(2).padStart(10), "|", best.toFixed(2).padStart(8), "|", avg.toFixed(7).padStart(7), "|", ((cpuUs / elapsedUs) * 100).toFixed(1).padStart(5));
|
|
75
|
+
windowOps = 0;
|
|
76
|
+
windowStart = now;
|
|
77
|
+
cpuStart = cpuNow;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const bench = RUN_BARE_METAL_CPU_TEST === false ? BENCH : BARE_METAL_CPU;
|
|
82
|
+
export const rv = (v) => typeof v === "function" ? v() : v;
|
|
83
|
+
export const sameValue = (a, b) => Object.is(a, b) || (a instanceof Date && b instanceof Date && +a === +b);
|
|
84
|
+
export class M2 extends Map {
|
|
85
|
+
// public g = this.get;
|
|
86
|
+
// public h = this.has;
|
|
87
|
+
// public d = this.delete;
|
|
88
|
+
// public c = this.clear
|
|
89
|
+
f = this.forEach; // [FOR EACH]
|
|
90
|
+
#subs;
|
|
91
|
+
#hist;
|
|
92
|
+
#histLimit = 0;
|
|
93
|
+
#strict = false;
|
|
94
|
+
#iterating = 0;
|
|
95
|
+
#batching = 0;
|
|
96
|
+
#pending;
|
|
97
|
+
#derived;
|
|
98
|
+
#dependents;
|
|
99
|
+
#emit(e) {
|
|
100
|
+
if (this.#batching > 0) {
|
|
101
|
+
// coalesce: last write per key wins, keep prev from first event
|
|
102
|
+
const pending = this.#pending.get(e.k);
|
|
103
|
+
this.#pending.set(e.k, pending ? { ...e, prev: pending.prev, batch: true } : { ...e, batch: true });
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
this.#record(e);
|
|
107
|
+
this.#subs?.forEach((sub) => {
|
|
108
|
+
try {
|
|
109
|
+
sub(e);
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
process.emitWarning(err);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
#record(e) {
|
|
117
|
+
if (!this.#hist)
|
|
118
|
+
return;
|
|
119
|
+
this.#hist.push(e);
|
|
120
|
+
if (this.#hist.length > this.#histLimit) {
|
|
121
|
+
this.#hist.splice(0, this.#hist.length - this.#histLimit);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
#invalidate(k) {
|
|
125
|
+
const deps = this.#dependents?.get(k);
|
|
126
|
+
if (!deps)
|
|
127
|
+
return;
|
|
128
|
+
for (const dk of deps) {
|
|
129
|
+
const d = this.#derived?.get(dk);
|
|
130
|
+
if (!d || d.dirty)
|
|
131
|
+
continue;
|
|
132
|
+
d.dirty = true;
|
|
133
|
+
this.#emit({ op: "dirty", k: dk, has: d.has });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
#computeDerived(k, d) {
|
|
137
|
+
const vals = d.deps.map((dep) => this.g(dep));
|
|
138
|
+
const next = d.fn(...vals);
|
|
139
|
+
const had = d.has;
|
|
140
|
+
const prev = d.value;
|
|
141
|
+
d.value = next;
|
|
142
|
+
d.has = true;
|
|
143
|
+
d.dirty = false;
|
|
144
|
+
if (!had || !sameValue(prev, next)) {
|
|
145
|
+
this.#emit({
|
|
146
|
+
op: had ? "upd" : "set",
|
|
147
|
+
k,
|
|
148
|
+
v: next,
|
|
149
|
+
prev: prev,
|
|
150
|
+
has: true,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return next;
|
|
154
|
+
}
|
|
155
|
+
#assertNotIterating(op) {
|
|
156
|
+
if (this.#strict && this.#iterating > 0) {
|
|
157
|
+
throw new Error(`M2I.${op}() during iteration. Use filterKV(), collect keys first, or disable $strict().`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
[Symbol.iterator]() { return this.entries(); }
|
|
161
|
+
on(fn) {
|
|
162
|
+
(this.#subs ??= new Set()).add(fn);
|
|
163
|
+
return () => this.off(fn);
|
|
164
|
+
}
|
|
165
|
+
off(fn) {
|
|
166
|
+
this.#subs?.delete(fn);
|
|
167
|
+
}
|
|
168
|
+
batch(fn) {
|
|
169
|
+
this.#batching++;
|
|
170
|
+
this.#pending ??= new M2();
|
|
171
|
+
try {
|
|
172
|
+
fn();
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
this.#batching--;
|
|
176
|
+
if (this.#batching === 0) {
|
|
177
|
+
const events = [...this.#pending.values()];
|
|
178
|
+
this.#pending = undefined;
|
|
179
|
+
for (const e of events) {
|
|
180
|
+
this.#record(e);
|
|
181
|
+
this.#subs?.forEach(sub => sub(e));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
s(v, k) {
|
|
188
|
+
this.#assertNotIterating("s");
|
|
189
|
+
const next = rv(v);
|
|
190
|
+
const has = this.h(k);
|
|
191
|
+
const prev = has ? this.g(k) : undefined;
|
|
192
|
+
super.set(k, next);
|
|
193
|
+
this.#invalidate(k);
|
|
194
|
+
this.#emit({
|
|
195
|
+
op: "set",
|
|
196
|
+
k,
|
|
197
|
+
v: next,
|
|
198
|
+
prev: prev,
|
|
199
|
+
has,
|
|
200
|
+
});
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
mutate(k, fn) {
|
|
204
|
+
this.#assertNotIterating("mutate");
|
|
205
|
+
if (!this.h(k))
|
|
206
|
+
return undefined;
|
|
207
|
+
const old = this.g(k);
|
|
208
|
+
const next = fn(old, true);
|
|
209
|
+
this.s(next, k);
|
|
210
|
+
return next;
|
|
211
|
+
}
|
|
212
|
+
d(k) {
|
|
213
|
+
this.#assertNotIterating("delete");
|
|
214
|
+
if (!this.h(k))
|
|
215
|
+
return false;
|
|
216
|
+
const prev = this.g(k);
|
|
217
|
+
const out = super.delete(k);
|
|
218
|
+
this.#invalidate(k);
|
|
219
|
+
this.#emit({
|
|
220
|
+
op: "del",
|
|
221
|
+
k,
|
|
222
|
+
prev,
|
|
223
|
+
has: false,
|
|
224
|
+
});
|
|
225
|
+
return out;
|
|
226
|
+
}
|
|
227
|
+
c() {
|
|
228
|
+
this.#assertNotIterating("clear");
|
|
229
|
+
const entries = [...this.entries()];
|
|
230
|
+
super.clear();
|
|
231
|
+
for (const [k, prev] of entries) {
|
|
232
|
+
this.#invalidate(k);
|
|
233
|
+
this.#emit({ op: "del", k, prev, has: false });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
dv(k, deps, fn) {
|
|
237
|
+
(this.#derived ??= new M2())
|
|
238
|
+
.set(k, {
|
|
239
|
+
deps,
|
|
240
|
+
fn,
|
|
241
|
+
dirty: true,
|
|
242
|
+
has: false,
|
|
243
|
+
});
|
|
244
|
+
for (const dep of deps) {
|
|
245
|
+
(this.#dependents ??= new M2())
|
|
246
|
+
.getOrM(() => new Set(), dep)
|
|
247
|
+
.add(k);
|
|
248
|
+
}
|
|
249
|
+
return this;
|
|
250
|
+
}
|
|
251
|
+
dirty(...keys) {
|
|
252
|
+
for (const k of keys) {
|
|
253
|
+
const d = this.#derived?.get(k);
|
|
254
|
+
if (d && !d.dirty) {
|
|
255
|
+
d.dirty = true;
|
|
256
|
+
this.#emit({ op: "dirty", k, has: d.has });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
g(k) {
|
|
262
|
+
const d = this.#derived?.get(k);
|
|
263
|
+
if (d) {
|
|
264
|
+
if (d.dirty || !d.has)
|
|
265
|
+
return this.#computeDerived(k, d);
|
|
266
|
+
return d.value;
|
|
267
|
+
}
|
|
268
|
+
return super.get(k);
|
|
269
|
+
}
|
|
270
|
+
h(k) {
|
|
271
|
+
const d = this.#derived?.get(k);
|
|
272
|
+
if (d) {
|
|
273
|
+
if (d.dirty || !d.has)
|
|
274
|
+
this.#computeDerived(k, d);
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
return super.has(k);
|
|
278
|
+
}
|
|
279
|
+
$debug(limit = 256) {
|
|
280
|
+
this.#hist ??= [];
|
|
281
|
+
this.#histLimit = limit;
|
|
282
|
+
return this;
|
|
283
|
+
}
|
|
284
|
+
$strict() {
|
|
285
|
+
this.#strict = true;
|
|
286
|
+
return this;
|
|
287
|
+
}
|
|
288
|
+
$history() {
|
|
289
|
+
return this.#hist ? [...this.#hist] : [];
|
|
290
|
+
}
|
|
291
|
+
$why(k) {
|
|
292
|
+
if (!this.#hist)
|
|
293
|
+
return undefined;
|
|
294
|
+
for (let i = this.#hist.length - 1; i >= 0; i--) {
|
|
295
|
+
if (Object.is(this.#hist[i].k, k))
|
|
296
|
+
return this.#hist[i];
|
|
297
|
+
}
|
|
298
|
+
return undefined;
|
|
299
|
+
}
|
|
300
|
+
$diff(other) {
|
|
301
|
+
const add = [];
|
|
302
|
+
const del = [];
|
|
303
|
+
const chg = [];
|
|
304
|
+
for (const [k, v] of this) {
|
|
305
|
+
if (!other.has(k)) {
|
|
306
|
+
add.push([k, v]);
|
|
307
|
+
}
|
|
308
|
+
else if (!Object.is(other.get(k), v)) {
|
|
309
|
+
chg.push([k, v, other.get(k)]);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
for (const k of other.keys()) {
|
|
313
|
+
if (!this.has(k))
|
|
314
|
+
del.push(k);
|
|
315
|
+
}
|
|
316
|
+
return { add, del, chg };
|
|
317
|
+
}
|
|
318
|
+
*entries() {
|
|
319
|
+
this.#iterating++;
|
|
320
|
+
try {
|
|
321
|
+
for (const entry of super.entries())
|
|
322
|
+
yield entry;
|
|
323
|
+
}
|
|
324
|
+
finally {
|
|
325
|
+
this.#iterating--;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
*keys() {
|
|
329
|
+
this.#iterating++;
|
|
330
|
+
try {
|
|
331
|
+
for (const k of super.keys())
|
|
332
|
+
yield k;
|
|
333
|
+
}
|
|
334
|
+
finally {
|
|
335
|
+
this.#iterating--;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
*values() {
|
|
339
|
+
this.#iterating++;
|
|
340
|
+
try {
|
|
341
|
+
for (const v of super.values())
|
|
342
|
+
yield v;
|
|
343
|
+
}
|
|
344
|
+
finally {
|
|
345
|
+
this.#iterating--;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
swap(value, key) {
|
|
349
|
+
const next = rv(value);
|
|
350
|
+
const previous = this.has(key) ? this.get(key) : next;
|
|
351
|
+
this.s(next, key);
|
|
352
|
+
return previous;
|
|
353
|
+
}
|
|
354
|
+
take(k) {
|
|
355
|
+
const value = this.g(k);
|
|
356
|
+
this.d(k);
|
|
357
|
+
return value;
|
|
358
|
+
}
|
|
359
|
+
peek(k) {
|
|
360
|
+
return this.h(k) ? this.g(k) : false;
|
|
361
|
+
}
|
|
362
|
+
getOrI(value, k) {
|
|
363
|
+
if (this.h(k))
|
|
364
|
+
return this.g(k);
|
|
365
|
+
const next = rv(value);
|
|
366
|
+
this.s(next, k);
|
|
367
|
+
return next;
|
|
368
|
+
}
|
|
369
|
+
getOrM(make, k) {
|
|
370
|
+
if (this.h(k))
|
|
371
|
+
return this.g(k);
|
|
372
|
+
const value = make();
|
|
373
|
+
this.s(value, k);
|
|
374
|
+
return value;
|
|
375
|
+
}
|
|
376
|
+
update(k, fn, initial) {
|
|
377
|
+
const has = this.h(k);
|
|
378
|
+
const old = has
|
|
379
|
+
? this.g(k)
|
|
380
|
+
: initial === undefined
|
|
381
|
+
? undefined
|
|
382
|
+
: rv(initial);
|
|
383
|
+
const next = rv(fn(old, has));
|
|
384
|
+
this.s(next, k);
|
|
385
|
+
return next;
|
|
386
|
+
}
|
|
387
|
+
setIfAbsent(value, k) {
|
|
388
|
+
if (this.h(k))
|
|
389
|
+
return false;
|
|
390
|
+
const next = rv(value);
|
|
391
|
+
this.s(next, k);
|
|
392
|
+
return true;
|
|
393
|
+
}
|
|
394
|
+
setIfPresent(value, k) {
|
|
395
|
+
if (!this.h(k))
|
|
396
|
+
return false;
|
|
397
|
+
const next = rv(value);
|
|
398
|
+
this.s(next, k);
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
replace(value, k) {
|
|
402
|
+
const next = rv(value);
|
|
403
|
+
return this.setIfPresent(next, k);
|
|
404
|
+
}
|
|
405
|
+
inc(key, by = 1) {
|
|
406
|
+
return this.update(key, (old) => ((old ?? 0) + by), 0);
|
|
407
|
+
}
|
|
408
|
+
touch(k) {
|
|
409
|
+
return this.setIfAbsent(undefined, k);
|
|
410
|
+
}
|
|
411
|
+
mapValues(fn) {
|
|
412
|
+
const out = new M2();
|
|
413
|
+
for (const [k, v] of this)
|
|
414
|
+
out.s(fn(v, k), k);
|
|
415
|
+
return out;
|
|
416
|
+
}
|
|
417
|
+
mapKeys(fn) {
|
|
418
|
+
const out = new M2();
|
|
419
|
+
for (const [k, v] of this)
|
|
420
|
+
out.s(v, fn(v, k));
|
|
421
|
+
return out;
|
|
422
|
+
}
|
|
423
|
+
filterKV(pred) {
|
|
424
|
+
for (const [k, v] of this) {
|
|
425
|
+
if (!pred(v, k))
|
|
426
|
+
this.d(k);
|
|
427
|
+
}
|
|
428
|
+
return this;
|
|
429
|
+
}
|
|
430
|
+
filterKeys(pred) {
|
|
431
|
+
return this.filterKV(pred);
|
|
432
|
+
}
|
|
433
|
+
partition(pred) {
|
|
434
|
+
const yes = new M2();
|
|
435
|
+
const no = new M2();
|
|
436
|
+
for (const [k, v] of this) {
|
|
437
|
+
(pred(v, k) ? yes : no).s(v, k);
|
|
438
|
+
}
|
|
439
|
+
return [yes, no];
|
|
440
|
+
}
|
|
441
|
+
find(pred) {
|
|
442
|
+
for (const [k, v] of this) {
|
|
443
|
+
if (pred(v, k))
|
|
444
|
+
return [k, v];
|
|
445
|
+
}
|
|
446
|
+
return undefined;
|
|
447
|
+
}
|
|
448
|
+
findKey(pred) {
|
|
449
|
+
for (const [k, v] of this) {
|
|
450
|
+
if (pred(v, k))
|
|
451
|
+
return k;
|
|
452
|
+
}
|
|
453
|
+
return undefined;
|
|
454
|
+
}
|
|
455
|
+
findValue(pred) {
|
|
456
|
+
for (const [k, v] of this) {
|
|
457
|
+
if (pred(v, k))
|
|
458
|
+
return v;
|
|
459
|
+
}
|
|
460
|
+
return undefined;
|
|
461
|
+
}
|
|
462
|
+
some(pred) {
|
|
463
|
+
for (const [k, v] of this) {
|
|
464
|
+
if (pred(v, k))
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
return false;
|
|
468
|
+
}
|
|
469
|
+
every(pred) {
|
|
470
|
+
for (const [k, v] of this) {
|
|
471
|
+
if (!pred(v, k))
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
reduce(fn, initial) {
|
|
477
|
+
let acc = initial;
|
|
478
|
+
for (const [k, v] of this)
|
|
479
|
+
acc = fn(acc, v, k);
|
|
480
|
+
return acc;
|
|
481
|
+
}
|
|
482
|
+
forEachKV(fn) {
|
|
483
|
+
for (const [k, v] of this)
|
|
484
|
+
fn(v, k, this);
|
|
485
|
+
}
|
|
486
|
+
invert() {
|
|
487
|
+
const out = new M2();
|
|
488
|
+
for (const [k, v] of this)
|
|
489
|
+
out.s(k, v);
|
|
490
|
+
return out;
|
|
491
|
+
}
|
|
492
|
+
unique(fn) {
|
|
493
|
+
const out = new M2();
|
|
494
|
+
for (const [k, v] of this) {
|
|
495
|
+
const key = (fn ? fn(v, k) : v);
|
|
496
|
+
if (!out.h(key))
|
|
497
|
+
out.s(v, key);
|
|
498
|
+
}
|
|
499
|
+
return out;
|
|
500
|
+
}
|
|
501
|
+
pick(keys) {
|
|
502
|
+
const out = new M2();
|
|
503
|
+
for (const k of keys) {
|
|
504
|
+
if (this.h(k))
|
|
505
|
+
out.s(this.g(k), k);
|
|
506
|
+
}
|
|
507
|
+
return out;
|
|
508
|
+
}
|
|
509
|
+
omit(keys) {
|
|
510
|
+
const omitted = new Set(keys);
|
|
511
|
+
const out = new M2();
|
|
512
|
+
for (const [k, v] of this) {
|
|
513
|
+
if (!omitted.has(k))
|
|
514
|
+
out.s(v, k);
|
|
515
|
+
}
|
|
516
|
+
return out;
|
|
517
|
+
}
|
|
518
|
+
merge(other) {
|
|
519
|
+
for (const [k, v] of other) {
|
|
520
|
+
const n = rv(v);
|
|
521
|
+
this.s(n, k);
|
|
522
|
+
}
|
|
523
|
+
return this;
|
|
524
|
+
}
|
|
525
|
+
clearAndSet(entries) {
|
|
526
|
+
return this.batch(() => {
|
|
527
|
+
this.clear();
|
|
528
|
+
for (const [k, v] of entries) {
|
|
529
|
+
const n = rv(v);
|
|
530
|
+
this.s(n, k);
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
toObject() {
|
|
535
|
+
const out = {};
|
|
536
|
+
for (const [k, v] of this)
|
|
537
|
+
out[k] = v;
|
|
538
|
+
return out;
|
|
539
|
+
}
|
|
540
|
+
toValues() { return A2.from(this.values()); } // [TOVALUES]
|
|
541
|
+
toKeys() { return A2.from(this.keys()); } // [TOKEYS]
|
|
542
|
+
toArray() { return A2.from(this.entries()); } // [TOARRAY]
|
|
543
|
+
group(fn) {
|
|
544
|
+
const out = new M2();
|
|
545
|
+
for (const [k, v] of this)
|
|
546
|
+
out.getOrM(() => new A2(), fn(v, k)).push(v);
|
|
547
|
+
return out;
|
|
548
|
+
}
|
|
549
|
+
clone() {
|
|
550
|
+
return new M2(this);
|
|
551
|
+
}
|
|
552
|
+
equals(other) {
|
|
553
|
+
if (other === this)
|
|
554
|
+
return true;
|
|
555
|
+
if (this.size !== other.size)
|
|
556
|
+
return false;
|
|
557
|
+
for (const [k, v] of this) {
|
|
558
|
+
if (!other.has(k) || !sameValue(other.get(k), v))
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
return true;
|
|
562
|
+
}
|
|
563
|
+
static fromObject(obj) {
|
|
564
|
+
return new M2(Object.entries(obj));
|
|
565
|
+
}
|
|
566
|
+
static fromEntries(entries) {
|
|
567
|
+
return new M2(entries);
|
|
568
|
+
}
|
|
569
|
+
static from(map) {
|
|
570
|
+
return new M2(map);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
export class O2 {
|
|
574
|
+
#data = Object.create(null);
|
|
575
|
+
f = this.forEachKV; // [FOR EACH]
|
|
576
|
+
#subs;
|
|
577
|
+
#hist;
|
|
578
|
+
#histLimit = 0;
|
|
579
|
+
#strict = false;
|
|
580
|
+
#iterating = 0;
|
|
581
|
+
#batching = 0;
|
|
582
|
+
#pending;
|
|
583
|
+
#derived;
|
|
584
|
+
#dependents;
|
|
585
|
+
constructor(entries) {
|
|
586
|
+
if (!entries)
|
|
587
|
+
return;
|
|
588
|
+
for (const [k, v] of entries)
|
|
589
|
+
this.#data[k] = v;
|
|
590
|
+
}
|
|
591
|
+
#rawHas(k) { return Object.prototype.hasOwnProperty.call(this.#data, k); }
|
|
592
|
+
#rawGet(k) { return this.#data[k]; }
|
|
593
|
+
#emit(e) {
|
|
594
|
+
if (this.#batching > 0) {
|
|
595
|
+
const pending = this.#pending[e.k];
|
|
596
|
+
this.#pending[e.k] = pending ? { ...e, prev: pending.prev, batch: true } : { ...e, batch: true };
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
this.#record(e);
|
|
600
|
+
this.#subs?.forEach((sub) => {
|
|
601
|
+
try {
|
|
602
|
+
sub(e);
|
|
603
|
+
}
|
|
604
|
+
catch (err) {
|
|
605
|
+
process.emitWarning(err);
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
#record(e) {
|
|
610
|
+
if (!this.#hist)
|
|
611
|
+
return;
|
|
612
|
+
this.#hist.push(e);
|
|
613
|
+
if (this.#hist.length > this.#histLimit) {
|
|
614
|
+
this.#hist.splice(0, this.#hist.length - this.#histLimit);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
#invalidate(k) {
|
|
618
|
+
const deps = this.#dependents?.[k];
|
|
619
|
+
if (!deps)
|
|
620
|
+
return;
|
|
621
|
+
for (const dk of deps) {
|
|
622
|
+
const d = this.#derived?.[dk];
|
|
623
|
+
if (!d || d.dirty)
|
|
624
|
+
continue;
|
|
625
|
+
d.dirty = true;
|
|
626
|
+
this.#emit({ op: "dirty", k: dk, has: d.has });
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
#computeDerived(k, d) {
|
|
630
|
+
const vals = d.deps.map((dep) => this.g(dep));
|
|
631
|
+
const next = d.fn(...vals);
|
|
632
|
+
const had = d.has;
|
|
633
|
+
const prev = d.value;
|
|
634
|
+
d.value = next;
|
|
635
|
+
d.has = true;
|
|
636
|
+
d.dirty = false;
|
|
637
|
+
if (!had || !sameValue(prev, next)) {
|
|
638
|
+
this.#emit({ op: had ? "upd" : "set", k, v: next, prev: prev, has: true });
|
|
639
|
+
}
|
|
640
|
+
return next;
|
|
641
|
+
}
|
|
642
|
+
#assertNotIterating(op) {
|
|
643
|
+
if (this.#strict && this.#iterating > 0) {
|
|
644
|
+
throw new Error(`O2.${op}() during iteration. Use filterKV(), collect keys first, or disable $strict().`);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
// IN ORDER TO MATCH MAPS CALL API FOR INSTANT AND EASY CONVERSIONS
|
|
648
|
+
get(k) {
|
|
649
|
+
const d = this.#derived?.[k];
|
|
650
|
+
if (d) {
|
|
651
|
+
if (d.dirty || !d.has)
|
|
652
|
+
return this.#computeDerived(k, d);
|
|
653
|
+
return d.value;
|
|
654
|
+
}
|
|
655
|
+
return this.#rawGet(k);
|
|
656
|
+
}
|
|
657
|
+
set(k, v) {
|
|
658
|
+
this.#assertNotIterating("s");
|
|
659
|
+
const next = rv(v);
|
|
660
|
+
const has = this.h(k);
|
|
661
|
+
const prev = has ? this.g(k) : undefined;
|
|
662
|
+
this.#data[k] = next;
|
|
663
|
+
this.#invalidate(k);
|
|
664
|
+
this.#emit({ op: "set", k, v: next, prev: prev, has });
|
|
665
|
+
return this;
|
|
666
|
+
} // note: flipped to match Map's (key, value)
|
|
667
|
+
has(k) {
|
|
668
|
+
const d = this.#derived?.[k];
|
|
669
|
+
if (d) {
|
|
670
|
+
if (d.dirty || !d.has)
|
|
671
|
+
this.#computeDerived(k, d);
|
|
672
|
+
return true;
|
|
673
|
+
}
|
|
674
|
+
return this.#rawHas(k);
|
|
675
|
+
}
|
|
676
|
+
delete(k) {
|
|
677
|
+
this.#assertNotIterating("delete");
|
|
678
|
+
if (!this.h(k))
|
|
679
|
+
return false;
|
|
680
|
+
const prev = this.g(k);
|
|
681
|
+
delete this.#data[k];
|
|
682
|
+
this.#invalidate(k);
|
|
683
|
+
this.#emit({ op: "del", k, prev, has: false });
|
|
684
|
+
return true;
|
|
685
|
+
}
|
|
686
|
+
clear() {
|
|
687
|
+
this.#assertNotIterating("clear");
|
|
688
|
+
const entries = this.toArray();
|
|
689
|
+
this.#data = Object.create(null); // one object swap, no per-key delete
|
|
690
|
+
for (const [k, prev] of entries) {
|
|
691
|
+
this.#invalidate(k);
|
|
692
|
+
this.#emit({ op: "del", k, prev, has: false });
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
forEach(fn) { return this.forEachKV(fn); }
|
|
696
|
+
get length() { return this.size; }
|
|
697
|
+
static groupBy(items, fn) {
|
|
698
|
+
const out = new O2();
|
|
699
|
+
let i = 0;
|
|
700
|
+
for (const item of items)
|
|
701
|
+
out.getOrM(fn(item, i++), () => new A2()).push(item);
|
|
702
|
+
return out;
|
|
703
|
+
}
|
|
704
|
+
get size() {
|
|
705
|
+
return Reflect.ownKeys(this.#data).length;
|
|
706
|
+
}
|
|
707
|
+
[Symbol.iterator]() { return this.entries(); }
|
|
708
|
+
on(fn) {
|
|
709
|
+
(this.#subs ??= new Set()).add(fn);
|
|
710
|
+
return () => this.off(fn);
|
|
711
|
+
}
|
|
712
|
+
off(fn) {
|
|
713
|
+
this.#subs?.delete(fn);
|
|
714
|
+
}
|
|
715
|
+
batch(fn) {
|
|
716
|
+
this.#batching++;
|
|
717
|
+
this.#pending ??= Object.create(null);
|
|
718
|
+
try {
|
|
719
|
+
fn();
|
|
720
|
+
}
|
|
721
|
+
finally {
|
|
722
|
+
this.#batching--;
|
|
723
|
+
if (this.#batching === 0) {
|
|
724
|
+
const pending = this.#pending;
|
|
725
|
+
this.#pending = undefined;
|
|
726
|
+
for (const k of Reflect.ownKeys(pending)) {
|
|
727
|
+
const e = pending[k];
|
|
728
|
+
this.#record(e);
|
|
729
|
+
this.#subs?.forEach((sub) => {
|
|
730
|
+
try {
|
|
731
|
+
sub(e);
|
|
732
|
+
}
|
|
733
|
+
catch (err) {
|
|
734
|
+
process.emitWarning(err);
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
return this;
|
|
741
|
+
}
|
|
742
|
+
s(k, v) {
|
|
743
|
+
this.#assertNotIterating("s");
|
|
744
|
+
const next = rv(v);
|
|
745
|
+
const has = this.h(k);
|
|
746
|
+
const prev = has ? this.g(k) : undefined;
|
|
747
|
+
this.#data[k] = next;
|
|
748
|
+
this.#invalidate(k);
|
|
749
|
+
this.#emit({ op: "set", k, v: next, prev: prev, has });
|
|
750
|
+
return this;
|
|
751
|
+
}
|
|
752
|
+
mutate(k, fn) {
|
|
753
|
+
this.#assertNotIterating("mutate");
|
|
754
|
+
if (!this.h(k))
|
|
755
|
+
return undefined;
|
|
756
|
+
const old = this.g(k);
|
|
757
|
+
const next = fn(old, true);
|
|
758
|
+
this.s(k, next);
|
|
759
|
+
return next;
|
|
760
|
+
}
|
|
761
|
+
d(k) {
|
|
762
|
+
this.#assertNotIterating("delete");
|
|
763
|
+
if (!this.h(k))
|
|
764
|
+
return false;
|
|
765
|
+
const prev = this.g(k);
|
|
766
|
+
delete this.#data[k];
|
|
767
|
+
this.#invalidate(k);
|
|
768
|
+
this.#emit({ op: "del", k, prev, has: false });
|
|
769
|
+
return true;
|
|
770
|
+
}
|
|
771
|
+
c() {
|
|
772
|
+
this.#assertNotIterating("clear");
|
|
773
|
+
const entries = this.toArray();
|
|
774
|
+
this.#data = Object.create(null); // one object swap, no per-key delete
|
|
775
|
+
for (const [k, prev] of entries) {
|
|
776
|
+
this.#invalidate(k);
|
|
777
|
+
this.#emit({ op: "del", k, prev, has: false });
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
dv(k, deps, fn) {
|
|
781
|
+
(this.#derived ??= Object.create(null))[k] = { deps, fn, dirty: true, has: false };
|
|
782
|
+
for (const dep of deps) {
|
|
783
|
+
const dependents = (this.#dependents ??= Object.create(null));
|
|
784
|
+
(dependents[dep] ??= new Set()).add(k);
|
|
785
|
+
}
|
|
786
|
+
return this;
|
|
787
|
+
}
|
|
788
|
+
dirty(...keys) {
|
|
789
|
+
for (const k of keys) {
|
|
790
|
+
const d = this.#derived?.[k];
|
|
791
|
+
if (d && !d.dirty) {
|
|
792
|
+
d.dirty = true;
|
|
793
|
+
this.#emit({ op: "dirty", k, has: d.has });
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
return this;
|
|
797
|
+
}
|
|
798
|
+
g(k) {
|
|
799
|
+
const d = this.#derived?.[k];
|
|
800
|
+
if (d) {
|
|
801
|
+
if (d.dirty || !d.has)
|
|
802
|
+
return this.#computeDerived(k, d);
|
|
803
|
+
return d.value;
|
|
804
|
+
}
|
|
805
|
+
return this.#rawGet(k);
|
|
806
|
+
}
|
|
807
|
+
h(k) {
|
|
808
|
+
const d = this.#derived?.[k];
|
|
809
|
+
if (d) {
|
|
810
|
+
if (d.dirty || !d.has)
|
|
811
|
+
this.#computeDerived(k, d);
|
|
812
|
+
return true;
|
|
813
|
+
}
|
|
814
|
+
return this.#rawHas(k);
|
|
815
|
+
}
|
|
816
|
+
$debug(limit = 256) {
|
|
817
|
+
this.#hist ??= [];
|
|
818
|
+
this.#histLimit = limit;
|
|
819
|
+
return this;
|
|
820
|
+
}
|
|
821
|
+
$strict() {
|
|
822
|
+
this.#strict = true;
|
|
823
|
+
return this;
|
|
824
|
+
}
|
|
825
|
+
$history() {
|
|
826
|
+
return this.#hist ? [...this.#hist] : [];
|
|
827
|
+
}
|
|
828
|
+
$why(k) {
|
|
829
|
+
if (!this.#hist)
|
|
830
|
+
return undefined;
|
|
831
|
+
for (let i = this.#hist.length - 1; i >= 0; i--) {
|
|
832
|
+
if (Object.is(this.#hist[i].k, k))
|
|
833
|
+
return this.#hist[i];
|
|
834
|
+
}
|
|
835
|
+
return undefined;
|
|
836
|
+
}
|
|
837
|
+
$diff(other) {
|
|
838
|
+
const add = [];
|
|
839
|
+
const del = [];
|
|
840
|
+
const chg = [];
|
|
841
|
+
for (const [k, v] of this) {
|
|
842
|
+
if (!other.h(k)) {
|
|
843
|
+
add.push([k, v]);
|
|
844
|
+
}
|
|
845
|
+
else if (!Object.is(other.g(k), v)) {
|
|
846
|
+
chg.push([k, v, other.g(k)]);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
for (const k of other.keys()) {
|
|
850
|
+
if (!this.h(k))
|
|
851
|
+
del.push(k);
|
|
852
|
+
}
|
|
853
|
+
return { add, del, chg };
|
|
854
|
+
}
|
|
855
|
+
*entries() {
|
|
856
|
+
this.#iterating++;
|
|
857
|
+
try {
|
|
858
|
+
for (const k of Reflect.ownKeys(this.#data))
|
|
859
|
+
yield [k, this.#data[k]];
|
|
860
|
+
}
|
|
861
|
+
finally {
|
|
862
|
+
this.#iterating--;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
*keys() {
|
|
866
|
+
this.#iterating++;
|
|
867
|
+
try {
|
|
868
|
+
for (const k of Reflect.ownKeys(this.#data))
|
|
869
|
+
yield k;
|
|
870
|
+
}
|
|
871
|
+
finally {
|
|
872
|
+
this.#iterating--;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
*values() {
|
|
876
|
+
this.#iterating++;
|
|
877
|
+
try {
|
|
878
|
+
for (const k of Reflect.ownKeys(this.#data))
|
|
879
|
+
yield this.#data[k];
|
|
880
|
+
}
|
|
881
|
+
finally {
|
|
882
|
+
this.#iterating--;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
swap(value, key) {
|
|
886
|
+
const next = rv(value);
|
|
887
|
+
const previous = this.h(key) ? this.g(key) : next; // h()/g() kept derived-aware
|
|
888
|
+
this.s(key, next);
|
|
889
|
+
return previous;
|
|
890
|
+
}
|
|
891
|
+
take(k) {
|
|
892
|
+
const value = this.g(k);
|
|
893
|
+
this.d(k);
|
|
894
|
+
return value;
|
|
895
|
+
}
|
|
896
|
+
peek(k) {
|
|
897
|
+
return this.h(k) ? this.g(k) : false;
|
|
898
|
+
}
|
|
899
|
+
getOrI(k, value) {
|
|
900
|
+
if (this.h(k))
|
|
901
|
+
return this.g(k);
|
|
902
|
+
const next = rv(value);
|
|
903
|
+
this.s(k, next);
|
|
904
|
+
return next;
|
|
905
|
+
}
|
|
906
|
+
getOrM(k, make) {
|
|
907
|
+
if (this.h(k))
|
|
908
|
+
return this.g(k);
|
|
909
|
+
const value = make();
|
|
910
|
+
this.s(k, value);
|
|
911
|
+
return value;
|
|
912
|
+
}
|
|
913
|
+
update(k, fn, initial) {
|
|
914
|
+
const has = this.h(k);
|
|
915
|
+
const old = has
|
|
916
|
+
? this.g(k)
|
|
917
|
+
: initial === undefined
|
|
918
|
+
? undefined
|
|
919
|
+
: rv(initial);
|
|
920
|
+
const next = rv(fn(old, has));
|
|
921
|
+
this.s(k, next);
|
|
922
|
+
return next;
|
|
923
|
+
}
|
|
924
|
+
setIfAbsent(k, value) {
|
|
925
|
+
if (this.h(k))
|
|
926
|
+
return false;
|
|
927
|
+
const next = rv(value);
|
|
928
|
+
this.s(k, next);
|
|
929
|
+
return true;
|
|
930
|
+
}
|
|
931
|
+
setIfPresent(k, value) {
|
|
932
|
+
if (!this.h(k))
|
|
933
|
+
return false;
|
|
934
|
+
const next = rv(value);
|
|
935
|
+
this.s(k, next);
|
|
936
|
+
return true;
|
|
937
|
+
}
|
|
938
|
+
replace(k, value) {
|
|
939
|
+
const next = rv(value);
|
|
940
|
+
return this.setIfPresent(k, next);
|
|
941
|
+
}
|
|
942
|
+
inc(key, by = 1) {
|
|
943
|
+
return this.update(key, (old) => ((old ?? 0) + by), 0);
|
|
944
|
+
}
|
|
945
|
+
touch(k) {
|
|
946
|
+
return this.setIfAbsent(k, undefined);
|
|
947
|
+
}
|
|
948
|
+
mapValues(fn) {
|
|
949
|
+
const out = new O2();
|
|
950
|
+
for (const [k, v] of this)
|
|
951
|
+
out.s(k, fn(k, v));
|
|
952
|
+
return out;
|
|
953
|
+
}
|
|
954
|
+
mapKeys(fn) {
|
|
955
|
+
const out = new O2();
|
|
956
|
+
for (const [k, v] of this)
|
|
957
|
+
out.s(fn(k, v), v);
|
|
958
|
+
return out;
|
|
959
|
+
}
|
|
960
|
+
filterKV(pred) {
|
|
961
|
+
for (const [k, v] of this) {
|
|
962
|
+
if (!pred(k, v))
|
|
963
|
+
this.d(k);
|
|
964
|
+
}
|
|
965
|
+
return this;
|
|
966
|
+
}
|
|
967
|
+
filterKeys(pred) {
|
|
968
|
+
return this.filterKV(pred);
|
|
969
|
+
}
|
|
970
|
+
partition(pred) {
|
|
971
|
+
const yes = new O2();
|
|
972
|
+
const no = new O2();
|
|
973
|
+
for (const [k, v] of this) {
|
|
974
|
+
(pred(k, v) ? yes : no).s(k, v);
|
|
975
|
+
}
|
|
976
|
+
return [yes, no];
|
|
977
|
+
}
|
|
978
|
+
find(pred) {
|
|
979
|
+
for (const [k, v] of this) {
|
|
980
|
+
if (pred(k, v))
|
|
981
|
+
return [k, v];
|
|
982
|
+
}
|
|
983
|
+
return undefined;
|
|
984
|
+
}
|
|
985
|
+
findKey(pred) {
|
|
986
|
+
for (const [k, v] of this) {
|
|
987
|
+
if (pred(k, v))
|
|
988
|
+
return k;
|
|
989
|
+
}
|
|
990
|
+
return undefined;
|
|
991
|
+
}
|
|
992
|
+
findValue(pred) {
|
|
993
|
+
for (const [k, v] of this) {
|
|
994
|
+
if (pred(k, v))
|
|
995
|
+
return v;
|
|
996
|
+
}
|
|
997
|
+
return undefined;
|
|
998
|
+
}
|
|
999
|
+
some(pred) {
|
|
1000
|
+
for (const [k, v] of this) {
|
|
1001
|
+
if (pred(k, v))
|
|
1002
|
+
return true;
|
|
1003
|
+
}
|
|
1004
|
+
return false;
|
|
1005
|
+
}
|
|
1006
|
+
every(pred) {
|
|
1007
|
+
for (const [k, v] of this) {
|
|
1008
|
+
if (!pred(k, v))
|
|
1009
|
+
return false;
|
|
1010
|
+
}
|
|
1011
|
+
return true;
|
|
1012
|
+
}
|
|
1013
|
+
reduce(fn, initial) {
|
|
1014
|
+
let acc = initial;
|
|
1015
|
+
for (const [k, v] of this)
|
|
1016
|
+
acc = fn(acc, k, v);
|
|
1017
|
+
return acc;
|
|
1018
|
+
}
|
|
1019
|
+
forEachKV(fn) {
|
|
1020
|
+
for (const [k, v] of this)
|
|
1021
|
+
fn(k, v, this);
|
|
1022
|
+
}
|
|
1023
|
+
invert() {
|
|
1024
|
+
const out = new O2();
|
|
1025
|
+
for (const [k, v] of this)
|
|
1026
|
+
out.s(k, v);
|
|
1027
|
+
return out;
|
|
1028
|
+
}
|
|
1029
|
+
unique(fn) {
|
|
1030
|
+
const out = new O2();
|
|
1031
|
+
for (const [k, v] of this) {
|
|
1032
|
+
const key = (fn ? fn(k, v) : v);
|
|
1033
|
+
if (!out.h(key))
|
|
1034
|
+
out.s(key, v);
|
|
1035
|
+
}
|
|
1036
|
+
return out;
|
|
1037
|
+
}
|
|
1038
|
+
pick(keys) {
|
|
1039
|
+
const out = new O2();
|
|
1040
|
+
for (const k of keys) {
|
|
1041
|
+
if (this.h(k))
|
|
1042
|
+
out.s(k, this.g(k));
|
|
1043
|
+
}
|
|
1044
|
+
return out;
|
|
1045
|
+
}
|
|
1046
|
+
omit(keys) {
|
|
1047
|
+
const omitted = new Set(keys);
|
|
1048
|
+
const out = new O2();
|
|
1049
|
+
for (const [k, v] of this) {
|
|
1050
|
+
if (!omitted.has(k))
|
|
1051
|
+
out.s(k, v);
|
|
1052
|
+
}
|
|
1053
|
+
return out;
|
|
1054
|
+
}
|
|
1055
|
+
merge(other) {
|
|
1056
|
+
for (const [k, v] of other) {
|
|
1057
|
+
const n = rv(v);
|
|
1058
|
+
this.s(k, n);
|
|
1059
|
+
}
|
|
1060
|
+
return this;
|
|
1061
|
+
}
|
|
1062
|
+
clearAndSet(entries) {
|
|
1063
|
+
return this.batch(() => {
|
|
1064
|
+
this.c(); // (M2's version calls the native Map.clear() here
|
|
1065
|
+
for (const [k, v] of entries) { // — silent, no del events; c() is almost
|
|
1066
|
+
const n = rv(v); // certainly what was intended, so I used it)
|
|
1067
|
+
this.s(k, n);
|
|
1068
|
+
}
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
toObject() {
|
|
1072
|
+
const out = {};
|
|
1073
|
+
for (const [k, v] of this)
|
|
1074
|
+
out[k] = v;
|
|
1075
|
+
return out;
|
|
1076
|
+
}
|
|
1077
|
+
toObjectFast() {
|
|
1078
|
+
return { ...this.#data };
|
|
1079
|
+
}
|
|
1080
|
+
snapshot() { return this.toObjectFast(); } // handy name for hook/selector use
|
|
1081
|
+
toJSON() { return this.toObjectFast(); } // JSON.stringify(store) just works
|
|
1082
|
+
select(fn) {
|
|
1083
|
+
return fn(this.#data);
|
|
1084
|
+
}
|
|
1085
|
+
patch(partial) {
|
|
1086
|
+
return this.batch(() => {
|
|
1087
|
+
for (const k of Reflect.ownKeys(partial))
|
|
1088
|
+
this.s(k, partial[k]);
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
toValues() { return A2.from(this.values()); } // [TOVALUES]
|
|
1092
|
+
toKeys() { return A2.from(this.keys()); } // [TOKEYS]
|
|
1093
|
+
toArray() { return A2.from(this.entries()); } // [TOARRAY]
|
|
1094
|
+
group(fn) {
|
|
1095
|
+
const out = new O2();
|
|
1096
|
+
for (const [k, v] of this)
|
|
1097
|
+
out.getOrM(fn(v, k), () => new A2()).push(v);
|
|
1098
|
+
return out;
|
|
1099
|
+
}
|
|
1100
|
+
clone() {
|
|
1101
|
+
return new O2(this);
|
|
1102
|
+
}
|
|
1103
|
+
equals(other) {
|
|
1104
|
+
if (other === this)
|
|
1105
|
+
return true;
|
|
1106
|
+
if (this.size !== other.size)
|
|
1107
|
+
return false;
|
|
1108
|
+
for (const [k, v] of this) {
|
|
1109
|
+
if (!other.h(k) || !sameValue(other.g(k), v))
|
|
1110
|
+
return false;
|
|
1111
|
+
}
|
|
1112
|
+
return true;
|
|
1113
|
+
}
|
|
1114
|
+
static fromObject(obj) {
|
|
1115
|
+
const out = new O2();
|
|
1116
|
+
out.#data = { ...obj }; // direct copy, no per-key emits
|
|
1117
|
+
return out;
|
|
1118
|
+
}
|
|
1119
|
+
static fromEntries(entries) {
|
|
1120
|
+
return new O2(entries);
|
|
1121
|
+
}
|
|
1122
|
+
static from(source) {
|
|
1123
|
+
const out = new O2();
|
|
1124
|
+
out.#data = source instanceof O2 ? { ...source.#data } : { ...source };
|
|
1125
|
+
return out;
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
/**
|
|
1129
|
+
* if (FIGHT === "db") {
|
|
1130
|
+
const db = new SERVER_KVDB(
|
|
1131
|
+
"__SAURON__",
|
|
1132
|
+
"SAURON_SLAVE_REG",
|
|
1133
|
+
"SAURON_MASTER_REG",
|
|
1134
|
+
"THE_WIZARD_ROOT_REG",
|
|
1135
|
+
{
|
|
1136
|
+
directory: "./db/server/bench-data",
|
|
1137
|
+
durability: "group",
|
|
1138
|
+
walBatchBytes: 1024 * 1024,
|
|
1139
|
+
walBatchRecords: 8192,
|
|
1140
|
+
walFlushIntervalMs: 1,
|
|
1141
|
+
walHighWaterBytes: 64 * 1024 * 1024,
|
|
1142
|
+
checkpointIntervalMs: 10_000,
|
|
1143
|
+
},
|
|
1144
|
+
);
|
|
1145
|
+
await db.set(`keyy`, '1');
|
|
1146
|
+
|
|
1147
|
+
bench("RAVEN.store.get", () => {
|
|
1148
|
+
db.hot(`keyy`);
|
|
1149
|
+
});
|
|
1150
|
+
}
|
|
1151
|
+
*/
|
|
1152
|
+
if (FIGHT === 'bare') {
|
|
1153
|
+
//const hot_cache: Record<string, string> = Object.create(null);
|
|
1154
|
+
const JSObj = Object.create(null);
|
|
1155
|
+
JSObj['keyy'] = "1";
|
|
1156
|
+
bench("JSObj['keyy'];", () => { JSObj['keyy']; });
|
|
1157
|
+
const O2Test = new O2();
|
|
1158
|
+
O2Test['keyy'] = '1';
|
|
1159
|
+
bench("O2Test['keyy']", () => { O2Test['keyy']; });
|
|
1160
|
+
O2Test.s("1", "keyy");
|
|
1161
|
+
bench("O2.get(k) [real #data]", () => { O2Test.get('keyy'); });
|
|
1162
|
+
const d = new O2();
|
|
1163
|
+
d.s("2", "a");
|
|
1164
|
+
d.dv("b", ["a"], (a) => a + "!");
|
|
1165
|
+
d.get("b");
|
|
1166
|
+
bench("O2.get(derived clean)", () => { d.get('b'); });
|
|
1167
|
+
const dd = new O2();
|
|
1168
|
+
dd.s("2", "a");
|
|
1169
|
+
dd.dv("b", ["a"], (a) => a + "!");
|
|
1170
|
+
bench("O2.get(derived dirty)", () => { dd.dirty('b'); dd.get('b'); });
|
|
1171
|
+
bench("O2.get(miss)", () => { O2Test.get('nope'); });
|
|
1172
|
+
bench("O2.h(k) hit", () => { O2Test.h('keyy'); });
|
|
1173
|
+
bench("O2.h(k) miss", () => { O2Test.h('nope'); });
|
|
1174
|
+
bench("O2.s(v,k) overwrite", () => { O2Test.s("1", "keyy"); });
|
|
1175
|
+
bench("O2.s(v,k) new+del churn", () => {
|
|
1176
|
+
const k = Math.random().toString(36).slice(2, 10);
|
|
1177
|
+
O2Test.s("1", k);
|
|
1178
|
+
O2Test.d(k);
|
|
1179
|
+
});
|
|
1180
|
+
bench("O2.size", () => { O2Test.size; });
|
|
1181
|
+
}
|
|
1182
|
+
if (FIGHT === "RAVEN") {
|
|
1183
|
+
/* ------------------------------------------------------------------ *
|
|
1184
|
+
* STORE
|
|
1185
|
+
* ------------------------------------------------------------------ */
|
|
1186
|
+
const storeKey = "bench-store";
|
|
1187
|
+
let storeValue = 0;
|
|
1188
|
+
root.state.store.set(storeKey, 1, { triggerRender: false });
|
|
1189
|
+
bench("RAVEN.store.get", () => {
|
|
1190
|
+
root.state.store.get(storeKey);
|
|
1191
|
+
});
|
|
1192
|
+
bench("RAVEN.store.has", () => {
|
|
1193
|
+
root.state.store.has(storeKey);
|
|
1194
|
+
});
|
|
1195
|
+
bench("RAVEN.store.set", () => {
|
|
1196
|
+
root.state.store.set(storeKey, ++storeValue, { triggerRender: false });
|
|
1197
|
+
});
|
|
1198
|
+
bench("RAVEN.store.set.functional", () => {
|
|
1199
|
+
root.state.store.set(storeKey, (prev) => (prev ?? 0) + 1, {
|
|
1200
|
+
triggerRender: false,
|
|
1201
|
+
});
|
|
1202
|
+
});
|
|
1203
|
+
bench("RAVEN.store.get.set", () => {
|
|
1204
|
+
root.state.store.get(storeKey);
|
|
1205
|
+
root.state.store.set(storeKey, ++storeValue, { triggerRender: false });
|
|
1206
|
+
});
|
|
1207
|
+
bench("RAVEN.store.entries", () => {
|
|
1208
|
+
root.state.store.entries();
|
|
1209
|
+
});
|
|
1210
|
+
bench("RAVEN.store.subscribe", () => {
|
|
1211
|
+
const dispose = root.state.store.subscribe(storeKey, () => { });
|
|
1212
|
+
dispose();
|
|
1213
|
+
});
|
|
1214
|
+
/* ------------------------------------------------------------------ *
|
|
1215
|
+
* STORE PERSISTENCE
|
|
1216
|
+
* ------------------------------------------------------------------ */
|
|
1217
|
+
const persistKey = "bench-persist";
|
|
1218
|
+
root.state.store.set(persistKey, 1, { triggerRender: false });
|
|
1219
|
+
root.state.store.persist(persistKey);
|
|
1220
|
+
bench("RAVEN.store.persist.get", () => {
|
|
1221
|
+
root.state.store.get(persistKey);
|
|
1222
|
+
});
|
|
1223
|
+
bench("RAVEN.store.persist.has", () => {
|
|
1224
|
+
root.state.store.has(persistKey);
|
|
1225
|
+
});
|
|
1226
|
+
bench("RAVEN.store.persist.set", () => {
|
|
1227
|
+
root.state.store.set(persistKey, ++storeValue, { triggerRender: false });
|
|
1228
|
+
});
|
|
1229
|
+
root.state.store.unpersist(persistKey);
|
|
1230
|
+
/* ------------------------------------------------------------------ *
|
|
1231
|
+
* STATE
|
|
1232
|
+
* ------------------------------------------------------------------ */
|
|
1233
|
+
const stateKey = "bench-state";
|
|
1234
|
+
let stateValue = 0;
|
|
1235
|
+
root.state.state.set(stateKey, 1, { triggerRender: false });
|
|
1236
|
+
bench("RAVEN.state.get", () => {
|
|
1237
|
+
root.state.state.get(stateKey);
|
|
1238
|
+
});
|
|
1239
|
+
bench("RAVEN.state.set", () => {
|
|
1240
|
+
root.state.state.set(stateKey, ++stateValue, { triggerRender: false });
|
|
1241
|
+
});
|
|
1242
|
+
bench("RAVEN.state.get.set", () => {
|
|
1243
|
+
root.state.state.get(stateKey);
|
|
1244
|
+
root.state.state.set(stateKey, ++stateValue, { triggerRender: false });
|
|
1245
|
+
});
|
|
1246
|
+
bench("RAVEN.state.getAll", () => {
|
|
1247
|
+
root.state.state.getAll();
|
|
1248
|
+
});
|
|
1249
|
+
bench("RAVEN.state.getByPrefix", () => {
|
|
1250
|
+
root.state.state.getByPrefix("bench-");
|
|
1251
|
+
});
|
|
1252
|
+
bench("RAVEN.state.subscribe", () => {
|
|
1253
|
+
const dispose = root.state.state.subscribe(stateKey, () => { });
|
|
1254
|
+
dispose();
|
|
1255
|
+
});
|
|
1256
|
+
/* ------------------------------------------------------------------ *
|
|
1257
|
+
* STATE NOTIFICATION
|
|
1258
|
+
* ------------------------------------------------------------------ */
|
|
1259
|
+
const notifyKey = "bench-state-notify";
|
|
1260
|
+
let notifyValue = 0;
|
|
1261
|
+
let notifyCount = 0;
|
|
1262
|
+
root.state.state.set(notifyKey, 0, { triggerRender: false });
|
|
1263
|
+
const disposeStateNotify = root.state.state.subscribe(notifyKey, () => {
|
|
1264
|
+
notifyCount++;
|
|
1265
|
+
});
|
|
1266
|
+
bench("RAVEN.state.notify", () => {
|
|
1267
|
+
root.state.state.set(notifyKey, ++notifyValue);
|
|
1268
|
+
root.state.flushSync();
|
|
1269
|
+
});
|
|
1270
|
+
disposeStateNotify();
|
|
1271
|
+
/* ------------------------------------------------------------------ *
|
|
1272
|
+
* HOOK: darkState
|
|
1273
|
+
* ------------------------------------------------------------------ */
|
|
1274
|
+
const [darkValue, setDarkValue] = darkState(0, {}, "bench-darkState");
|
|
1275
|
+
bench("RAVEN.darkState.get", () => {
|
|
1276
|
+
darkState(0, {}, "bench-darkState");
|
|
1277
|
+
});
|
|
1278
|
+
bench("RAVEN.darkState.set", () => {
|
|
1279
|
+
setDarkValue(++storeValue);
|
|
1280
|
+
});
|
|
1281
|
+
bench("RAVEN.darkState.get.set", () => {
|
|
1282
|
+
darkState(0, {}, "bench-darkState");
|
|
1283
|
+
setDarkValue(++storeValue);
|
|
1284
|
+
});
|
|
1285
|
+
/* ------------------------------------------------------------------ *
|
|
1286
|
+
* HOOK: darkState SELECTOR
|
|
1287
|
+
* ------------------------------------------------------------------ */
|
|
1288
|
+
const [selectedValue, setSelectedValue] = darkState({ count: 0, other: 0 }, {
|
|
1289
|
+
selector: (v) => v.count,
|
|
1290
|
+
}, "bench-darkState-selector");
|
|
1291
|
+
bench("RAVEN.darkState.selector.get", () => {
|
|
1292
|
+
darkState({ count: 0, other: 0 }, {
|
|
1293
|
+
selector: (v) => v.count,
|
|
1294
|
+
}, "bench-darkState-selector");
|
|
1295
|
+
});
|
|
1296
|
+
bench("RAVEN.darkState.selector.set", () => {
|
|
1297
|
+
setSelectedValue((prev) => ({
|
|
1298
|
+
...prev,
|
|
1299
|
+
count: prev.count + 1,
|
|
1300
|
+
}));
|
|
1301
|
+
});
|
|
1302
|
+
/* ------------------------------------------------------------------ *
|
|
1303
|
+
* HOOK: darkState DERIVED
|
|
1304
|
+
* ------------------------------------------------------------------ */
|
|
1305
|
+
const [derivedValue, setDerivedValue, derived] = darkState(1, {
|
|
1306
|
+
derive: (v) => v * 2,
|
|
1307
|
+
}, "bench-darkState-derived");
|
|
1308
|
+
bench("RAVEN.darkState.derived.get", () => {
|
|
1309
|
+
darkState(1, {
|
|
1310
|
+
derive: (v) => v * 2,
|
|
1311
|
+
}, "bench-darkState-derived");
|
|
1312
|
+
});
|
|
1313
|
+
bench("RAVEN.darkState.derived.set", () => {
|
|
1314
|
+
setDerivedValue(++storeValue);
|
|
1315
|
+
});
|
|
1316
|
+
/* ------------------------------------------------------------------ *
|
|
1317
|
+
* KEEP FIXTURES LIVE
|
|
1318
|
+
* ------------------------------------------------------------------ */
|
|
1319
|
+
void notifyCount;
|
|
1320
|
+
void darkValue;
|
|
1321
|
+
void selectedValue;
|
|
1322
|
+
void derivedValue;
|
|
1323
|
+
void derived;
|
|
1324
|
+
}
|