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

high memory usage on loop #101

Open
TorchedSammy opened this issue Dec 19, 2023 · 5 comments
Open

high memory usage on loop #101

TorchedSammy opened this issue Dec 19, 2023 · 5 comments

Comments

@TorchedSammy
Copy link
Contributor

i'm not sure how to title this or to properly report this.

n = 0; while n < 10000000000 do n = n + 1 end
running this sample of code in hilbish (which in reality just uses golua) and I get this:



for some reason, this doesn't happen in golua-repl

@arnodel
Copy link
Owner

arnodel commented Dec 19, 2023 via email

@TorchedSammy
Copy link
Contributor Author

What is the global table in Hilbish? If its __index metamethod is
redefined (as a function) then the runtime will try to call it

I set it in Lua to define global variables as environment variables. It can be seen here: https://github.com/Rosettea/Hilbish/blob/master/nature/init.lua#L35-L63

EDIT: to check my theory is right, you can declare n as local. This should make it fast again

yes, no change in memory usage at all.

@arnodel
Copy link
Owner

arnodel commented Dec 19, 2023

What is the global table in Hilbish? If its __index metamethod is
redefined (as a function) then the runtime will try to call it

I set it in Lua to define global variables as environment variables. It can be seen here: https://github.com/Rosettea/Hilbish/blob/master/nature/init.lua#L35-L63

Ah, so does it mean every time n is executed, if the global table doesn't have. key called n, the program checks if there is an environment variable called n and returns it if that is the case? Also do you define __newindex as well?

@TorchedSammy
Copy link
Contributor Author

Yes, it reads and sets environment variables as Lua variables. To and back:

@arnodel
Copy link
Owner

arnodel commented Dec 19, 2023

Ok, so your code snippet basically calls os.Getenv("n") and os.Setenv("n", ...) 10 billion times. That may account for a significant portion of the execution time - also I imagine the number is converted to and from a string each time (this is all speculative, it would need to be checked). As for the memory leak, I am not sure there is one - it may just be that 10 billion instances of Termination are allocated and so it keeps the GC busy, but all memory is probably reclaimed. If the terminations were not GCed, there would be a leak of the order of 10 billion * (size of a termination >= 48) bytes, which would be hundreds of GB.

I think a termination pool might mitigate that, as I mentioned previously. It's something I can try implementing - it might be straightforward enough as it could be modelled on continuation pool implementations. I don't know if I would have time to do that soon though.

Another approach would be to convince the Go compiler that the termination created in the runtime.Index function can be allocated on the stack, but that's probably impossible.

@TorchedSammy TorchedSammy changed the title memory leaking high memory usage on loop Dec 20, 2023
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