JavaScriptKit icon indicating copy to clipboard operation
JavaScriptKit copied to clipboard

Improve reference behavior

Open j-f1 opened this issue 5 years ago • 3 comments

I made a small change to report whenever an object ref is deleted:

---
 Runtime/src/index.ts | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Runtime/src/index.ts b/Runtime/src/index.ts
index 0e641ff..ddfba4e 100644
--- a/Runtime/src/index.ts
+++ b/Runtime/src/index.ts
@@ -93,6 +93,7 @@ class SwiftRuntimeHeap {
     release(ref: ref) {
         const value = this._heapValueById.get(ref);
         const isObject = typeof value == "object"
+        console.log('dereferencing', value);
         if (isObject) {
             const entry = this._heapEntryByValue.get(value)!;
             entry.rc--;

(copy the content of the code block and run pbpaste | git apply in your terminal to make this change)

It reports many deallocations. The thing that caught my attention was the repeated deallocation of functions (like toString and hasOwnProperty). I wonder if there’s a way to recognize functions that are part of the JavaScript language and either dynamically call them when needed or mark them so they don’t get released.

j-f1 avatar Sep 16 '20 17:09 j-f1

I also wonder if we could improve performance by having another Swift → JS calling convention without a return value that avoids the shuffling of memory there.

j-f1 avatar Sep 16 '20 17:09 j-f1

mark them so they don’t get released.

I imagine maintaining a cache of JS functions (a dictionary from their names to function references) could work, but this would prevent monkey patching on the JS side from working. Which is not a great practice anyway, so maybe a cache is fine? 🤔

MaxDesiatov avatar Sep 16 '20 21:09 MaxDesiatov

We could just store the key path to the global value then request it each time Swift asks for it to preserve the ability to monkey patch.

j-f1 avatar Sep 16 '20 22:09 j-f1