diff --git a/runtime/pb/wire.lua b/runtime/pb/wire.lua index 6262045a766e3063d3e08a1b790d7b20326b4fc3..8e21add2974bcf573c114e2a3e54985a02ab8890 100644 --- a/runtime/pb/wire.lua +++ b/runtime/pb/wire.lua @@ -45,7 +45,15 @@ -- --------------------------------------------------------------------------- -- encode_varint(n) -> string -- Accepts uint64_t/int64_t cdata, Lua number, or boolean. +-- +-- Fast path: small non-negative Lua numbers (0..127) become a single +-- string.char(n) call with no cdata allocation, no `out` table, no +-- table.concat. Covers most length prefixes for short strings, many +-- enum ordinals, and most small int values in typical RPC payloads. local function encode_varint(n) + if type(n) == 'number' and n >= 0 and n < 0x80 then + return string.char(n) + end n = to_uint64(n) local out = {} local i = 1