lua-resty-string
lua-resty-string copied to clipboard
Is it a good idea to add max_buffer_reuse?
https://github.com/openresty/lua-resty-string/blob/78e5020229718c557484191c891a68097907d818/lib/resty/string.lua#L27
I think it is a good idea. but we need a better function name.
Does this look good?
local str_type = ffi.typeof("uint8_t[?]")
local counter = 0
local BUF_MAX_REUSE = 10000
local BUF_MAX_LEN = 1024
local hex_buf = ffi_new(str_type, BUF_MAX_LEN)
function _M.to_hex(s)
local len = #s
local buf_len = len * 2
local buf
if buf_len <= BUF_MAX_LEN then
counter = counter + 1
if counter > BUF_MAX_REUSE then
hex_buf = ffi_new(str_type, BUF_MAX_LEN)
counter = 0
end
buf = hex_buf
else
buf = ffi_new(str_type, buf_len)
end
C.ngx_hex_dump(buf, s, len)
return ffi_str(buf, buf_len)
end
I final got what you mean. Why do you need the max reuse time ?
I'm worried about burning out RAM when using to_hex heavily, so I'm asking your opinion about it. Is it a good idea or it should be fine? Thanks!