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

Indirect weak map and cache #5065

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions packages/realm/.mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"loader": "tsx",
"enable-source-maps": true,
"no-warnings": true,
"expose-gc": true,
"require": "src/node/index.ts",
"spec": "src/tests/*.test.ts"
}
56 changes: 56 additions & 0 deletions packages/realm/src/IndirectWeakCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

import { IdGetter, IndirectWeakMap } from "./internal";

/**
* A cache of objects (the value) which can either be constructed on demand or retrieved from cache.
* The cache is considered weak as it extends the `IndirectWeakMap` to store its values, making them available
* for garbage collection.
* @internal
*/
export class IndirectWeakCache<
K extends object,
Copy link
Contributor

@takameyer takameyer Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, we should start naming our generics. I always have to read the implementation to deduce what these letters actually mean. Could be as simple as:

Key extends object,
Value extends object,
Args extends unknown[],
Hash = unknown

Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree .. ol' habits stick around.
I pushed a commit with some less abbreviated names 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh. I think K and V are pretty classic abbreviations for Key and Value. And I don't think the Type suffix adds any value, both because they are capitalized, and because types are the only thing that TS supports as generic arguments (even things that look "valuey" like true and "hello" are promoted to "literal types" and are still considered types).

I agree that Args and Hash are good names though. But we don't need to go all Java Enterprise Edition on all of our names.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time to flip a 🪙 regarding Key vs K and Value vs V?
I don't feel strong either way.

As much as I like a good name, please also read / review the other code too 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, i'm not gonna die on this hill, but i've always felt being more explicit is better. As long as we don't start using T, K, V for anything else that Type, Key and Value, i'm fine with it. Just be explicit where it matters.

V extends object,
Args extends unknown[],
Id = unknown,
> extends IndirectWeakMap<K, V, Id> {
constructor(private construct: { new (...args: Args): V }, getId: IdGetter<K, Id>) {
super(getId);
}
/**
* Get an existing value from the cache or construct and store one in case of a miss.
* @param key Object passed to the getId function provided at construction of the cache.
* @param args An optional array of constructor arguments can be passed as well, which will be used in case of a cache miss
* to construct and store a new value object.
* @returns An existing or new value.
* @throws If `args` are not supplied and no object existed in the cache.
*/
getOrCreate(key: K, args?: Args) {
const existing = this.get(key);
if (existing) {
return existing;
} else if (args) {
const result = new this.construct(...args);
this.set(key, result);
return result;
} else {
throw new Error("Needed to create an object, but no args were supplied");
}
}
}
74 changes: 74 additions & 0 deletions packages/realm/src/IndirectWeakMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

/** @internal */
export type IdGetter<K, Id> = (key: K) => Id;

/**
* A map from some type of object (the key) into another type of object (the value), where a
* function (the `getId` function supplied at construction) is called to derive an id of the key,
* which is used when looking up the value. This makes it possible for multiple different key
* objects to get the same value object. This is subtly different from a traditional hash map,
* the id is the only value used as basis for key equality, meaning two different key objects could
* map to the same value, as long as they derive to the same id.
* This property is what is considered "indirect" about the map.
* The map is considered weak in the sense that values are wrapped in a `WeakRef` before being
* inserted in the underling map. A value is also registered with a finalization registry, ensuring
* that their entry in the underlying map is removed when they get garbage collected,
* in an effort to make the entire `IndirectWeakMap` avoid leaks.
* @internal
*/
export class IndirectWeakMap<K extends object, V extends object, Id> implements WeakMap<K, V> {
[Symbol.toStringTag] = "IndirectWeakMap";

private registry = new FinalizationRegistry<Id>((hash) => {
this.values.delete(hash);
});

constructor(private getId: IdGetter<K, Id>, private values: Map<Id, WeakRef<V>> = new Map()) {}

set(key: K, value: V): this {
const id = this.getId(key);
const ref = new WeakRef(value);
// Unregister the finalization registry on value being removed from the map
// to avoid its finalization to prune the new value from the map of values.
const existingRef = this.values.get(id);
if (existingRef) {
this.registry.unregister(existingRef);
}
// Register the new value with the finalization registry,
// to prune its WeakRef from the map of values.
this.registry.register(value, id, ref);
this.values.set(id, ref);
return this;
}

has(key: K): boolean {
return this.get(key) !== undefined;
}

get(key: K): V | undefined {
const id = this.getId(key);
return this.values.get(id)?.deref();
}

delete(key: K): boolean {
const id = this.getId(key);
return this.values.delete(id);
}
}
13 changes: 7 additions & 6 deletions packages/realm/src/app-services/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Credentials,
DefaultFunctionsFactory,
EmailPasswordAuth,
IndirectWeakMap,
Listeners,
Sync,
User,
Expand Down Expand Up @@ -77,6 +78,8 @@ export type AppChangeCallback = () => void;

