Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it a good idea to add max_buffer_reuse? #91

Open
edo888 opened this issue Oct 29, 2022 · 4 comments
Open

Is it a good idea to add max_buffer_reuse? #91

edo888 opened this issue Oct 29, 2022 · 4 comments

Comments

@edo888
Copy link

edo888 commented Oct 29, 2022

local hex_buf = ffi_new(str_type, BUF_MAX_LEN)

@zhuizhuhaomeng
Copy link
Contributor

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

@edo888
Copy link
Author

edo888 commented Oct 29, 2022

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

@zhuizhuhaomeng
Copy link
Contributor

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

@edo888
Copy link
Author

edo888 commented Nov 2, 2022

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants