|
From: <gi...@cr...> - 2026-01-30 04:55:10
|
via 03f7a86f9360fedb60591b8267e28cedf8b6b3b5 (commit)
from 71f451d4b8f61c3cae9800c8544be2010965f3aa (commit)
-----------------------------------------------------------------------
commit 03f7a86f9360fedb60591b8267e28cedf8b6b3b5
Author: gammafunk <gam...@gm...>
Date: Thu Jan 29 22:04:06 2026 -0600
Don't pollute the lua C stack (DracoOmega)
For some reason, I never checked whether the error handler added in
2b9214b6 was being removed by lua_pcall(). I assumed it just removed it
like how it removes the called function and its arguments. It turns out
we were polluting the lua C stack with an unremoved copy of this handler
C function with almost *every* call to lua from C! This grew the stack
so much that various weird issues could happen. In the case of the Disco
Pan vault, where lua is getting called a lot in every turn, the game
would start to lag more than it did in the past on that level. This was
probably the source of the mysterious objstat behaviour as well, but
I'll have to investigate that further.
This commit removes the handler from position 1 of the stack after each
pcall. Debugging has shown that we're now entering functions like
lua_element_colour_calc::get() each time with a C stack size of 0
instead one that can easily grow into the thousands. Movement through
Disco Pan is significantly faster, although still laggy due to the
amount of lua calls happening.
-----------------------------------------------------------------------
Summary of changes:
crawl-ref/source/clua.cc | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/crawl-ref/source/clua.cc b/crawl-ref/source/clua.cc
index efb91acb05..d6198418dc 100644
--- a/crawl-ref/source/clua.cc
+++ b/crawl-ref/source/clua.cc
@@ -564,7 +564,15 @@ int CLua::pcall(int argc, int retc)
msgh = 1;
}
- return lua_pcall(_state, argc, retc, msgh);
+ const int retval = lua_pcall(_state, argc, retc, msgh);
+
+ // lua_pcall doesn't remove the error handler, so we have to do it. TODO:
+ // Maybe just push the handler once and leave it at index 1? It would be
+ // a slight performance gain.
+ if (!managed_vm)
+ lua_remove(_state, 1);
+
+ return retval;
}
bool CLua::calltopfn(const char *params, va_list args, int retc,
--
Dungeon Crawl Stone Soup
|