@@ -16,6 +16,7 @@ local tonumber = tonumber
16
16
local tostring = tostring
17
17
local rawget = rawget
18
18
local select = select
19
+ local tb_clear = require " table.clear"
19
20
-- local error = error
20
21
21
22
@@ -24,7 +25,8 @@ if not ok or type(new_tab) ~= "function" then
24
25
new_tab = function (narr , nrec ) return {} end
25
26
end
26
27
27
-
28
+ local tab_pool_len = 0
29
+ local tab_pool = new_tab (16 , 0 )
28
30
local _M = new_tab (0 , 55 )
29
31
30
32
_M ._VERSION = ' 0.30'
@@ -59,6 +61,27 @@ local unsub_commands = {
59
61
local mt = { __index = _M }
60
62
61
63
64
+ local function get_tab_from_pool ()
65
+ if tab_pool_len > 0 then
66
+ tab_pool_len = tab_pool_len - 1
67
+ return tab_pool [tab_pool_len + 1 ]
68
+ end
69
+
70
+ return new_tab (24 , 0 ) -- one field takes 5 slots
71
+ end
72
+
73
+
74
+ local function put_tab_into_pool (tab )
75
+ if tab_pool_len >= 32 then
76
+ return
77
+ end
78
+
79
+ tb_clear (tab )
80
+ tab_pool_len = tab_pool_len + 1
81
+ tab_pool [tab_pool_len ] = tab
82
+ end
83
+
84
+
62
85
function _M .new (self )
63
86
local sock , err = tcp ()
64
87
if not sock then
305
328
local function _gen_req (args )
306
329
local nargs = # args
307
330
308
- local req = new_tab (nargs * 5 + 1 , 0 )
309
- req [1 ] = " *" .. nargs .. " \r\n "
310
- local nbits = 2
331
+ local req = get_tab_from_pool ()
332
+ req [1 ] = " *"
333
+ req [2 ] = nargs
334
+ req [3 ] = " \r\n "
335
+ local nbits = 4
311
336
312
337
for i = 1 , nargs do
313
338
local arg = args [i ]
@@ -355,6 +380,8 @@ local function _do_cmd(self, ...)
355
380
-- print("request: ", table.concat(req))
356
381
357
382
local bytes , err = sock :send (req )
383
+ put_tab_into_pool (req )
384
+
358
385
if not bytes then
359
386
return nil , err
360
387
end
@@ -625,6 +652,10 @@ function _M.commit_pipeline(self)
625
652
end
626
653
627
654
local bytes , err = sock :send (reqs )
655
+ for _ , req in ipairs (reqs ) do
656
+ put_tab_into_pool (req )
657
+ end
658
+
628
659
if not bytes then
629
660
return nil , err
630
661
end
0 commit comments