Open
Conversation
theweakgod
reviewed
Mar 17, 2026
Comment on lines
+248
to
+255
| local _bytes = {} | ||
| for _, proto_str in ipairs(alpn) do | ||
| _bytes[#_bytes + 1] = #proto_str | ||
| for _, proto_byte in ipairs( | ||
| { str_byte(proto_str, 1, #proto_str) }) do | ||
| _bytes[#_bytes + 1] = proto_byte | ||
| end | ||
| end |
Contributor
There was a problem hiding this comment.
The str_byte loop works, but we can simplify this quite a bit.
Since ALPN identifiers are a finite IANA-registered set, a pre-built wire-format lookup avoids runtime byte construction entirely for common protocols, with a fallback for anything else:
local ALPN_WIRE = {
["h2"] = "\x02h2",
["http/1.1"] = "\x08http/1.1",
["http/1.0"] = "\x08http/1.0",
}
local bytes_array = {}
for _, p in ipairs(alpn) do
bytes_array[#bytes_array+1] = ALPN_WIRE[p] or (string.char(#p) .. p)
end
local _bytes = table.concat(bytes_array)
alpn_str[0].data = ffi.new("unsigned char[?]", #_bytes, _bytes)
alpn_str[0].len = #_bytestable.concat produces the full wire string in one allocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I hereby granted the copyright of the changes in this pull request
to the authors of this lua-resty-core project.