Skip to content

Commit 293737a

Browse files
revert: remove runtime changes; keep memory benchmark and docs only
Co-Authored-By: Harry Brundage <[email protected]>
1 parent bf2c6cf commit 293737a

File tree

4 files changed

+8
-65
lines changed

4 files changed

+8
-65
lines changed

Benchmarking.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ node --expose-gc -r ts-node/register/transpile-only bench/memory-large-root.benc
9191
# control number of instances retained
9292
MQT_MEMORY_N=20 pnpm x bench/memory-large-root.benchmark.ts
9393
```
94+
9495
Notes:
9596
- In CI (CI=true) and CodSpeed runs, heap snapshots are disabled automatically to avoid environment limitations. Override locally with MQT_MEMORY_HEAPSNAP=1 if needed.
9697

src/api.ts

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
} from "mobx-state-tree";
2828
import type { FlowReturn } from "mobx-state-tree/dist/internal";
2929
import { CantRunActionError } from "./errors";
30-
import { $context, $parent, $quickType, $readOnly, $type, $identifier } from "./symbols";
30+
import { $context, $parent, $quickType, $readOnly, $type } from "./symbols";
3131
import type {
3232
CreateTypes,
3333
IAnyComplexType,
@@ -236,57 +236,7 @@ export function resolveIdentifier<T extends IAnyModelType>(
236236
if (!context) {
237237
throw new Error("can't resolve references in a readonly tree with no context");
238238
}
239-
const cache = (context as any).referenceCache;
240-
if (cache && typeof cache.get === "function") {
241-
return cache.get(identifier) as Instance<T> | undefined;
242-
}
243-
const root: any = getRoot<IAnyType>(target);
244-
const visited = new Set<any>();
245-
const isTargetType = (node: any): boolean => {
246-
try {
247-
return (isType(type) ? type : (type as any)).is(node);
248-
} catch {
249-
return false;
250-
}
251-
};
252-
const stack: any[] = [root];
253-
while (stack.length) {
254-
const node = stack.pop();
255-
if (!node || typeof node !== "object" || visited.has(node)) continue;
256-
visited.add(node);
257-
if ($readOnly in node) {
258-
const id = node[$identifier];
259-
if (id === identifier && isTargetType(node)) {
260-
return node as Instance<T>;
261-
}
262-
for (const key of Object.keys(node)) {
263-
const val = node[key];
264-
if (val && typeof val === "object") {
265-
stack.push(val);
266-
}
267-
}
268-
if (Array.isArray(node)) {
269-
for (const val of node) stack.push(val);
270-
}
271-
if (node instanceof Map) {
272-
for (const val of node.values()) stack.push(val);
273-
}
274-
} else {
275-
for (const key of Object.keys(node)) {
276-
const val = node[key];
277-
if (val && typeof val === "object") {
278-
stack.push(val);
279-
}
280-
}
281-
if (Array.isArray(node)) {
282-
for (const val of node) stack.push(val);
283-
}
284-
if (node instanceof Map) {
285-
for (const val of node.values()) stack.push(val);
286-
}
287-
}
288-
}
289-
return undefined as Instance<T> | undefined;
239+
return context.referenceCache.get(identifier) as Instance<T> | undefined;
290240
}
291241

292242
export const applySnapshot = <T extends IAnyType>(target: IStateTreeNode<T>, snapshot: SnapshotIn<T>): void => {

src/fast-instantiator.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,7 @@ export class InstantiatorBuilder<T extends IClassModelType<Record<string, IAnyTy
113113
}
114114
}
115115
116-
context.referencesToResolve = null; // cleanup the list of references to resolve, no need to retain them past construction
117-
if (!(process && process.env && process.env.MQT_KEEP_REF_CACHE)) {
118-
if (context.referenceCache && typeof context.referenceCache.clear === "function") {
119-
context.referenceCache.clear();
120-
}
121-
context.referenceCache = null;
122-
}
116+
context.referencesToResolve = null;
123117
124118
return instance;
125119
};

src/reference.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ export class ReferenceType<TargetType extends IAnyComplexType> extends BaseType<
3232
}
3333

3434
instantiate(snapshot: this["InputType"] | undefined, context: TreeContext, _parent: IStateTreeNode | null): this["InstanceType"] {
35-
const cache = context.referenceCache;
36-
if (!snapshot || !cache || !cache.has(snapshot)) {
35+
if (!snapshot || !context.referenceCache.has(snapshot)) {
3736
throw new Error(`can't resolve reference ${snapshot}`);
3837
}
39-
return cache.get(snapshot);
38+
return context.referenceCache.get(snapshot);
4039
}
4140

4241
is(value: any): value is this["InstanceType"];
@@ -62,11 +61,10 @@ export class SafeReferenceType<TargetType extends IAnyComplexType> extends BaseT
6261
}
6362

6463
instantiate(snapshot: string | undefined, context: TreeContext, _parent: IStateTreeNode | null): this["InstanceType"] {
65-
const cache = context.referenceCache;
66-
if (!snapshot || !cache || !cache.has(snapshot)) {
64+
if (!snapshot || !context.referenceCache.has(snapshot)) {
6765
return undefined as this["InstanceType"];
6866
}
69-
return cache.get(snapshot);
67+
return context.referenceCache.get(snapshot);
7068
}
7169

7270
is(value: any): value is this["InstanceType"];

0 commit comments

Comments
 (0)