mirror of
				https://github.com/luanti-org/luanti.git
				synced 2025-11-04 01:05:48 +01:00 
			
		
		
		
	Optimize appending to tables in core.serialize and dump
				
					
				
			This commit is contained in:
		
				
					committed by
					
						
						Lars Müller
					
				
			
			
				
	
			
			
			
						parent
						
							747857bffa
						
					
				
				
					commit
					34e73da424
				
			@@ -122,8 +122,16 @@ function dump(value, indent)
 | 
			
		||||
	local newline = indent == "" and "" or "\n"
 | 
			
		||||
 | 
			
		||||
	local rope = {}
 | 
			
		||||
	local function write(str)
 | 
			
		||||
		table.insert(rope, str)
 | 
			
		||||
	local write
 | 
			
		||||
	do
 | 
			
		||||
		-- Keeping the length of the table as a local variable is *much*
 | 
			
		||||
		-- faster than invoking the length operator.
 | 
			
		||||
		-- See https://gitspartv.github.io/LuaJIT-Benchmarks/#test12.
 | 
			
		||||
		local i = 0
 | 
			
		||||
		function write(str)
 | 
			
		||||
			i = i + 1
 | 
			
		||||
			rope[i] = str
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	local n_refs = {}
 | 
			
		||||
 
 | 
			
		||||
@@ -218,9 +218,13 @@ function core.serialize(value)
 | 
			
		||||
		core.log("deprecated", "Support for dumping functions in `core.serialize` is deprecated.")
 | 
			
		||||
	end
 | 
			
		||||
	local rope = {}
 | 
			
		||||
	-- Keeping the length of the table as a local variable is *much*
 | 
			
		||||
	-- faster than invoking the length operator.
 | 
			
		||||
	-- See https://gitspartv.github.io/LuaJIT-Benchmarks/#test12.
 | 
			
		||||
	local i = 0
 | 
			
		||||
	serialize(value, function(text)
 | 
			
		||||
		 -- Faster than table.insert(rope, text) on PUC Lua 5.1
 | 
			
		||||
		rope[#rope + 1] = text
 | 
			
		||||
		i = i + 1
 | 
			
		||||
		rope[i] = text
 | 
			
		||||
	end)
 | 
			
		||||
	return table_concat(rope)
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user