-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
caching.ts
416 lines (355 loc) 路 11.4 KB
/
caching.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import gensync from "gensync";
import type { Handler } from "gensync";
import {
maybeAsync,
isAsync,
onFirstPause,
waitFor,
isThenable,
} from "../gensync-utils/async";
import { isIterableIterator } from "./util";
export type { CacheConfigurator };
export type SimpleCacheConfigurator = {
(forever: boolean): void;
<T>(handler: () => T): T;
forever: () => void;
never: () => void;
using: <T>(handler: () => T) => T;
invalidate: <T>(handler: () => T) => T;
};
export type CacheEntry<ResultT, SideChannel> = Array<{
value: ResultT;
valid: (channel: SideChannel) => Handler<boolean>;
}>;
const synchronize = <ArgsT extends any[], ResultT>(
gen: (...args: ArgsT) => Handler<ResultT>,
): ((...args: ArgsT) => ResultT) => {
return gensync(gen).sync;
};
// eslint-disable-next-line require-yield
function* genTrue() {
return true;
}
export function makeWeakCache<ArgT extends object, ResultT, SideChannel>(
handler: (
arg: ArgT,
cache: CacheConfigurator<SideChannel>,
) => Handler<ResultT> | ResultT,
): (arg: ArgT, data: SideChannel) => Handler<ResultT> {
return makeCachedFunction<ArgT, ResultT, SideChannel>(WeakMap, handler);
}
export function makeWeakCacheSync<ArgT extends object, ResultT, SideChannel>(
handler: (arg: ArgT, cache?: CacheConfigurator<SideChannel>) => ResultT,
): (arg: ArgT, data?: SideChannel) => ResultT {
return synchronize<[ArgT, SideChannel], ResultT>(
makeWeakCache<ArgT, ResultT, SideChannel>(handler),
);
}
export function makeStrongCache<ArgT, ResultT, SideChannel>(
handler: (
arg: ArgT,
cache: CacheConfigurator<SideChannel>,
) => Handler<ResultT> | ResultT,
): (arg: ArgT, data: SideChannel) => Handler<ResultT> {
return makeCachedFunction<ArgT, ResultT, SideChannel>(Map, handler);
}
export function makeStrongCacheSync<ArgT, ResultT, SideChannel>(
handler: (arg: ArgT, cache?: CacheConfigurator<SideChannel>) => ResultT,
): (arg: ArgT, data?: SideChannel) => ResultT {
return synchronize<[ArgT, SideChannel], ResultT>(
makeStrongCache<ArgT, ResultT, SideChannel>(handler),
);
}
/* NOTE: Part of the logic explained in this comment is explained in the
* getCachedValueOrWait and setupAsyncLocks functions.
*
* > There are only two hard things in Computer Science: cache invalidation and naming things.
* > -- Phil Karlton
*
* I don't know if Phil was also thinking about handling a cache whose invalidation function is
* defined asynchronously is considered, but it is REALLY hard to do correctly.
*
* The implemented logic (only when gensync is run asynchronously) is the following:
* 1. If there is a valid cache associated to the current "arg" parameter,
* a. RETURN the cached value
* 3. If there is a FinishLock associated to the current "arg" parameter representing a valid cache,
* a. Wait for that lock to be released
* b. RETURN the value associated with that lock
* 5. Start executing the function to be cached
* a. If it pauses on a promise, then
* i. Let FinishLock be a new lock
* ii. Store FinishLock as associated to the current "arg" parameter
* iii. Wait for the function to finish executing
* iv. Release FinishLock
* v. Send the function result to anyone waiting on FinishLock
* 6. Store the result in the cache
* 7. RETURN the result
*/
function makeCachedFunction<ArgT, ResultT, SideChannel>(
CallCache: new <Cached>() => CacheMap<ArgT, Cached, SideChannel>,
handler: (
arg: ArgT,
cache: CacheConfigurator<SideChannel>,
) => Handler<ResultT> | ResultT,
): (arg: ArgT, data: SideChannel) => Handler<ResultT> {
const callCacheSync = new CallCache<ResultT>();
const callCacheAsync = new CallCache<ResultT>();
const futureCache = new CallCache<Lock<ResultT>>();
return function* cachedFunction(arg: ArgT, data: SideChannel) {
const asyncContext = yield* isAsync();
const callCache = asyncContext ? callCacheAsync : callCacheSync;
const cached = yield* getCachedValueOrWait<ArgT, ResultT, SideChannel>(
asyncContext,
callCache,
futureCache,
arg,
data,
);
if (cached.valid) return cached.value;
const cache = new CacheConfigurator(data);
const handlerResult: Handler<ResultT> | ResultT = handler(arg, cache);
let finishLock: Lock<ResultT>;
let value: ResultT;
if (isIterableIterator(handlerResult)) {
const gen = handlerResult as Generator<unknown, ResultT, unknown>;
value = yield* onFirstPause(gen, () => {
finishLock = setupAsyncLocks(cache, futureCache, arg);
});
} else {
value = handlerResult;
}
updateFunctionCache(callCache, cache, arg, value);
if (finishLock) {
futureCache.delete(arg);
finishLock.release(value);
}
return value;
};
}
type CacheMap<ArgT, ResultT, SideChannel> =
| Map<ArgT, CacheEntry<ResultT, SideChannel>>
// @ts-expect-error todo(flow->ts): add `extends object` constraint to ArgT
| WeakMap<ArgT, CacheEntry<ResultT, SideChannel>>;
function* getCachedValue<ArgT, ResultT, SideChannel>(
cache: CacheMap<ArgT, ResultT, SideChannel>,
arg: ArgT,
data: SideChannel,
): Handler<{ valid: true; value: ResultT } | { valid: false; value: null }> {
const cachedValue: CacheEntry<ResultT, SideChannel> | void = cache.get(arg);
if (cachedValue) {
for (const { value, valid } of cachedValue) {
if (yield* valid(data)) return { valid: true, value };
}
}
return { valid: false, value: null };
}
function* getCachedValueOrWait<ArgT, ResultT, SideChannel>(
asyncContext: boolean,
callCache: CacheMap<ArgT, ResultT, SideChannel>,
futureCache: CacheMap<ArgT, Lock<ResultT>, SideChannel>,
arg: ArgT,
data: SideChannel,
): Handler<{ valid: true; value: ResultT } | { valid: false; value: null }> {
const cached = yield* getCachedValue(callCache, arg, data);
if (cached.valid) {
return cached;
}
if (asyncContext) {
const cached = yield* getCachedValue(futureCache, arg, data);
if (cached.valid) {
const value = yield* waitFor<ResultT>(cached.value.promise);
return { valid: true, value };
}
}
return { valid: false, value: null };
}
function setupAsyncLocks<ArgT, ResultT, SideChannel>(
config: CacheConfigurator<SideChannel>,
futureCache: CacheMap<ArgT, Lock<ResultT>, SideChannel>,
arg: ArgT,
): Lock<ResultT> {
const finishLock = new Lock<ResultT>();
updateFunctionCache(futureCache, config, arg, finishLock);
return finishLock;
}
function updateFunctionCache<
ArgT,
ResultT,
SideChannel,
Cache extends CacheMap<ArgT, ResultT, SideChannel>,
>(
cache: Cache,
config: CacheConfigurator<SideChannel>,
arg: ArgT,
value: ResultT,
) {
if (!config.configured()) config.forever();
let cachedValue: CacheEntry<ResultT, SideChannel> | void = cache.get(arg);
config.deactivate();
switch (config.mode()) {
case "forever":
cachedValue = [{ value, valid: genTrue }];
cache.set(arg, cachedValue);
break;
case "invalidate":
cachedValue = [{ value, valid: config.validator() }];
cache.set(arg, cachedValue);
break;
case "valid":
if (cachedValue) {
cachedValue.push({ value, valid: config.validator() });
} else {
cachedValue = [{ value, valid: config.validator() }];
cache.set(arg, cachedValue);
}
}
}
class CacheConfigurator<SideChannel = void> {
_active: boolean = true;
_never: boolean = false;
_forever: boolean = false;
_invalidate: boolean = false;
_configured: boolean = false;
_pairs: Array<
[cachedValue: unknown, handler: (data: SideChannel) => Handler<unknown>]
> = [];
_data: SideChannel;
constructor(data: SideChannel) {
this._data = data;
}
simple() {
return makeSimpleConfigurator(this);
}
mode() {
if (this._never) return "never";
if (this._forever) return "forever";
if (this._invalidate) return "invalidate";
return "valid";
}
forever() {
if (!this._active) {
throw new Error("Cannot change caching after evaluation has completed.");
}
if (this._never) {
throw new Error("Caching has already been configured with .never()");
}
this._forever = true;
this._configured = true;
}
never() {
if (!this._active) {
throw new Error("Cannot change caching after evaluation has completed.");
}
if (this._forever) {
throw new Error("Caching has already been configured with .forever()");
}
this._never = true;
this._configured = true;
}
using<T>(handler: (data: SideChannel) => T): T {
if (!this._active) {
throw new Error("Cannot change caching after evaluation has completed.");
}
if (this._never || this._forever) {
throw new Error(
"Caching has already been configured with .never or .forever()",
);
}
this._configured = true;
const key = handler(this._data);
const fn = maybeAsync(
handler,
`You appear to be using an async cache handler, but Babel has been called synchronously`,
);
if (isThenable(key)) {
// @ts-expect-error todo(flow->ts): improve function return type annotation
return key.then((key: unknown) => {
this._pairs.push([key, fn]);
return key;
});
}
this._pairs.push([key, fn]);
return key;
}
invalidate<T>(handler: (data: SideChannel) => T): T {
this._invalidate = true;
return this.using(handler);
}
validator(): (data: SideChannel) => Handler<boolean> {
const pairs = this._pairs;
return function* (data: SideChannel) {
for (const [key, fn] of pairs) {
if (key !== (yield* fn(data))) return false;
}
return true;
};
}
deactivate() {
this._active = false;
}
configured() {
return this._configured;
}
}
function makeSimpleConfigurator(
cache: CacheConfigurator<any>,
): SimpleCacheConfigurator {
function cacheFn(val) {
if (typeof val === "boolean") {
if (val) cache.forever();
else cache.never();
return;
}
return cache.using(() => assertSimpleType(val()));
}
cacheFn.forever = () => cache.forever();
cacheFn.never = () => cache.never();
cacheFn.using = cb => cache.using(() => assertSimpleType(cb()));
cacheFn.invalidate = cb => cache.invalidate(() => assertSimpleType(cb()));
return cacheFn as any;
}
// Types are limited here so that in the future these values can be used
// as part of Babel's caching logic.
export type SimpleType =
| string
| boolean
| number
| null
| void
| Promise<SimpleType>;
export function assertSimpleType(value: unknown): SimpleType {
if (isThenable(value)) {
throw new Error(
`You appear to be using an async cache handler, ` +
`which your current version of Babel does not support. ` +
`We may add support for this in the future, ` +
`but if you're on the most recent version of @babel/core and still ` +
`seeing this error, then you'll need to synchronously handle your caching logic.`,
);
}
if (
value != null &&
typeof value !== "string" &&
typeof value !== "boolean" &&
typeof value !== "number"
) {
throw new Error(
"Cache keys must be either string, boolean, number, null, or undefined.",
);
}
return value;
}
class Lock<T> {
released: boolean = false;
promise: Promise<T>;
_resolve: (value: T) => void;
constructor() {
this.promise = new Promise(resolve => {
this._resolve = resolve;
});
}
release(value: T) {
this.released = true;
this._resolve(value);
}
}