micro-optimizations on hotspots
This commit is contained in:
parent
4b5a8b90a6
commit
d93fc42cef
14
encoding.lua
14
encoding.lua
@ -1,6 +1,8 @@
|
||||
local char, byte, floor = string.char, string.byte, math.floor
|
||||
|
||||
-- https://gist.github.com/mebens/938502
|
||||
local function rshift(x, by)
|
||||
return math.floor(x / 2 ^ by)
|
||||
return floor(x / 2 ^ by)
|
||||
end
|
||||
|
||||
-- https://stackoverflow.com/a/32387452
|
||||
@ -12,15 +14,15 @@ local function bitand(a, b)
|
||||
result = result + bitval -- set the current bit
|
||||
end
|
||||
bitval = bitval * 2 -- shift left
|
||||
a = math.floor(a/2) -- shift right
|
||||
b = math.floor(b/2)
|
||||
a = floor(a/2) -- shift right
|
||||
b = floor(b/2)
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function mapsync.encode_uint16(int)
|
||||
local a, b = int % 0x100, int / 0x100
|
||||
return string.char(a, b)
|
||||
return char(a, b)
|
||||
end
|
||||
|
||||
function mapsync.encode_uint32(v)
|
||||
@ -28,11 +30,11 @@ function mapsync.encode_uint32(v)
|
||||
local b2 = bitand( rshift(v, 8), 0xFF )
|
||||
local b3 = bitand( rshift(v, 16), 0xFF )
|
||||
local b4 = bitand( rshift(v, 24), 0xFF )
|
||||
return string.char(b1, b2, b3, b4)
|
||||
return char(b1, b2, b3, b4)
|
||||
end
|
||||
|
||||
function mapsync.decode_uint16(str, ofs)
|
||||
ofs = ofs or 0
|
||||
local a, b = string.byte(str, ofs + 1, ofs + 2)
|
||||
local a, b = byte(str, ofs + 1, ofs + 2)
|
||||
return a + b * 0x100
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user