ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.2k stars 516 forks source link

Efficiently assigning a C++ stored table to a global table #1415

Open vdweller84 opened 2 years ago

vdweller84 commented 2 years ago

Hello, this is a technical question and not an issue. I hope it is acceptable to ask here!

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:

1) 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::protectedfunction elsewhere. 2) 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?