Skip to content

Commit

Permalink
WeakRef the as()'d objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson committed Dec 6, 2024
1 parent 6671a39 commit 0f2dcb2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-dingos-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osdk/client": patch
---

Internal optimization to reduce memory usage for unused interface objects
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,20 @@ export const get$as = createSimpleCache<
const osdkObjectToInterfaceView = createSimpleCache(
new WeakMap<
OsdkBase<any>,
Map<string, OsdkBase<any>>
Map<string, WeakRef<OsdkBase<any>>>
>(),
() =>
new Map<
/* interface api name */ string,
/* $as'd object */ OsdkBase<any>
/* $as'd object */ WeakRef<OsdkBase<any>>
>(),
);

function $asFactory(
objDef: FetchedObjectTypeDefinition,
): DollarAsFn {
// We use the exact same logic for both the interface rep and the underlying rep

return function $as<
NEW_Q extends ObjectOrInterfaceDefinition,
>(
Expand Down Expand Up @@ -98,13 +99,13 @@ function $asFactory(

const existing = osdkObjectToInterfaceView
.get(underlying)
.get(targetInterfaceApiName);
.get(targetInterfaceApiName)?.deref();
if (existing) return existing;

const osdkInterface = createOsdkInterface(underlying, def.def);
osdkObjectToInterfaceView.get(underlying).set(
targetInterfaceApiName,
osdkInterface,
new WeakRef(osdkInterface),
);
return osdkInterface;
};
Expand Down

0 comments on commit 0f2dcb2

Please sign in to comment.