Skip to content

Prevent early deallocation of object references - #13

Merged
kateinoigakukun merged 2 commits into
swiftwasm:masterfrom
MaxDesiatov:object-cache
Jun 20, 2020
Merged

Prevent early deallocation of object references#13
kateinoigakukun merged 2 commits into
swiftwasm:masterfrom
MaxDesiatov:object-cache

Conversation

@MaxDesiatov

@MaxDesiatov MaxDesiatov commented Jun 19, 2020

Copy link
Copy Markdown
Member

Currently, when two references are created for the same object (i.e. with the same id), deallocating the first references invalidates the second. This is can cause crashes, which is reproduced in the newly added test. To avoid this issue, a new private cache of weak references is created, which can retrieve the same JSObjectRef instance for the same id, when called from RawJSValue.jsValue().

Obviously, this adds some overhead on object retrieval when the Swift <-> JS bridge is crossed, but I think invalid references are a much more serious problem than this overhead. I don't know yet what implementation could be more efficient, if that's possible at all.

@kateinoigakukun

Copy link
Copy Markdown
Member

I think it should be a better approach to manage a reference count in JavaScript library #14
What do you think about the approach?

@MaxDesiatov

Copy link
Copy Markdown
Member Author

Yeah, that could work too. I wasn't sure if doing anything on the JavaScript side is slower, but most probably just incrementing and decrementing a counter is faster than maintaining a dictionary with its hashing algorithms 🙂 Also, could be more memory efficient, since you don't waste the space on the cache dictionary

@kateinoigakukun
kateinoigakukun merged commit d670953 into swiftwasm:master Jun 20, 2020
@MaxDesiatov
MaxDesiatov deleted the object-cache branch November 26, 2020 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants