lua-users home
lua-l archive

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


Tim Hill <drtimhill@gmail.com> writes:
> It's pretty late here, but looking at your code isn't it always trying
> to get not-present values? If so, I'm not sure if this can be extended
> to Lua behavior when it gets values that are present in the Registry?

Indeed, and for the "integer" case, it uses huge integers, not the
smallish integers you'd normally use.

I've modified the example program (see attachment) to use a more
realistic case of a small number of already-present entries , and added
tests for string accesses (using both lua_rawget and lua_getfield) too.

Here's the results I get (which granted, are likely on a different CPU
etc than William used):

   ITERS: 16777216

   lua_rawgeti              0.36
   lua_rawgetp              0.82
   lua_rawget (string)      1.43
   lua_getfield (string)    1.47
   lua_pushvalue (upvalue)  0.20

   lua_rawgeti              0.36
   lua_rawgetp              0.81
   lua_rawget (string)      1.54
   lua_getfield (string)    1.49
   lua_pushvalue (upvalue)  0.23

   lua_rawgeti              0.37
   lua_rawgetp              0.92
   lua_rawget (string)      1.47
   lua_getfield (string)    1.41
   lua_pushvalue (upvalue)  0.24


-miles


#include <stdio.h>
#include <time.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#define MAX  1<<24
#define NUP  8

static int none(lua_State *L) {
	clock_t start = clock();

	for (int i = 0; i < MAX; i++) {
		lua_pushvalue(L, lua_upvalueindex(i % NUP));
		lua_pop(L, 1);
	}

	printf("lua_pushvalue (upvalue)  %4.2f\n", (double)(clock() - start) / CLOCKS_PER_SEC );

	return 0;
}

const char *str[NUP] = {
  "test1", "someslightly", "goobermax", "brunch",
  "ralphie", "flowerbud", "crankshaft", "zombiehead"
};

const int vals[NUP] = { 2987, 4, 98799, 99, 9123, 777, 3, 42 };

int main(void)
{
	lua_State *L = luaL_newstate();
	clock_t start;

	printf ("ITERS: %ld\n", MAX);

	for (int i = 0; i < NUP; i++) {
		lua_pushinteger (L, vals[i]);
		lua_rawseti (L, LUA_REGISTRYINDEX, i);
		lua_pushinteger (L, vals[i]);
		lua_rawsetp (L, LUA_REGISTRYINDEX, str[i]);
		lua_pushstring (L, str[i]);
		lua_pushinteger (L, vals[i]);
		lua_rawset (L, LUA_REGISTRYINDEX);
	}

	for (int i = 0; i < 3; i++) {
		putchar ('\n');

		start = clock();

		for (int i = 0; i < MAX; i++) {
			lua_rawgeti(L, LUA_REGISTRYINDEX, i % NUP);
			lua_pop(L, 1);
		}

		printf("lua_rawgeti              %4.2f\n", (double)(clock() - start) / CLOCKS_PER_SEC );

		start = clock();

		for (int i = 0; i < MAX; i++) {
			lua_rawgetp(L, LUA_REGISTRYINDEX, (void *)str[i % NUP]);
			lua_pop(L, 1);
		}

		printf("lua_rawgetp              %4.2f\n", (double)(clock() - start) / CLOCKS_PER_SEC );

		start = clock();

		for (int i = 0; i < MAX; i++) {
			lua_pushstring (L, str[i % NUP]);
			lua_rawget(L, LUA_REGISTRYINDEX);
			lua_pop(L, 1);
		}

		printf("lua_rawget (string)      %4.2f\n", MAX, (double)(clock() - start) / CLOCKS_PER_SEC );

		start = clock();

		for (int i = 0; i < MAX; i++) {
			lua_getfield(L, LUA_REGISTRYINDEX, str[i % NUP]);
			lua_pop(L, 1);
		}

		printf("lua_getfield (string)    %4.2f\n", (double)(clock() - start) / CLOCKS_PER_SEC );

		for (int i = 0; i < NUP; i++) {
			lua_pushnumber(L, i);
		}
		lua_pushcclosure(L, &none, NUP);
		lua_call(L, 0, 0);

		/* grow registery */
		for (int i = 0; i < 256; i++) {
			lua_pushnumber(L, 0xd0d0caca);
			luaL_ref(L, LUA_REGISTRYINDEX);
		}
	}

	return 0;
} /* main() */

-- 
values of β will give rise to dom!