Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Understanding RegisterGlobal #366

Closed
tznind opened this issue Apr 13, 2020 · 3 comments
Closed

Understanding RegisterGlobal #366

tznind opened this issue Apr 13, 2020 · 3 comments

Comments

@tznind
Copy link
Contributor

tznind commented Apr 13, 2020

Is RegisterGlobal recursion required to call methods from lua scripts or is it a performance thing?

I've been using NLua for ~4 months now and it's amazing! But I have noticed a performance hit that seems to come from registering my globals e.g. lua["myvar"] = x;. I think it is because I have quite complex objects with many collections, so I end up with up to ~1200 globals getting registered each time I prepare to run a script.

Each script I want to run is very small e.g. return Actor.Stats[Fight] < 100 so they tend to only touch a fraction of the domain object. But I do call them very often.

I notice that the RegisterGlobal function stops after 2 levels of recursion but you still seem to be able to call methods beyond that.

I have coded a rather hacky workaround to register my root properties without all the recursive reflection exploration (that occurs in RegisterGlobal):

tznind/Wanderer@cea9b82

I'd like to make it cleaner though and understand if I am doing something very stupid with this.

Any guidance would be much appreciated.

@viniciusjarina
Copy link
Member

Hi @tznind thank you for the feedback is really nice to see people are using the library.

I was wondering why do you need to register so many globals?
Because if you do lua["foo"] = this you shuold be able to call any property from foo doing
foo.MyProp1 ... foo.MyProp2 without registering anything.

We can definitely fix the RegisterGlobal to your "fast" implementation as long that doesn't regress any test, feel free to send a PR.

@tznind
Copy link
Contributor Author

tznind commented Apr 14, 2020

Thanks for taking the time to reply

Hi @tznind thank you for the feedback is really nice to see people are using the library.

I was wondering why do you need to register so many globals?
Because if you do lua["foo"] = this you shuold be able to call any property from foo doing
foo.MyProp1 ... foo.MyProp2 without registering anything.

I only want to create 4/5 globals. But each time I do lua["foo"] = this I end up with a globals count upwards of 1000. I think this is due to the recursion in RegisterGlobal (which lists all the methods on this and properties and subproperties etc).

We can definitely fix the RegisterGlobal to your "fast" implementation as long that doesn't regress any test, feel free to send a PR.

I think what I have done is pretty hacky so probably not ideal (it's on a feature branch of my repo). But I've created a PR which exposes the maximum recursion level of the RegisterGlobal method. When you set this to 0 on a Lua instance then you end up with only 1 global when you do lua["foo"] = this (instead of 1 for every method and property on this to the nth level of recursion).

You can still call all the sub properties and methods (and theres a test using System.DataTable to show that)

Let me know if you want me to revert the System.Data.DataTable test and use a simpler class like List<string> which would reduce the PR footprint a little.

@tznind
Copy link
Contributor Author

tznind commented Apr 26, 2020

Going to close this now since 7ff6e7c (MaximumRecursion setting) should fix this issue (a small number of complex/deep global class instances resulting in thousands of globals entries being registered).

Thanks for all the help tidying the PR @viniciusjarina .

@tznind tznind closed this as completed Apr 26, 2020
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

No branches or pull requests

2 participants