Skip to content

Pool objects created when passing objects from generated assemblies to the user#92

Merged
Kasuromi merged 21 commits intoBepInEx:masterfrom
simonkellly:pooled-objects
Jul 10, 2023
Merged

Pool objects created when passing objects from generated assemblies to the user#92
Kasuromi merged 21 commits intoBepInEx:masterfrom
simonkellly:pooled-objects

Conversation

@simonkellly
Copy link
Copy Markdown
Contributor

This essentially stops the massive memory usage which occurs as a result of creating a brand new wrapper object every time you call a method/field/property from any of the generated assemblies.

Copy link
Copy Markdown
Member

@Kasuromi Kasuromi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, this has been something on our radar for a while.

Unfortunately since this all relies on the GarbageCollector::RunFinalizer hook, we need to be able to reliably fetch the method on x86/x64. I suggest looking for a way to fetch the method through xref traversal or grabbing one or two more signatures for x64 games.

@Alexejhero
Copy link
Copy Markdown

Could we get this PR merged soon? 😃

@simonkellly
Copy link
Copy Markdown
Contributor Author

AFAIK this is ready to go, lmk if changes are required

@Kasuromi Kasuromi requested a review from js6pak June 14, 2023 09:21
@simonkellly
Copy link
Copy Markdown
Contributor Author

I have made the changes,

Some of the objects remain in the cache as their finalizer doesnt run, but forcing the GC to run tends to remove a chunk of them so I don't think it is much of an issue

@simonkellly simonkellly requested a review from js6pak June 16, 2023 15:06
@BepInEx BepInEx deleted a comment from Wael1231 Jun 16, 2023
@BepInEx BepInEx deleted a comment from Wael1231 Jun 16, 2023
@BepInEx BepInEx deleted a comment from Wael1231 Jun 16, 2023
@Wael1231

This comment was marked as spam.

Copy link
Copy Markdown
Member

@Kasuromi Kasuromi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this current implementation, if the GarbageCollector::RunFinalizer method doesn't get resolved (e.g. signatures return zero) Il2CppInterop will throw an exception for passing a null method and will then pass on this null pointer into the detouring API.
In the case of BepInEx an assertion in Dobby is hit, but the runtime will generally continue.

@js6pak suggested here that Il2CppInterop should print a warning and disable pooling when it's unable to resolve the method. If that's feasible it'll be best to implement to ensure backwards compatibility with other games that for one reason or another we can't fetch this method for.

@Alexejhero
Copy link
Copy Markdown

Is there anything that still needs to be done before merging this pull request? I have a mod release that is waiting on these changes. 😊

@Kasuromi Kasuromi merged commit 876e0f0 into BepInEx:master Jul 10, 2023
ToniMacaroni added a commit to ToniMacaroni/Il2CppInterop that referenced this pull request Dec 16, 2023
@ds5678 ds5678 added this to the 1.4.6 milestone Jul 15, 2024
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.

6 participants