Skip to content

Commit

Permalink
PERF Only finalizationRegistry.unregister proxy if it is registered (
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Sep 15, 2023
1 parent 0fb6888 commit e492392
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/core/pyproxy.ts
Expand Up @@ -140,6 +140,7 @@ type PyProxyShared = {
flags: number;
promise: Promise<any> | undefined;
destroyed_msg: string | undefined;
gcRegistered: boolean;
};
type PyProxyProps = {
/**
Expand Down Expand Up @@ -261,6 +262,7 @@ function pyproxy_new(
flags,
promise: undefined,
destroyed_msg: undefined,
gcRegistered: false,
};
Module._Py_IncRef(ptr);
}
Expand Down Expand Up @@ -293,6 +295,7 @@ Module.pyproxy_new = pyproxy_new;

function gc_register_proxy(shared: PyProxyShared) {
const shared_copy = Object.assign({}, shared);
shared.gcRegistered = true;
Module.finalizationRegistry.register(shared, shared_copy, shared);
}
Module.gc_register_proxy = gc_register_proxy;
Expand Down Expand Up @@ -462,7 +465,9 @@ Module.pyproxy_destroy = function (
// just in case!
const ptr = shared.ptr;
shared.ptr = 0;
Module.finalizationRegistry.unregister(shared);
if (shared.gcRegistered) {
Module.finalizationRegistry.unregister(shared);
}
pyproxy_decref_cache(shared.cache);

try {
Expand Down

0 comments on commit e492392

Please sign in to comment.