Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cache/unstable): add memoize() and LruCache #4725

Merged
merged 22 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 39 additions & 33 deletions .github/dependency_graph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions _tools/check_circular_package_dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Mod =
| "assert"
| "async"
| "bytes"
| "cache"
| "cli"
| "collections"
| "crypto"
Expand Down Expand Up @@ -80,6 +81,7 @@ const ENTRYPOINTS: Record<Mod, string[]> = {
assert: ["mod.ts"],
async: ["mod.ts"],
bytes: ["mod.ts"],
cache: ["mod.ts"],
cli: ["mod.ts"],
collections: ["mod.ts"],
crypto: ["mod.ts"],
Expand Down
1 change: 1 addition & 0 deletions _tools/check_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const ENTRY_POINTS = [
"../assert/mod.ts",
"../async/mod.ts",
"../bytes/mod.ts",
"../cache/mod.ts",
"../cli/mod.ts",
"../crypto/mod.ts",
"../collections/mod.ts",
Expand Down
87 changes: 87 additions & 0 deletions cache/_serialize_arg_list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import type { MemoizationCache } from "./memoize.ts";

/**
* Default serialization of arguments list for use as cache keys. Equivalence
* follows [`SameValueZero`](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-samevaluezero)
* reference equality, such that `getKey(x, y) === getKey(x, y)` for all values
* of `x` and `y`, but `getKey({}) !== getKey({})`.
*
* @param cache The cache for which the keys will be used.
* @returns `getKey`, the function for getting cache keys.
*/

export function _serializeArgList<Return>(
cache: MemoizationCache<unknown, Return>,
): (this: unknown, ...args: unknown[]) => string {
const weakKeyToKeySegmentCache = new WeakMap<WeakKey, string>();
const weakKeySegmentToKeyCache = new Map<string, string[]>();
let i = 0;

const registry = new FinalizationRegistry<string>((keySegment) => {
for (const key of weakKeySegmentToKeyCache.get(keySegment) ?? []) {
cache.delete(key);
}
weakKeySegmentToKeyCache.delete(keySegment);
});

return function getKey(...args) {
const weakKeySegments: string[] = [];
const keySegments = [this, ...args].map((arg) => {
if (typeof arg === "undefined") return "undefined";
if (typeof arg === "bigint") return `${arg}n`;

if (typeof arg === "number") {
return String(arg);
}

if (
arg === null ||
typeof arg === "string" ||
typeof arg === "boolean"
) {
// This branch will need to be updated if further types are added to
// the language that support value equality,
// e.g. https://github.com/tc39/proposal-record-tuple
return JSON.stringify(arg);
}

try {
assertWeakKey(arg);
} catch {
if (typeof arg === "symbol") {
return `Symbol.for(${JSON.stringify(arg.description)})`;
}
// Non-weak keys other than `Symbol.for(...)` are handled by the branches above.
throw new Error(
"Should be unreachable. Please open an issue at https://github.com/denoland/std/issues/new",

Check warning on line 57 in cache/_serialize_arg_list.ts

View check run for this annotation

Codecov / codecov/patch

cache/_serialize_arg_list.ts#L55-L57

Added lines #L55 - L57 were not covered by tests
);
}

if (!weakKeyToKeySegmentCache.has(arg)) {
const keySegment = `{${i++}}`;
weakKeySegments.push(keySegment);
registry.register(arg, keySegment);
weakKeyToKeySegmentCache.set(arg, keySegment);
}

const keySegment = weakKeyToKeySegmentCache.get(arg)!;
weakKeySegments.push(keySegment);
return keySegment;
});

const key = keySegments.join(",");

for (const keySegment of weakKeySegments) {
const keys = weakKeySegmentToKeyCache.get(keySegment) ?? [];
keys.push(key);
weakKeySegmentToKeyCache.set(keySegment, keys);
}

return key;
};
}

function assertWeakKey(arg: unknown): asserts arg is WeakKey {
new WeakRef(arg as WeakKey);
}
Loading