@@ 202,9 202,19 @@ for _, spec in ipairs(WRAPPERS) do
local decode = wire[dec_fn]
local tag = TAG_BY_WIRE[wt]
- M[name .. '_encode'] = function(v)
- if v == nil or is_default(v) then return '' end
- return tag .. encode(v)
+ if wt == wire.WIRE_LEN then
+ -- StringValue / BytesValue: skip encode_len's `varint(#v) .. v` and
+ -- let LuaJIT fold tag + varint + body into a single multi-concat
+ -- instead of two sequential concats.
+ M[name .. '_encode'] = function(v)
+ if v == nil or is_default(v) then return '' end
+ return tag .. wire.encode_varint(#v) .. v
+ end
+ else
+ M[name .. '_encode'] = function(v)
+ if v == nil or is_default(v) then return '' end
+ return tag .. encode(v)
+ end
end
M[name .. '_decode'] = function(buf)
@@ 321,12 331,18 @@ struct_encode = function(t)
-- Each Struct entry: tag(1, LEN)=0x0a, entry_len, entry_payload
-- Entry payload: tag(1, LEN)=0x0a + key_len_prefixed_bytes
-- + tag(2, LEN)=0x12 + value_len_prefixed_bytes
+ -- Split the outer entry wrap so the final `table.concat` joins entries
+ -- in one pass (matches the codec.lua nested-message pattern). The inner
+ -- entry stays a single multi-concat: it's cheaper than three more out
+ -- slots once the entry is small.
for k, v in pairs(t) do
local key_str = type(k) == 'string' and k or tostring(k)
- local entry = '\x0a' .. wire.encode_len(key_str)
- .. '\x12' .. wire.encode_len(value_encode(v))
+ local val_body = value_encode(v)
+ local entry = '\x0a' .. wire.encode_varint(#key_str) .. key_str
+ .. '\x12' .. wire.encode_varint(#val_body) .. val_body
n = n + 1; out[n] = '\x0a'
- n = n + 1; out[n] = wire.encode_len(entry)
+ n = n + 1; out[n] = wire.encode_varint(#entry)
+ n = n + 1; out[n] = entry
end
return table.concat(out)
end
@@ 335,8 351,10 @@ list_encode = function(t)
if t == nil then return '' end
local out, n = {}, 0
for i = 1, #t do
+ local body = value_encode(t[i])
n = n + 1; out[n] = '\x0a'
- n = n + 1; out[n] = wire.encode_len(value_encode(t[i]))
+ n = n + 1; out[n] = wire.encode_varint(#body)
+ n = n + 1; out[n] = body
end
return table.concat(out)
end
@@ 464,12 482,14 @@ any_encode = function(v)
local type_url = v.type_url
if type_url ~= nil and type_url ~= '' then
n = n + 1; out[n] = '\x0a' -- field 1, LEN
- n = n + 1; out[n] = wire.encode_len(type_url)
+ n = n + 1; out[n] = wire.encode_varint(#type_url)
+ n = n + 1; out[n] = type_url
end
local value = v.value
if value ~= nil and value ~= '' then
n = n + 1; out[n] = '\x12' -- field 2, LEN
- n = n + 1; out[n] = wire.encode_len(value)
+ n = n + 1; out[n] = wire.encode_varint(#value)
+ n = n + 1; out[n] = value
end
return table.concat(out)
end
@@ 554,8 574,10 @@ local function fieldmask_encode(v)
if v == nil or #v == 0 then return '' end
local out, n = {}, 0
for i = 1, #v do
+ local s = v[i]
n = n + 1; out[n] = '\x0a' -- field 1, LEN
- n = n + 1; out[n] = wire.encode_len(v[i])
+ n = n + 1; out[n] = wire.encode_varint(#s)
+ n = n + 1; out[n] = s
end
return table.concat(out)
end