mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-28 22:03:20 +01:00
fix(scripting): hold the Lua screen heap at each allocation on 64-bit Windows too (#490)
* fix(scripting): hold the Lua screen heap at each allocation on 64-bit Windows too LuaJIT 2.0 on x64, the game's runtime on Windows, makes no state with an allocator of the game's, so a Lua screen's memory was checked only by the instruction hook every 1000 instructions. A loop doubling a string (s = s .. s, about 50 instructions) went on to LuaJIT's own heap limit: the unit test binary peaked at 1271 MB. The state is now made with luaL_newstate() and the screen's counting allocator is put in front of LuaJIT's own with lua_setallocf, which LuaJIT 2.0 has. LuaJIT's allocator still makes every block, so its heap stays where 2.0 on x64 needs it; the screen's allocator refuses a block that would take the screen over twice its limit during a script call, and LuaJIT raises "not enough memory" before the string is made. The same path now runs with every LuaJIT, so the lua_newstate probe is gone. LuaJIT's allocator is put back before lua_close, which frees its arena only then. The same loop now peaks at 310 MB with the game's 128 MB limit. The limit is read per screen, so the unit tests use 32 MB: a new test runs the doubling loop, and the one-concatenation test, skipped on Windows until now, runs everywhere. Follows up remark 4 of the review of #356. * fix(lua): the two memory globals behind functions, and the default order SonarCloud: the limit setting and the peak were mutable globals (S5421), the peak loop used relaxed order (S8417) and had an empty body (S108). They are function-local statics now, the loop uses the default order and says why it loops.
This commit is contained in:
@@ -39,16 +39,14 @@ constexpr std::size_t max_log_line{1024}; // bytes of the text, a longer one is
|
||||
// the largest file a screen reads: its script or module, a file below its folder
|
||||
constexpr std::uintmax_t max_screen_file{16 * 1024 * 1024};
|
||||
// the memory a Lua screen may hold: its Lua heap, and the pixels of its images, fonts and sprites. checked with a full
|
||||
// collection, so its garbage doesn't count; between the collections it may take the screen up to twice this
|
||||
// collection, so its garbage doesn't count; between the collections it may take the screen up to twice this, which
|
||||
// the screen's allocator refuses to go over as each block is asked for
|
||||
constexpr std::size_t max_screen_memory{128 * 1024 * 1024};
|
||||
|
||||
// true when the Lua screens count their memory in an allocator of their own, which refuses what goes over
|
||||
// max_screen_memory as it is asked for: LuaJIT 2.1 in its GC64 mode, a 32-bit build. LuaJIT 2.0 on x64 (the game's on
|
||||
// Windows) and 2.1 on x64 without GC64 take no allocator; there the memory is checked by the instruction hook every
|
||||
// so many instructions, and before what makes images and large strings in one call (string.rep, string.format,
|
||||
// table.concat, string.gsub), but not at the concatenation operator: one concatenation of large strings is made
|
||||
// whole, as far as LuaJIT's own heap goes (2 GB on x64), and the screen is stopped at the next check
|
||||
bool lua_screen_memory_allocator();
|
||||
// for the unit tests, which fill the memory of a screen and stay small: the memory a Lua screen made from now on may
|
||||
// hold in place of max_screen_memory, and the most any Lua screen held since it was set (its heap and its objects)
|
||||
void set_lua_screen_memory_limit(std::size_t Bytes);
|
||||
std::size_t lua_screen_memory_peak();
|
||||
|
||||
struct pending_command
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user