The Variant class is now meant to be used on the stack instead of allocated on the heap. As a result (and because of related changes), performance of Channels has roughly doubled when pushing and popping most types.

This commit is contained in:
Alex Szpakowski
2016-02-14 16:39:32 -04:00
parent 7995e6ba85
commit 23fcaec89f
13 changed files with 287 additions and 321 deletions
+10 -29
View File
@@ -33,18 +33,12 @@ namespace thread
LuaThread::LuaThread(const std::string &name, love::Data *code)
: code(code)
, name(name)
, args(nullptr)
, nargs(0)
{
threadName = name;
}
LuaThread::~LuaThread()
{
// No args should still exist at this point,
// but you never know.
for (int i = 0; i < nargs; ++i)
args[i]->release();
}
void LuaThread::threadFunction()
@@ -74,16 +68,12 @@ void LuaThread::threadFunction()
error = luax_tostring(L, -1);
else
{
int pushedargs = nargs;
for (int i = 0; i < nargs; ++i)
{
args[i]->toLua(L);
args[i]->release();
}
// Set both args and nargs to nil, prevents the deconstructor from
// accessing it again.
nargs = 0;
args = nullptr;
int pushedargs = (int) args.size();
for (int i = 0; i < pushedargs; i++)
args[i].toLua(L);
args.clear();
if (lua_pcall(L, pushedargs, 0, 0) != 0)
error = luax_tostring(L, -1);
@@ -97,14 +87,9 @@ void LuaThread::threadFunction()
this->release();
}
bool LuaThread::start(Variant **args, int nargs)
bool LuaThread::start(const std::vector<Variant> &args)
{
for (int i = 0; i < this->nargs; ++i)
this->args[i]->release();
this->args = args;
this->nargs = nargs;
return Threadable::start();
}
@@ -126,16 +111,12 @@ void LuaThread::onError()
p.type = THREAD_THREAD_ID;
p.object = this;
std::vector<StrongRef<Variant>> vargs = {
new Variant(THREAD_THREAD_ID, &p),
new Variant(error.c_str(), error.length())
std::vector<Variant> vargs = {
Variant(p.type, &p),
Variant(error.c_str(), error.length())
};
event::Message *msg = new event::Message("threaderror", vargs);
for (const StrongRef<Variant> &v : vargs)
v->release();
eventmodule->push(msg);
msg->release();
}