lua-resty-string icon indicating copy to clipboard operation
lua-resty-string copied to clipboard

Is it a good idea to add max_buffer_reuse?

Open edo888 opened this issue 3 years ago • 4 comments

https://github.com/openresty/lua-resty-string/blob/78e5020229718c557484191c891a68097907d818/lib/resty/string.lua#L27

edo888 avatar Oct 29 '22 04:10 edo888

I think it is a good idea. but we need a better function name.

zhuizhuhaomeng avatar Oct 29 '22 04:10 zhuizhuhaomeng

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

edo888 avatar Oct 29 '22 07:10 edo888

I final got what you mean. Why do you need the max reuse time ?

zhuizhuhaomeng avatar Nov 02 '22 15:11 zhuizhuhaomeng

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!

edo888 avatar Nov 02 '22 15:11 edo888