0

In C++, I have a vector of sol::table stored:

std::vector<sol::table> vecInstLuaTable;

At some point in my code, I want to assign one of these tables to a global table called "me" :

XEROOM::EXPOSED::SetScopeT(CURRENTROOMPTR->vecInstLuaTable[index]);
XELUASTATE::RunFunction(func);
XEROOM::EXPOSED::ResetScope();

void SetScopeT(const sol::table& t) {
    vecStackScope.push_back(lua["me"]);
    lua["me"] = t;
}

void ResetScope() {
    lua["me"] = vecStackScope.back();
    vecStackScope.pop_back();
}

The functions SetScopeT() and ResetScope() apparently take quite the amount of time. I understand that lua-C++ interop is not a free ride, but I have 2 questions about it:

Apparently in SetScopeT(), using a const sol::table& parameter instead of a sol::table improves performance by a lot. Why though? Isn't sol::table a masqueraded reference by itself? Same goes by using sol::protected_function& and sol::protected_function elsewhere. Is the method described above efficient or am I doing something unbelievably stupid for such a simple task? If so, what's the best (fastest) way to achieve it?

wohlstad
  • 12,661
  • 10
  • 26
  • 39

1 Answers1

0

"The functions SetScopeT() and ResetScope() apparently take quite the amount of time"

From the C++ perspective, there are only two important categories of operations: the lookup of lua["me"] (trivial optimization: do it once in SetScopeT) and value operations (copy/assign) on a sol::table. Since you say that it takes a lot of time, it seems entirely expected that adding another copy (for pass-by-value) makes it even slower.

What's easily fixed: you're using vecStackScope as a stack. That means you have the occasional vector reallocation. This can be avoided by moving to std::list. You're losing the random-access performance, but you don't use that anyway.

MSalters
  • 173,980
  • 10
  • 155
  • 350