From 8bc76c78a69360efc7a07a3c4e92f393cca22543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Fri, 13 Aug 2010 10:17:31 +0300 Subject: db: smarter string pointer encoding (include length field) So we don't need explicit null terminator in most cases saving space. It will also speed up comparisons as getting string blob is now constant time (no strlen needed). --- lua-squarkdb.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'lua-squarkdb.c') diff --git a/lua-squarkdb.c b/lua-squarkdb.c index dbac6d0..09d4afe 100644 --- a/lua-squarkdb.c +++ b/lua-squarkdb.c @@ -227,7 +227,7 @@ static int Lsqdb_map_strings(lua_State *L) { struct sqdb *db; const char *str; - char *ptr; + unsigned char *ptr; size_t len, total, pos; db = Lsqdb_checkarg(L, 1); @@ -238,7 +238,9 @@ static int Lsqdb_map_strings(lua_State *L) lua_pushnil(L); while (lua_next(L, 2) != 0) { str = luaL_checklstring(L, -2, &len); - total += len + 1; + total += len; + if (len >= (1 << SQDB_LENGTH_BITS)) + total++; lua_pop(L, 1); } @@ -252,15 +254,20 @@ static int Lsqdb_map_strings(lua_State *L) lua_pushnil(L); while (lua_next(L, 2) != 0) { str = lua_tolstring(L, -2, &len); - memcpy(&ptr[pos], str, len + 1); lua_pop(L, 1); - /* table[key] = pos */ + /* table[key] = encoded_string_pointer */ lua_pushvalue(L, -1); - lua_pushinteger(L, pos); - lua_rawset(L, 2); + if (len >= (1 << SQDB_LENGTH_BITS)) { + lua_pushinteger(L, pos << SQDB_LENGTH_BITS); + ptr[pos++] = len; + } else { + lua_pushinteger(L, (pos << SQDB_LENGTH_BITS) + len); + } + memcpy(&ptr[pos], str, len); + pos += len; - pos += len + 1; + lua_rawset(L, 2); } return 0; -- cgit v1.2.3