Optimize table creation

master
ShadowNinja 2013-11-30 12:11:07 -05:00
parent 06baf05c64
commit 4594ba6522
5 changed files with 50 additions and 112 deletions

View File

@ -871,26 +871,13 @@ void read_groups(lua_State *L, int index,
/******************************************************************************/ /******************************************************************************/
void push_items(lua_State *L, const std::vector<ItemStack> &items) void push_items(lua_State *L, const std::vector<ItemStack> &items)
{ {
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Get the table insert function
lua_getglobal(L, "table");
lua_getfield(L, -1, "insert");
int table_insert = lua_gettop(L);
// Create and fill table // Create and fill table
lua_newtable(L); lua_createtable(L, items.size(), 0);
int table = lua_gettop(L); std::vector<ItemStack>::const_iterator iter = items.begin();
for(u32 i=0; i<items.size(); i++){ for (u32 i = 0; iter != items.end(); iter++) {
ItemStack item = items[i]; LuaItemStack::create(L, *iter);
lua_pushvalue(L, table_insert); lua_rawseti(L, -2, ++i);
lua_pushvalue(L, table);
LuaItemStack::create(L, item);
if(lua_pcall(L, 2, 0, errorhandler))
script_error(L);
} }
lua_remove(L, -2); // Remove insert
lua_remove(L, -2); // Remove table
lua_remove(L, -2); // Remove error handler
} }
/******************************************************************************/ /******************************************************************************/

View File

@ -389,67 +389,52 @@ int ModApiCraft::l_get_all_craft_recipes(lua_State *L)
ICraftDefManager *cdef = gdef->cdef(); ICraftDefManager *cdef = gdef->cdef();
CraftInput input; CraftInput input;
CraftOutput output(o_item,0); CraftOutput output(o_item,0);
std::vector<CraftDefinition*> recipes_list = cdef->getCraftRecipes(output, gdef); std::vector<CraftDefinition*> recipes_list;
if (recipes_list.empty()) recipes_list = cdef->getCraftRecipes(output, gdef);
{ if (recipes_list.empty()) {
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;
} }
// Get the table insert function
lua_getglobal(L, "table"); lua_createtable(L, recipes_list.size(), 0);
lua_getfield(L, -1, "insert"); std::vector<CraftDefinition*>::const_iterator iter = recipes_list.begin();
int table_insert = lua_gettop(L); for (u16 i = 0; iter != recipes_list.end(); iter++) {
lua_newtable(L);
int table = lua_gettop(L);
for (std::vector<CraftDefinition*>::const_iterator
i = recipes_list.begin();
i != recipes_list.end(); i++)
{
CraftOutput tmpout; CraftOutput tmpout;
tmpout.item = ""; tmpout.item = "";
tmpout.time = 0; tmpout.time = 0;
CraftDefinition *def = *i; tmpout = (*iter)->getOutput(input, gdef);
tmpout = def->getOutput(input, gdef);
std::string query = tmpout.item; std::string query = tmpout.item;
char *fmtpos, *fmt = &query[0]; char *fmtpos, *fmt = &query[0];
if (strtok_r(fmt, " ", &fmtpos) == output.item) if (strtok_r(fmt, " ", &fmtpos) == output.item) {
{ input = (*iter)->getInput(output, gdef);
input = def->getInput(output, gdef);
lua_pushvalue(L, table_insert);
lua_pushvalue(L, table);
lua_newtable(L); lua_newtable(L);
int k = 1; lua_newtable(L); // items
lua_newtable(L); std::vector<ItemStack>::const_iterator iter = input.items.begin();
for(std::vector<ItemStack>::const_iterator for (u16 j = 0; iter != input.items.end(); iter++) {
i = input.items.begin(); if (iter->empty())
i != input.items.end(); i++, k++)
{
if (i->empty())
continue; continue;
lua_pushinteger(L,k); lua_pushstring(L, iter->name.c_str());
lua_pushstring(L,i->name.c_str()); lua_rawseti(L, -2, ++j);
lua_settable(L, -3);
} }
lua_setfield(L, -2, "items"); lua_setfield(L, -2, "items");
setintfield(L, -1, "width", input.width); setintfield(L, -1, "width", input.width);
switch (input.method) { switch (input.method) {
case CRAFT_METHOD_NORMAL: case CRAFT_METHOD_NORMAL:
lua_pushstring(L,"normal"); lua_pushstring(L, "normal");
break; break;
case CRAFT_METHOD_COOKING: case CRAFT_METHOD_COOKING:
lua_pushstring(L,"cooking"); lua_pushstring(L, "cooking");
break; break;
case CRAFT_METHOD_FUEL: case CRAFT_METHOD_FUEL:
lua_pushstring(L,"fuel"); lua_pushstring(L, "fuel");
break; break;
default: default:
lua_pushstring(L,"unknown"); lua_pushstring(L, "unknown");
} }
lua_setfield(L, -2, "type"); lua_setfield(L, -2, "type");
lua_pushstring(L, &tmpout.item[0]); lua_pushstring(L, &tmpout.item[0]);
lua_setfield(L, -2, "output"); lua_setfield(L, -2, "output");
if (lua_pcall(L, 2, 0, 0)) lua_rawseti(L, -2, ++i);
script_error(L);
} }
} }
return 1; return 1;

View File

@ -448,28 +448,20 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L)
// minetest.get_objects_inside_radius(pos, radius) // minetest.get_objects_inside_radius(pos, radius)
int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L) int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
{ {
// Get the table insert function
lua_getglobal(L, "table");
lua_getfield(L, -1, "insert");
int table_insert = lua_gettop(L);
GET_ENV_PTR; GET_ENV_PTR;
// Do it // Do it
v3f pos = checkFloatPos(L, 1); v3f pos = checkFloatPos(L, 1);
float radius = luaL_checknumber(L, 2) * BS; float radius = luaL_checknumber(L, 2) * BS;
std::set<u16> ids = env->getObjectsInsideRadius(pos, radius); std::set<u16> ids = env->getObjectsInsideRadius(pos, radius);
lua_newtable(L); ScriptApiBase *script = getScriptApiBase(L);
int table = lua_gettop(L); lua_createtable(L, ids.size(), 0);
for(std::set<u16>::const_iterator std::set<u16>::const_iterator iter = ids.begin();
i = ids.begin(); i != ids.end(); i++){ for(u32 i = 0; iter != ids.end(); iter++) {
ServerActiveObject *obj = env->getActiveObject(*i); ServerActiveObject *obj = env->getActiveObject(*iter);
// Insert object reference into table // Insert object reference into table
lua_pushvalue(L, table_insert); script->objectrefGetOrCreate(obj);
lua_pushvalue(L, table); lua_rawseti(L, -2, ++i);
getScriptApiBase(L)->objectrefGetOrCreate(obj);
if(lua_pcall(L, 2, 0, 0))
script_error(L);
} }
return 1; return 1;
} }
@ -579,25 +571,16 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
ndef->getIds(lua_tostring(L, 3), filter); ndef->getIds(lua_tostring(L, 3), filter);
} }
// Get the table insert function
lua_getglobal(L, "table");
lua_getfield(L, -1, "insert");
int table_insert = lua_gettop(L);
lua_newtable(L); lua_newtable(L);
int table = lua_gettop(L); u64 i = 0;
for(s16 x=minp.X; x<=maxp.X; x++) for(s16 x = minp.X; x <= maxp.X; x++)
for(s16 y=minp.Y; y<=maxp.Y; y++) for(s16 y = minp.Y; y <= maxp.Y; y++)
for(s16 z=minp.Z; z<=maxp.Z; z++) for(s16 z = minp.Z; z <= maxp.Z; z++) {
{ v3s16 p(x, y, z);
v3s16 p(x,y,z);
content_t c = env->getMap().getNodeNoEx(p).getContent(); content_t c = env->getMap().getNodeNoEx(p).getContent();
if(filter.count(c) != 0){ if(filter.count(c) != 0) {
lua_pushvalue(L, table_insert);
lua_pushvalue(L, table);
push_v3s16(L, p); push_v3s16(L, p);
if(lua_pcall(L, 2, 0, 0)) lua_rawseti(L, -2, ++i);
script_error(L);
} }
} }
return 1; return 1;

