Skip to content

Commit d8a2664

Browse files
committed
optimize: now we pre-calculate the exact size of the resulting Lua table and preallocate the table space, which makes it 8%+ faster for a request with a dozen request headers and 40%+ faster for a hundred request headers.
1 parent f998256 commit d8a2664

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/ngx_http_lua_headers.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,18 @@ ngx_http_lua_ngx_req_get_headers(lua_State *L)
322322

323323
ngx_http_lua_check_fake_request(L, r);
324324

325-
lua_createtable(L, 0, 4);
325+
part = &r->headers_in.headers.part;
326+
count = part->nelts;
327+
while (part->next) {
328+
part = part->next;
329+
count += part->nelts;
330+
}
331+
332+
if (max > 0 && count > max) {
333+
count = max;
334+
}
335+
336+
lua_createtable(L, 0, count);
326337

327338
if (!raw) {
328339
lua_pushlightuserdata(L, &ngx_http_lua_req_get_headers_metatable_key);
@@ -366,7 +377,7 @@ ngx_http_lua_ngx_req_get_headers(lua_State *L)
366377
"lua request header: \"%V: %V\"",
367378
&header[i].key, &header[i].value);
368379

369-
if (max > 0 && ++count == max) {
380+
if (--count == 0) {
370381
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
371382
"lua hit request header limit %d", max);
372383

0 commit comments

Comments
 (0)