Skip to content

Commit 3535632

Browse files
committed
optimize: we now cache the userdata metatable (for the __gc metamethod) in the lua registry for the datagram-typed cosockets.
1 parent 3fd3234 commit 3535632

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/ngx_http_lua_socket_udp.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ enum {
6868

6969

7070
static char ngx_http_lua_socket_udp_metatable_key;
71+
static char ngx_http_lua_udp_udata_metatable_key;
7172
static u_char ngx_http_lua_socket_udp_buffer[UDP_MAX_DATAGRAM_SIZE];
7273

7374

@@ -103,6 +104,14 @@ ngx_http_lua_inject_socket_udp_api(ngx_log_t *log, lua_State *L)
103104
lua_rawset(L, LUA_REGISTRYINDEX);
104105
/* }}} */
105106

107+
/* udp socket object metatable */
108+
lua_pushlightuserdata(L, &ngx_http_lua_udp_udata_metatable_key);
109+
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
110+
lua_pushcfunction(L, ngx_http_lua_socket_udp_upstream_destroy);
111+
lua_setfield(L, -2, "__gc");
112+
lua_rawset(L, LUA_REGISTRYINDEX);
113+
/* }}} */
114+
106115
lua_pop(L, 1);
107116
}
108117

@@ -252,9 +261,8 @@ ngx_http_lua_socket_udp_setpeername(lua_State *L)
252261
}
253262

254263
#if 1
255-
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
256-
lua_pushcfunction(L, ngx_http_lua_socket_udp_upstream_destroy);
257-
lua_setfield(L, -2, "__gc");
264+
lua_pushlightuserdata(L, &ngx_http_lua_udp_udata_metatable_key);
265+
lua_rawget(L, LUA_REGISTRYINDEX);
258266
lua_setmetatable(L, -2);
259267
#endif
260268

0 commit comments

Comments
 (0)