type AppListenerToken = binding.AppSubscriptionToken;

const appByUser = new IndirectWeakMap<binding.SyncUser, App<any, any>, string>(({ identity }) => identity);

/**
* The class represents an Atlas App Services Application.
*
Expand All @@ -87,8 +90,6 @@ type AppListenerToken = binding.AppSubscriptionToken;
export class App<FunctionsFactoryType = DefaultFunctionsFactory, CustomDataType = Record<string, unknown>> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static appById = new Map<string, binding.WeakRef<App<any, any>>>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static appByUserId = new Map<string, binding.WeakRef<App<any, any>>>();

/**
* Get or create a singleton Realm App from an id.
Expand Down Expand Up @@ -130,8 +131,8 @@ export class App<FunctionsFactoryType = DefaultFunctionsFactory, CustomDataType
public static userAgent = `RealmJS/${App.deviceInfo.sdkVersion} (${App.deviceInfo.platform}, v${App.deviceInfo.platformVersion})`;

/** @internal */
public static getAppByUser(userInternal: binding.SyncUser): App {
const app = this.appByUserId.get(userInternal.identity)?.deref();
public static getByUser(userInternal: binding.SyncUser) {
const app = appByUser.get(userInternal);
if (!app) {
throw new Error(`Cannot determine which app is associated with user (id = ${userInternal.identity})`);
}
Expand Down Expand Up @@ -202,8 +203,8 @@ export class App<FunctionsFactoryType = DefaultFunctionsFactory, CustomDataType

public async logIn(credentials: Credentials) {
const userInternal = await this.internal.logInWithCredentials(credentials.internal);
App.appByUserId.set(userInternal.identity, new binding.WeakRef(this));
return new User(userInternal, this);
appByUser.set(userInternal, this);
return User.get(userInternal);
}

public get emailPasswordAuth(): EmailPasswordAuth {
Expand Down
8 changes: 4 additions & 4 deletions packages/realm/src/app-services/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class User<
UserProfileDataType = DefaultUserProfileData,
> {
/** @internal */
public app: App;
public app: App<FunctionsFactoryType, CustomDataType>;

/** @internal */
public internal: binding.SyncUser;
Expand All @@ -104,13 +104,13 @@ export class User<
FunctionsFactoryType = DefaultFunctionsFactory,
CustomDataType = DefaultObject,
UserProfileDataType = DefaultUserProfileData,
>(internal: binding.SyncUser) {
>(internal: binding.SyncUser): User<FunctionsFactoryType, CustomDataType, UserProfileDataType> {
// TODO: Use a WeakRef to memoize the SDK object
return new User<FunctionsFactoryType, CustomDataType, UserProfileDataType>(internal, App.getAppByUser(internal));
return new User(internal, App.getByUser(internal));
}

/** @internal */
constructor(internal: binding.SyncUser, app: App) {
constructor(internal: binding.SyncUser, app: App<FunctionsFactoryType, CustomDataType>) {
this.internal = internal;
this.app = app;
this.cachedProfile = undefined;
Expand Down
4 changes: 4 additions & 0 deletions packages/realm/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export * from "./Listeners";
export * from "./JSONCacheMap";
/** @internal */
export * from "./TimeoutPromise";
/** @internal */
export * from "./IndirectWeakMap";
/** @internal */
export * from "./IndirectWeakCache";

/** @internal */
export * from "./PropertyHelpers";
Expand Down
62 changes: 62 additions & 0 deletions packages/realm/src/tests/IndirectWeakCache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";

import { gc } from "./utils";

import { IndirectWeakCache } from "../IndirectWeakCache";

describe("IndirectWeakCache", () => {
it("constructs, gets and forgets values", async () => {
class Internal {
constructor(public $addr: bigint) {}
}
class External {
constructor(public foo: string) {}
}

const int1 = new Internal(1n);
const int1b = new Internal(1n);

const cache = new IndirectWeakCache(External, ({ $addr }: Internal) => $addr);
const objs: Record<number, External> = {};
objs[0] = cache.getOrCreate(int1, ["bar"]);
expect(objs[0].foo).equals("bar");
// Getting again with another key sharing the $addr should return the same object
objs[1] = cache.getOrCreate(int1b, ["baz"]);
expect(objs[1]).equals(objs[0]);
// And it shouldn't update the object
expect(objs[1].foo).equals("bar");
// Getting again without providing constructor args should return the same object
objs[2] = cache.getOrCreate(int1);
expect(objs[2]).equals(objs[0]);

// Forgetting the previously returned values, should make the cache forget the original object
delete objs[0];
delete objs[1];
delete objs[2];
await gc();
await gc();

// Now that the object is pruned from cache, we need to supply constructor arguments when getting it
expect(() => {
cache.getOrCreate(int1);
}).throws("Needed to create an object, but no args were supplied");
});
});
84 changes: 84 additions & 0 deletions packages/realm/src/tests/IndirectWeakMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";

import { IndirectWeakMap } from "../IndirectWeakMap";
import { gc } from "./utils";

type TestKey = { hash: number };
type TestValue = { message: string };

describe("IndirectWeakMap", () => {
it("is possible to test WeakRef", async () => {
const ref = new WeakRef({ foo: "bar" });
await gc();
expect(ref.deref()).equals(undefined);
});

it("stores, retrieves and forgets values", async () => {
const internalMap = new Map<number, WeakRef<TestValue>>();
const map = new IndirectWeakMap<TestKey, TestValue, number>((k) => k.hash, internalMap);

const key1: TestKey = { hash: 1 };
const key2: TestKey = { hash: 2 };
const values: TestValue[] = [{ message: "A" }, { message: "B" }, { message: "C" }];
map.set(key1, values[0]);
map.set(key2, values[1]);
expect(map.get(key1)).equals(values[0]);
expect(map.get(key2)).equals(values[1]);
expect(map.has(key1)).equals(true);
expect(internalMap.size).equals(2);

// Forget and garbage collect a value
delete values[0];
await gc();
// We need two gc cycles for the finalization registry to kick in
await gc();

expect(map.get(key1)).equals(undefined);
expect(map.get(key2)).equals(values[1]);
expect(map.has(key1)).equals(false);
expect(map.has(key2)).equals(true);
expect(internalMap.size).equals(1);

// Try getting a value for a different key object with the same hash
const key2b: TestKey = { hash: 2 };
expect(map.get(key2b)).equals(values[1]);

// Try overriding an existing value
map.set(key2, values[2]);
expect(map.get(key2)).equals(values[2]);

// Forgetting the previous value shouldn't prune the internal map
delete values[1];
await gc();
// We need two gc cycles for the finalization registry to kick in
await gc();

expect(map.get(key2)).equals(values[2]);

// Forgetting the last value should prune the internal map entirely
delete values[2];
await gc();
// We need two gc cycles for the finalization registry to kick in
await gc();

expect(internalMap.size).equals(0);
});
});
10 changes: 10 additions & 0 deletions packages/realm/src/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ export function closeRealm(this: Mocha.Context & Partial<RealmContext>) {
delete this.realm;
}
}

export async function gc() {
// Breaking synchronious execution seem to be needed
await new Promise(setImmediate);
if (global.gc) {
global.gc();
} else {
throw new Error("Expected the test to be run with --expose-gc");
}
}