Optimizations for (complicated) code?

Just lookin for some suggestions if anyone can think of any optimizations

Essentially what this code does is allows your to construct a string bit by bit.

local bytebuilder = {}

local char   = string.char
local byte   = string.byte
local b32ex  = bit32.extract
local split  = string.split
local concat = table.concat
local abs    = math.abs

local ipairs = ipairs
local   next = next

local queue_to_char = {}
local bits_for_num = {}
local char_to_bits = {}

for i = 0, 255 do
	queue_to_char[
		b32ex(i,0) ..
		b32ex(i,1) ..
		b32ex(i,2) ..
		b32ex(i,3) ..
		b32ex(i,4) ..
		b32ex(i,5) ..
		b32ex(i,6) ..
		b32ex(i,7)
	] = string.char(i)
	bits_for_num[i] = {
		b32ex(i,0),
		b32ex(i,1),
		b32ex(i,2),
		b32ex(i,3),
		b32ex(i,4),
		b32ex(i,5),
		b32ex(i,6),
		b32ex(i,7)
	}
	char_to_bits[string.char(i)] = bits_for_num[i]
end

local bit_pows = {0}

for i = 0, 31 do
	bit_pows[33-i] = 2^(i)
end

bytebuilder.construct = function()
	local data = ""
	local queued = {0,0,0,0,0,0,0,0} --current bit sequence
	local bitpos = 1
	local pos = 0
	
	local constructer = {}
	function constructer:AppendBit(Bit)
		queued[bitpos] = Bit
		bitpos = bitpos + 1
		if bitpos == 9 then
			bitpos = 1
			data = data .. queue_to_char[concat(queued)]
			--no need to clear queue, data will just be overwritten
		end
	end
	
	function constructer:Bool(Bool)
		self:AppendBit(Bool and 1 or 0)
	end
	
	function constructer:Number(Number, Bits, Signed)
		pos = abs(Number)
		for i = Bits-1, 0, -1 do --big endian
			self:AppendBit(b32ex(pos, i))
		end
		
		if Signed then --add signed bit list, will be faster to read
			self:Bool(Number < 0)
		end
	end
	
	function constructer:String(String, Length)
		for i = 1, Length or #String do
			for _, b in next, bits_for_num[byte(String, i) or 0] do
				self:AppendBit(b)
			end
		end
	end
	
	function constructer:Character(Character)
		for _, b in next, bits_for_num[byte(Character) or 0] do
			self:AppendBit(b)
		end
	end
	
	function constructer:Resault()
		while bitpos ~= 1 do --append bits untill the queue is cleared
			self:AppendBit(0)
		end
		return data
	end
	
	constructer.Export = constructer.Resault
	
	return constructer
end

bytebuilder.deconstruct = function(Data)
	local deconstructer = {}
	Data = split(Data, "")
	
	local bytes = {}
	for cp, c in next, Data do --next is actually faster then ipairs
		for bp, b in next, char_to_bits[c] do
			bytes[(cp-1)*8+bp] = b
		end
	end
	
	local position = 0
	local byte_iterator = function()
		position = position + 1
		return bytes[position]
	end
	
	function deconstructer:Bool()
		return byte_iterator() == 1
	end
	
	function deconstructer:Number(Bits, Signed)
		local Resault = 0
		
		for _, m in next, bit_pows, 33 - Bits do
			Resault = Resault + byte_iterator() * m
		end
		
		if Signed and self:Bool() then
			Resault = -Resault
		end
		
		return Resault
	end
	
	function deconstructer:String(Length)
		local S = {}
		
		for i = 1, Length do
			S[i] = char( --can't use :Number , this is in small endian
					byte_iterator()      +
					byte_iterator() * 2  +
					byte_iterator() * 4  +
					byte_iterator() * 8  +
					byte_iterator() * 16 +
					byte_iterator() * 32 +
					byte_iterator() * 64 +
					byte_iterator() * 128
			)
		end
		
		return concat(S)
	end
	
	function deconstructer:Character()
		return char( --can't use :Number , this is in small endian
			byte_iterator()      +
			byte_iterator() * 2  +
			byte_iterator() * 4  +
			byte_iterator() * 8  +
			byte_iterator() * 16 +
			byte_iterator() * 32 +
			byte_iterator() * 64 +
			byte_iterator() * 128
		)
	end
	
	return deconstructer
end

return bytebuilder

(any suggestion is appreciated)

1 Like