[git] Faster serialization

Moderator: Moderator

Post Reply
Message
Author
johnnyzero
Thalore
Posts: 148
Joined: Tue Feb 28, 2012 6:36 am

[git] Faster serialization

#1 Post by johnnyzero »

T-Engine has basic serialization support implemented in game/loader/pre-init.lua. This serialization is fine for what T-Engine needs, except that it is really slow in the current git* code. So, I've implemented a buffered version of serialize.

After loading a fresh session of ToME, the profile thread flushes CSETs. On my system, the current git code takes about 40 seconds to finish flushing csets, with the profile thread eating all the cpu cycles it can. Using my table.serialize implementation, flushing csets is done in about one second. Note that these times include zipping the resulting serialized data and sending the result to te4.org.

Code: Select all

function table.serialize(src, sub, no_G, base)
	local chunks = {}
	if sub then 
		chunks[1] = "{"
	end
	for k, e in pairs(src) do
		local nk = nil
		local nkC = {}
		local tk, te = type(k), type(e)
		if no_G then
			if tk == "table" then
				nkC[#nkC+1] = "["
				nkC[#nkC+1] = table.serialize(k, true)
				nkC[#nkC+1] = "]"
			elseif tk == "string" then
				nkC[#nkC+1] = k
			else
				nkC[#nkC+1] = "["
				nkC[#nkC+1] = tostring(k)
				nkC[#nkC+1] = "]"
			end
		else
			if not sub then 
				nkC[#nkC+1] = (base and tostring(base) or "_G") 
			end
			nkC[#nkC+1] = "["
			if tk == "table" then 
				nkC[#nkC+1] = table.serialize(k, true)
			elseif tk == "string" then
				-- escaped quotes matter
				nkC[#nkC+1] = string.format("%q", k)
			else
				nkC[#nkC+1] = tostring(k)
			end
			nkC[#nkC+1] = "]"
		end

		nk = table.concat(nkC)

		-- These are the types of data we are willing to serialize
		if te == "table" or te == "string" or te == "number" or te == "boolean" then
			chunks[#chunks+1] = nk
			chunks[#chunks+1] = "="
			if te == "table" then
				chunks[#chunks+1] = table.serialize(e, true)
			elseif te == "number" then
				-- float output matters
				chunks[#chunks+1] = string.format("%f", e)
			elseif te == "string" then
				-- escaped quotes matter
				chunks[#chunks+1] = string.format("%q", e)
			else -- te == "boolean"
				chunks[#chunks+1] = tostring(e)
			end
			chunks[#chunks+1] = " "
		end
		
		if sub then 
			chunks[#chunks+1] = ", "
		end
	end
	if sub then 
		chunks[#chunks+1] = "}"
	end
			
	return table.concat(chunks)
end

johnnyzero
Thalore
Posts: 148
Joined: Tue Feb 28, 2012 6:36 am

Re: [git] Faster serialization

#2 Post by johnnyzero »

Update: the patch has been accepted.

Code: Select all

commit 6f43ddef9b0e454c7afc272c20c6227ce7f8f5c8
Author: DarkGod <darkgod@net-core.org>
Date:   Tue Oct 1 18:11:31 2013 +0200

    use faster serialization

cttw
Archmage
Posts: 394
Joined: Sat Oct 22, 2011 10:31 am

Re: [git] Faster serialization

#3 Post by cttw »

Good job!

Karagy
Wayist
Posts: 24
Joined: Tue Dec 13, 2011 2:11 pm

Re: [git] Faster serialization

#4 Post by Karagy »

Draw attention to Serializing table data into a string
It can proceed cycled tables.

Post Reply