View File

@ -50,7 +50,6 @@ int ModApiRollback::l_rollback_get_node_actions(lua_State *L)
lua_createtable(L, actions.size(), 0); lua_createtable(L, actions.size(), 0);
for (unsigned int i = 1; iter != actions.end(); ++iter, ++i) { for (unsigned int i = 1; iter != actions.end(); ++iter, ++i) {
lua_pushnumber(L, i); // Push index
lua_createtable(L, 0, 5); // Make a table with enough space pre-allocated lua_createtable(L, 0, 5); // Make a table with enough space pre-allocated
lua_pushstring(L, iter->actor.c_str()); lua_pushstring(L, iter->actor.c_str());
@ -68,7 +67,7 @@ int ModApiRollback::l_rollback_get_node_actions(lua_State *L)
push_RollbackNode(L, iter->n_new); push_RollbackNode(L, iter->n_new);
lua_setfield(L, -2, "newnode"); lua_setfield(L, -2, "newnode");
lua_settable(L, -3); // Add action table to main table lua_rawseti(L, -2, i); // Add action table to main table
} }
return 1; return 1;

View File

@ -229,16 +229,13 @@ int ModApiServer::l_get_modnames(lua_State *L)
// mods_sorted; not great performance but the number of mods on a // mods_sorted; not great performance but the number of mods on a
// server will likely be small. // server will likely be small.
for(std::list<std::string>::iterator i = mods_unsorted.begin(); for(std::list<std::string>::iterator i = mods_unsorted.begin();
i != mods_unsorted.end(); ++i) i != mods_unsorted.end(); ++i) {
{
bool added = false; bool added = false;
for(std::list<std::string>::iterator x = mods_sorted.begin(); for(std::list<std::string>::iterator x = mods_sorted.begin();
x != mods_sorted.end(); ++x) x != mods_sorted.end(); ++x) {
{
// I doubt anybody using Minetest will be using // I doubt anybody using Minetest will be using
// anything not ASCII based :) // anything not ASCII based :)
if((*i).compare(*x) <= 0) if(i->compare(*x) <= 0) {
{
mods_sorted.insert(x, *i); mods_sorted.insert(x, *i);
added = true; added = true;
break; break;
@ -248,25 +245,12 @@ int ModApiServer::l_get_modnames(lua_State *L)
mods_sorted.push_back(*i); mods_sorted.push_back(*i);
} }
// Get the table insertion function from Lua.
lua_getglobal(L, "table");
lua_getfield(L, -1, "insert");
int insertion_func = lua_gettop(L);
// Package them up for Lua // Package them up for Lua
lua_newtable(L); lua_createtable(L, mods_sorted.size(), 0);
int new_table = lua_gettop(L); std::list<std::string>::iterator iter = mods_sorted.begin();
std::list<std::string>::iterator i = mods_sorted.begin(); for (u16 i = 0; iter != mods_sorted.end(); iter++) {
while(i != mods_sorted.end()) lua_pushstring(L, iter->c_str());
{ lua_rawseti(L, -2, ++i);
lua_pushvalue(L, insertion_func);
lua_pushvalue(L, new_table);
lua_pushstring(L, (*i).c_str());
if(lua_pcall(L, 2, 0, 0) != 0)
{
script_error(L);
}
++i;
} }
return 1; return 1;
} }