lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Thanks.  I tried that, and the result is .75 seconds--but that's still
> twice the time as 5.0.  Also, the same code running on 5.0 is even
> faster than before (.3).

(code attached)

-- 
Glenn Maynard
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

int iFreeListRef = 0;
int iMaxReference = 0;
int myluaL_ref (lua_State *L) {
	int ref;
	if (lua_isnil(L, -1)) {
		lua_pop(L, 1);  /* remove from stack */
		return LUA_REFNIL;  /* `nil' has a unique fixed reference */
	}

	ref = iFreeListRef;
	if (ref != 0) {  /* any free element? */
		lua_rawgeti(L, LUA_REGISTRYINDEX, ref);  /* remove it from list */
		iFreeListRef = (int)lua_tonumber(L, -1);  /* iFreeListRef = LUA_REGISTRYINDEX[iFreeListRef] */
		lua_pop(L, 1);  /* remove from stack */
	}
	else {  /* no free elements */
		++iMaxReference;
		ref = iMaxReference;  /* create new reference */
	}
	lua_rawseti(L, LUA_REGISTRYINDEX, ref);
	return ref;
}


void myluaL_unref (lua_State *L, int ref) {
	if (ref >= 0) {
		lua_pushnumber(L, iFreeListRef);
		lua_rawseti(L, LUA_REGISTRYINDEX, ref);  /* LUA_REGISTRYINDEX[ref] = iFreeListRef */
		iFreeListRef = ref;
		if(ref == iMaxReference)
			--iMaxReference;
	}
}

main()
{
	lua_State *L = lua_open();

	lua_pushstring( L, "testing" );
	int iReference = myluaL_ref( L );

	for(int i = 0; i < 1000000; ++i)
	{
		lua_rawgeti( L, LUA_REGISTRYINDEX, iReference );
		int iReference2 = myluaL_ref( L );
		myluaL_unref( L, iReference2 );
	}
}