Replace lua_newtable() with lua_createtable()

Lua can preallocate space in table for array or non-array elements type.
This should improve performance when setting table, so when we can we
use lua_createtable() since this is just a gain.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-04-25 15:04:27 +02:00
parent e8de7a4605
commit dc29720ef7
8 changed files with 33 additions and 26 deletions

View File

@ -1598,7 +1598,7 @@ luaA_client_geometry(lua_State *L)
static inline int
luaA_pushstruts(lua_State *L, strut_t struts)
{
lua_newtable(L);
lua_createtable(L, 4, 0);
lua_pushnumber(L, struts.left);
lua_setfield(L, -2, "left");
lua_pushnumber(L, struts.right);
@ -1980,7 +1980,7 @@ luaA_client_index(lua_State *L)
{
const char *u_or_p = NULL;
lua_newtable(L);
lua_createtable(L, 0, 1);
if(c->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
u_or_p = "user_position";
@ -1989,7 +1989,7 @@ luaA_client_index(lua_State *L)
if(u_or_p)
{
lua_newtable(L);
lua_createtable(L, 0, 2);
lua_pushnumber(L, c->size_hints.x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, c->size_hints.y);
@ -2005,7 +2005,7 @@ luaA_client_index(lua_State *L)
if(u_or_p)
{
lua_newtable(L);
lua_createtable(L, 0, 2);
lua_pushnumber(L, c->size_hints.width);
lua_setfield(L, -2, "width");
lua_pushnumber(L, c->size_hints.height);

35
dbus.c
View File

@ -72,15 +72,17 @@ a_dbus_message_iter(DBusMessageIter *iter)
break;
case DBUS_TYPE_STRUCT:
{
/* create a new table to store all the value */
lua_newtable(globalconf.L);
DBusMessageIter subiter;
/* initialize a sub iterator */
dbus_message_iter_recurse(iter, &subiter);
/* create a new table to store the dict */
int n = a_dbus_message_iter(&subiter);
/* create a new table to store all the value */
lua_createtable(globalconf.L, n, 0);
/* move the table before array elements */
lua_insert(globalconf.L, - n - 1);
for(int i = n; i > 0; i--)
lua_rawseti(globalconf.L, - i - 1, i);
}
@ -102,8 +104,8 @@ a_dbus_message_iter(DBusMessageIter *iter)
case dbustype: \
{ \
const type *data; \
lua_newtable(globalconf.L); \
dbus_message_iter_get_fixed_array(&sub, &data, &datalen); \
lua_createtable(globalconf.L, datalen, 0); \
for(int i = 0; i < datalen; i++) \
{ \
lua_pushnumber(globalconf.L, data[i]); \
@ -129,8 +131,8 @@ a_dbus_message_iter(DBusMessageIter *iter)
case DBUS_TYPE_BOOLEAN:
{
const bool *b;
lua_newtable(globalconf.L);
dbus_message_iter_get_fixed_array(&sub, &b, &datalen);
lua_createtable(globalconf.L, datalen, 0);
for(int i = 0; i < datalen; i++)
{
lua_pushboolean(globalconf.L, b[i]);
@ -143,30 +145,35 @@ a_dbus_message_iter(DBusMessageIter *iter)
else if(array_type == DBUS_TYPE_DICT_ENTRY)
{
DBusMessageIter subiter;
lua_newtable(globalconf.L);
/* initialize a sub iterator */
dbus_message_iter_recurse(iter, &subiter);
/* get the keys and the values
* n is the number of entry in * dict */
* n is the number of entry in dict */
int n = a_dbus_message_iter(&subiter);
/* create a new table to store all the value */
lua_createtable(globalconf.L, n, 0);
/* move the table before array elements */
lua_insert(globalconf.L, - (n * 2) - 1);
for(int i = 0; i < n; i ++)
lua_rawset(globalconf.L, - (n * 2) - 1 + i * 2);
}
else
{
DBusMessageIter subiter;
lua_newtable(globalconf.L);
/* prepare to dig into the array*/
dbus_message_iter_recurse(iter, &subiter);
/* now iterate over every element of the array */
int n = a_dbus_message_iter(&subiter);
/* create a new table to store all the value */
lua_createtable(globalconf.L, n, 0);
/* move the table before array elements */
lua_insert(globalconf.L, - n - 1);
for(int i = n; i > 0; i--)
lua_rawseti(globalconf.L, - i - 1, i);
}
@ -225,7 +232,7 @@ a_dbus_process_request(DBusConnection *dbus_connection, DBusMessage *msg)
if(globalconf.hooks.dbus == LUA_REFNIL)
return;
lua_newtable(globalconf.L);
lua_createtable(globalconf.L, 0, 5);
switch(dbus_message_get_type(msg))
{

View File

@ -692,7 +692,7 @@ keygrabber_handlekpress(lua_State *L, xcb_key_press_event_t *e)
if(!key_press_lookup_string(e, buf, countof(buf), &ksym))
return false;
lua_newtable(L);
lua_createtable(L, 0, 8);
lua_pushboolean(L, e->state & XCB_MOD_MASK_CONTROL);
lua_setfield(L, -2, "Control");

2
luaa.c
View File

@ -418,7 +418,7 @@ luaA_table2wtable(lua_State *L)
return;
lua_newtable(L); /* create *real* content table */
lua_newtable(L); /* metatable */
lua_createtable(L, 0, 5); /* metatable */
lua_pushvalue(L, -2); /* copy content table */
lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
lua_pushcclosure(L, luaA_wtable_ipairs, 2);

4
luaa.h
View File

@ -173,7 +173,7 @@ luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
static inline int
luaA_pusharea(lua_State *L, area_t geometry)
{
lua_newtable(L);
lua_createtable(L, 0, 4);
lua_pushnumber(L, geometry.x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, geometry.y);
@ -316,7 +316,7 @@ luaA_getopt_padding(lua_State *L, int idx, padding_t *dpadding)
static inline int
luaA_pushpadding(lua_State *L, padding_t *padding)
{
lua_newtable(L);
lua_createtable(L, 0, 4);
lua_pushnumber(L, padding->right);
lua_setfield(L, -2, "right");
lua_pushnumber(L, padding->left);

View File

@ -393,13 +393,13 @@ luaA_mouse_newindex(lua_State *L)
int
luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
{
lua_newtable(L);
lua_createtable(L, 0, 2);
lua_pushnumber(L, x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, y);
lua_setfield(L, -2, "y");
lua_newtable(L);
lua_createtable(L, 5, 0);
int i = 1;

View File

@ -431,7 +431,7 @@ luaA_screen_tags(lua_State *L)
}
else
{
lua_newtable(L);
lua_createtable(L, s->tags.len, 0);
for(i = 0; i < s->tags.len; i++)
{
tag_push(L, s->tags.tab[i]);

View File

@ -77,7 +77,7 @@ spawn_monitor_event(SnMonitorEvent *event, void *data)
SnStartupSequence *sequence = sn_monitor_event_get_startup_sequence(event);
SnMonitorEventType event_type = sn_monitor_event_get_type(event);
lua_newtable(globalconf.L);
lua_createtable(globalconf.L, 0, 2);
lua_pushstring(globalconf.L, sn_startup_sequence_get_id(sequence));
lua_setfield(globalconf.L, -2, "id");