Skip to content

Commit 3fd3234

Browse files
committed
optimize: we now cache the userdata metatable (for the __gc metamethod) in the stream-typed cosocket implementation.
1 parent 58f916f commit 3fd3234

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

src/ngx_http_lua_socket_tcp.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ enum {
173173
static char ngx_http_lua_req_socket_metatable_key;
174174
static char ngx_http_lua_raw_req_socket_metatable_key;
175175
static char ngx_http_lua_tcp_socket_metatable_key;
176+
static char ngx_http_lua_upstream_udata_metatable_key;
177+
static char ngx_http_lua_downstream_udata_metatable_key;
178+
static char ngx_http_lua_pool_udata_metatable_key;
179+
static char ngx_http_lua_pattern_udata_metatable_key;
176180

177181

178182
void
@@ -280,6 +284,38 @@ ngx_http_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
280284
lua_setfield(L, -2, "__index");
281285
lua_rawset(L, LUA_REGISTRYINDEX);
282286
/* }}} */
287+
288+
/* {{{upstream userdata metatable */
289+
lua_pushlightuserdata(L, &ngx_http_lua_upstream_udata_metatable_key);
290+
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
291+
lua_pushcfunction(L, ngx_http_lua_socket_tcp_upstream_destroy);
292+
lua_setfield(L, -2, "__gc");
293+
lua_rawset(L, LUA_REGISTRYINDEX);
294+
/* }}} */
295+
296+
/* {{{downstream userdata metatable */
297+
lua_pushlightuserdata(L, &ngx_http_lua_downstream_udata_metatable_key);
298+
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
299+
lua_pushcfunction(L, ngx_http_lua_socket_downstream_destroy);
300+
lua_setfield(L, -2, "__gc");
301+
lua_rawset(L, LUA_REGISTRYINDEX);
302+
/* }}} */
303+
304+
/* {{{socket pool userdata metatable */
305+
lua_pushlightuserdata(L, &ngx_http_lua_pool_udata_metatable_key);
306+
lua_createtable(L, 0, 1); /* metatable */
307+
lua_pushcfunction(L, ngx_http_lua_socket_shutdown_pool);
308+
lua_setfield(L, -2, "__gc");
309+
lua_rawset(L, LUA_REGISTRYINDEX);
310+
/* }}} */
311+
312+
/* {{{socket compiled pattern userdata metatable */
313+
lua_pushlightuserdata(L, &ngx_http_lua_pattern_udata_metatable_key);
314+
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
315+
lua_pushcfunction(L, ngx_http_lua_socket_cleanup_compiled_pattern);
316+
lua_setfield(L, -2, "__gc");
317+
lua_rawset(L, LUA_REGISTRYINDEX);
318+
/* }}} */
283319
}
284320

285321

@@ -487,9 +523,8 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
487523
}
488524

489525
#if 1
490-
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
491-
lua_pushcfunction(L, ngx_http_lua_socket_tcp_upstream_destroy);
492-
lua_setfield(L, -2, "__gc");
526+
lua_pushlightuserdata(L, &ngx_http_lua_upstream_udata_metatable_key);
527+
lua_rawget(L, LUA_REGISTRYINDEX);
493528
lua_setmetatable(L, -2);
494529
#endif
495530

@@ -3015,9 +3050,8 @@ ngx_http_lua_socket_tcp_receiveuntil(lua_State *L)
30153050
return luaL_error(L, "no memory");
30163051
}
30173052

3018-
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
3019-
lua_pushcfunction(L, ngx_http_lua_socket_cleanup_compiled_pattern);
3020-
lua_setfield(L, -2, "__gc");
3053+
lua_pushlightuserdata(L, &ngx_http_lua_pattern_udata_metatable_key);
3054+
lua_rawget(L, LUA_REGISTRYINDEX);
30213055
lua_setmetatable(L, -2);
30223056

30233057
ngx_memzero(cp, size);
@@ -3707,9 +3741,8 @@ ngx_http_lua_req_socket(lua_State *L)
37073741
}
37083742

37093743
#if 1
3710-
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
3711-
lua_pushcfunction(L, ngx_http_lua_socket_downstream_destroy);
3712-
lua_setfield(L, -2, "__gc");
3744+
lua_pushlightuserdata(L, &ngx_http_lua_downstream_udata_metatable_key);
3745+
lua_rawget(L, LUA_REGISTRYINDEX);
37133746
lua_setmetatable(L, -2);
37143747
#endif
37153748

@@ -3967,9 +4000,8 @@ static int ngx_http_lua_socket_tcp_setkeepalive(lua_State *L)
39674000
return luaL_error(L, "no memory");
39684001
}
39694002

3970-
lua_createtable(L, 0, 1); /* metatable */
3971-
lua_pushcfunction(L, ngx_http_lua_socket_shutdown_pool);
3972-
lua_setfield(L, -2, "__gc");
4003+
lua_pushlightuserdata(L, &ngx_http_lua_pool_udata_metatable_key);
4004+
lua_rawget(L, LUA_REGISTRYINDEX);
39734005
lua_setmetatable(L, -2);
39744006

39754007
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,

0 commit comments

Comments
 (0)