From 12b5da2417d7e028e87c687d95b5597757d25c2f Mon Sep 17 00:00:00 2001 From: David Lowndes Date: Fri, 22 Nov 2024 22:27:28 +0000 Subject: [PATCH] Remove some unnecessary dynamic vectors/strings (#7862) * Eliminate dynamic vector & string storage where there's a simpler alternative. Code tweak in prepareForEvaluation to use logical OR rather than bitwise and const the initial bool variable. * Pick the nit. * Fix issue with constexpr of initializer_list that MSVC doesn't object to. * Restore named sandboxWhitelist, & sandboxLibraryTables. More ref usage. 2 functions can be static. * format --------- Co-authored-by: David Lowndes --- src/common/LuaSupport.cpp | 33 +++++++++++-------- .../modulators/FormulaModulationHelper.cpp | 13 +++----- src/surge-testrunner/UnitTestsDSP.cpp | 4 +-- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/common/LuaSupport.cpp b/src/common/LuaSupport.cpp index 96f8b4dd73f..57bfc929cbe 100644 --- a/src/common/LuaSupport.cpp +++ b/src/common/LuaSupport.cpp @@ -105,7 +105,7 @@ int Surge::LuaSupport::parseStringDefiningMultipleFunctions( // sloppy int res = 0; std::vector frev(functions.rbegin(), functions.rend()); - for (auto functionName : frev) + for (const auto &functionName : frev) { lua_getglobal(L, functionName.c_str()); if (lua_isfunction(L, -1)) @@ -124,7 +124,7 @@ int Surge::LuaSupport::parseStringDefiningMultipleFunctions( #endif } -int lua_limitRange(lua_State *L) +static int lua_limitRange(lua_State *L) { #if HAS_LUA auto x = luaL_checknumber(L, -3); @@ -137,7 +137,7 @@ int lua_limitRange(lua_State *L) } // custom print that outputs limited amount of arguments and restricts use to strings and numbers -int lua_sandboxPrint(lua_State *L) +static int lua_sandboxPrint(lua_State *L) { #if HAS_LUA int n = lua_gettop(L); // number of arguments @@ -181,38 +181,45 @@ bool Surge::LuaSupport::setSurgeFunctionEnvironment(lua_State *L) lua_setfield(L, eidx, sharedTableName); // add whitelisted functions and modules - std::vector sandboxWhitelist = {"pairs", "ipairs", "unpack", + + // clang-format off + static constexpr std::initializer_list sandboxWhitelist + {"pairs", "ipairs", "unpack", "next", "type", "tostring", "tonumber", "setmetatable", "error"}; + // clang-format on + for (const auto &f : sandboxWhitelist) { - lua_getglobal(L, f.c_str()); // stack: f>t>f - if (lua_isnil(L, -1)) // check if the global exists + lua_getglobal(L, f); // stack: f>t>f + if (lua_isnil(L, -1)) // check if the global exists { lua_pop(L, 1); - std::cout << "Error: global not found [ " << f.c_str() << " ]" << std::endl; + std::cout << "Error: global not found [ " << f << " ]" << std::endl; continue; } - lua_setfield(L, -2, f.c_str()); // stack: f>t + lua_setfield(L, -2, f); // stack: f>t } // add library tables - std::vector sandboxLibraryTables = {"math", "string", "table", "bit"}; + // clang-format off + static constexpr std::initializer_list sandboxLibraryTables = {"math", "string", "table", "bit"}; + // clang-format on for (const auto &t : sandboxLibraryTables) { - lua_getglobal(L, t.c_str()); // stack: f>t>(t) + lua_getglobal(L, t); // stack: f>t>(t) int gidx = lua_gettop(L); if (!lua_istable(L, gidx)) { lua_pop(L, 1); - std::cout << "Error: not a table [ " << t.c_str() << " ]" << std::endl; + std::cout << "Error: not a table [ " << t << " ]" << std::endl; continue; } // we want to add to a local table so the entries in the global table can't be overwritten lua_createtable(L, 0, 10); // stack: f>t>(t)>t - lua_setfield(L, eidx, t.c_str()); - lua_getfield(L, eidx, t.c_str()); + lua_setfield(L, eidx, t); + lua_getfield(L, eidx, t); int lidx = lua_gettop(L); lua_pushnil(L); diff --git a/src/common/dsp/modulators/FormulaModulationHelper.cpp b/src/common/dsp/modulators/FormulaModulationHelper.cpp index 90f1f2853a4..f155aaa3323 100644 --- a/src/common/dsp/modulators/FormulaModulationHelper.cpp +++ b/src/common/dsp/modulators/FormulaModulationHelper.cpp @@ -327,11 +327,9 @@ end { lua_pushnumber(s.L, i + 1); lua_gettable(s.L, -2); - bool res = false; - if (lua_isboolean(s.L, -1)) - res = lua_toboolean(s.L, -1); + const bool res = (lua_isboolean(s.L, -1)) ? lua_toboolean(s.L, -1) : false; lua_pop(s.L, 1); - s.subAnyMacro = s.subAnyMacro | res; + s.subAnyMacro = s.subAnyMacro || res; s.subMacros[i] = res; } } @@ -832,10 +830,9 @@ std::vector createDebugDataOfModState(const EvaluatorState &es) } }; - std::vector tablesList = {es.stateName, sharedTableName}; - for (const auto &t : tablesList) + for (const auto &t : {es.stateName, sharedTableName}) { - lua_getglobal(es.L, t.c_str()); + lua_getglobal(es.L, t); if (!lua_istable(es.L, -1)) { lua_pop(es.L, -1); @@ -1007,4 +1004,4 @@ end } } // namespace Formula -} // namespace Surge \ No newline at end of file +} // namespace Surge diff --git a/src/surge-testrunner/UnitTestsDSP.cpp b/src/surge-testrunner/UnitTestsDSP.cpp index 4c33022c327..47eb18f4130 100644 --- a/src/surge-testrunner/UnitTestsDSP.cpp +++ b/src/surge-testrunner/UnitTestsDSP.cpp @@ -239,7 +239,7 @@ TEST_CASE("Unison at Different Sample Rates", "[osc]") } }; - std::vector srs = {{44100, 48000}}; + static constexpr std::initializer_list srs{44100, 48000}; SECTION("Wavetable Oscillator") { @@ -746,4 +746,4 @@ TEST_CASE("Oscillator Onset", "[dsp]") // See issue 7570 << std::endl;*/ } } -} \ No newline at end of file +}