Attempt to speed up tpt.parts without using billions of memory

This commit is contained in:
Simon Robertshaw 2011-12-19 17:03:42 +00:00
parent 5e0bec261c
commit a523dcab79

View File

@ -14,7 +14,7 @@ int *mouseclick_functions = NULL;
int tptProperties; //Table for some TPT properties int tptProperties; //Table for some TPT properties
int tptPropertiesVersion; int tptPropertiesVersion;
int tptElements; //Table for TPT element names int tptElements; //Table for TPT element names
int tptParts, tptPartsMeta, tptElementTransitions; int tptParts, tptPartsMeta, tptElementTransitions, tptPartsCData, tptPartMeta, tptPart, cIndex;
void luacon_open(){ void luacon_open(){
int i = 0, j; int i = 0, j;
char tmpname[12]; char tmpname[12];
@ -116,33 +116,11 @@ tpt.parts = ffi.cast(\"particle *\", tpt.partsdata)\n\
ffi = nil\n\ ffi = nil\n\
tpt.partsdata = nil"); tpt.partsdata = nil");
//Since ffi is REALLY REALLY dangrous, we'll remove it from the environment completely (TODO) //Since ffi is REALLY REALLY dangrous, we'll remove it from the environment completely (TODO)
//lua_pushstring(l, "parts");
//tptPartsCData = lua_gettable(l, tptProperties);
#else #else
//This uses a lot of memory (60MB+), but very good performance
lua_newtable(l); lua_newtable(l);
tptParts = lua_gettop(l); tptParts = lua_gettop(l);
for(i = 0; i < NPART; i++)
{
int currentPart, currentPartMeta;
lua_newtable(l);
currentPart = lua_gettop(l);
lua_newtable(l);
currentPartMeta = lua_gettop(l);
lua_pushinteger(l, i);
lua_setfield(l, currentPart, "id");
lua_pushcfunction(l, luacon_partwrite);
lua_setfield(l, currentPartMeta, "__newindex");
lua_pushcfunction(l, luacon_partread);
lua_setfield(l, currentPartMeta, "__index");
lua_setmetatable(l, currentPart);
lua_rawseti (l, tptParts, i);
}
lua_setfield(l, tptProperties, "parts");
//Poor performance (nested metatabled created on get/set) but good little memory usage
/*lua_newtable(l);
tptParts = lua_gettop(l);
lua_newtable(l); lua_newtable(l);
tptPartsMeta = lua_gettop(l); tptPartsMeta = lua_gettop(l);
lua_pushcfunction(l, luacon_partswrite); lua_pushcfunction(l, luacon_partswrite);
@ -150,7 +128,19 @@ tpt.partsdata = nil");
lua_pushcfunction(l, luacon_partsread); lua_pushcfunction(l, luacon_partsread);
lua_setfield(l, tptPartsMeta, "__index"); lua_setfield(l, tptPartsMeta, "__index");
lua_setmetatable(l, tptParts); lua_setmetatable(l, tptParts);
lua_setfield(l, tptProperties, "parts");*/ lua_setfield(l, tptProperties, "parts");
lua_newtable(l);
tptPart = lua_gettop(l);
lua_newtable(l);
tptPartMeta = lua_gettop(l);
lua_pushcfunction(l, luacon_partwrite);
lua_setfield(l, tptPartMeta, "__newindex");
lua_pushcfunction(l, luacon_partread);
lua_setfield(l, tptPartMeta, "__index");
lua_setmetatable(l, tptPart);
tptPart = luaL_ref(l, LUA_REGISTRYINDEX);
#endif #endif
lua_newtable(l); lua_newtable(l);
@ -211,30 +201,29 @@ tpt.partsdata = nil");
} }
#ifndef FFI #ifndef FFI
int luacon_partread(lua_State* l){ int luacon_partread(lua_State* l){
int format, offset; int format, offset, tempinteger;
char * tempstring;
int tempinteger;
float tempfloat; float tempfloat;
int i; int i;
char * key = mystrdup(luaL_optstring(l, 2, "")); char * key = mystrdup(luaL_optstring(l, 2, ""));
offset = luacon_particle_getproperty(key, &format); offset = luacon_particle_getproperty(key, &format);
free(key);
//Get Raw Index value for particle i = cIndex;
lua_pushstring(l, "id");
lua_rawget(l, 1);
i = lua_tointeger (l, lua_gettop(l));
lua_pop(l, 1);
if(i < 0 || i >= NPART || offset==-1) if(i < 0 || i >= NPART || offset==-1)
{ {
if(i < 0 || i >= NPART) if(i < 0 || i >= NPART) {
free(key);
return luaL_error(l, "Out of range"); return luaL_error(l, "Out of range");
else } else if(strcmp(key, "id")==0) {
free(key);
lua_pushnumber(l, i);
return 1;
} else {
free(key);
return luaL_error(l, "Invalid property"); return luaL_error(l, "Invalid property");
}
} }
free(key);
switch(format) switch(format)
{ {
case 0: case 0:
@ -250,29 +239,23 @@ int luacon_partread(lua_State* l){
} }
int luacon_partwrite(lua_State* l){ int luacon_partwrite(lua_State* l){
int format, offset; int format, offset;
char * tempstring;
int tempinteger;
float tempfloat;
int i; int i;
char * key = mystrdup(luaL_optstring(l, 2, "")); char * key = mystrdup(luaL_optstring(l, 2, ""));
offset = luacon_particle_getproperty(key, &format); offset = luacon_particle_getproperty(key, &format);
free(key);
//Get Raw Index value for particle i = cIndex;
lua_pushstring(l, "id");
lua_rawget(l, 1);
i = lua_tointeger (l, lua_gettop(l));
lua_pop(l, 1);
if(i < 0 || i >= NPART || offset==-1) if(i < 0 || i >= NPART || offset==-1)
{ {
if(i < 0 || i >= NPART) if(i < 0 || i >= NPART) {
return luaL_error(l, "Out of range"); free(key);
else return luaL_error(l, "array index out of bounds");
} else {
free(key);
return luaL_error(l, "Invalid property"); return luaL_error(l, "Invalid property");
}
} }
free(key);
switch(format) switch(format)
{ {
case 0: case 0:
@ -294,23 +277,16 @@ int luacon_partsread(lua_State* l){
i = luaL_optinteger(l, 2, 0); i = luaL_optinteger(l, 2, 0);
if(i<0 || i>=NPART) if(i<0 || i>=NPART)
return luaL_error(l, "Out of range"); {
return luaL_error(l, "array index out of bounds");
}
lua_newtable(l); lua_rawgeti(l, LUA_REGISTRYINDEX, tptPart);
currentPart = lua_gettop(l); cIndex = i;
lua_newtable(l);
currentPartMeta = lua_gettop(l);
lua_pushinteger(l, i);
lua_setfield(l, currentPart, "id");
lua_pushcfunction(l, luacon_partwrite);
lua_setfield(l, currentPartMeta, "__newindex");
lua_pushcfunction(l, luacon_partread);
lua_setfield(l, currentPartMeta, "__index");
lua_setmetatable(l, currentPart);
return 1; return 1;
} }
int luacon_partswrite(lua_State* l){ int luacon_partswrite(lua_State* l){
return luaL_error(l, "Not writable"); return luaL_error(l, "table readonly");
} }
#endif #endif
int luacon_particle_getproperty(char * key, int * format) int luacon_particle_getproperty(char * key, int * format)