[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Speed optimized base64 encoder for Lua 5.3
- From: Ico Doornekamp <lua@...>
- Date: Tue, 04 Nov 2014 18:50:48 +0100
* On 2014-11-04 18:21:41 +0100, Patrick Donnelly wrote:
> You can avoid allocating 2 copies of s doing:
>
> local function base64(s)
> local byte = string.byte
> return s:gsub("..?.?", function(cs)
> local a, b, c = byte(cs, 1, 3)
> if not b then
> return bs[a>>2] .. bs[(a&3)<<4] .. "=="
> elseif not c then
> return bs[a>>2] .. bs[(a&3)<<4|b>>4] .. bs[(b&15)<<2] .. "="
> else
> return bs[a>>2] .. bs[(a&3)<<4|b>>4] .. bs[(b&15)<<2|c>>6] .. bs[c&63]
> end
> end)
> end
I tried something like this as well, but for some reason the performance
is about 20% worse for my workload (+4MB size input).