These variables in my loops are causing massive framerate drop. How can I fix this?

So I have been working on a custom 3D rendering engine. (which is very cool and all, but it seems to run horribly due to some variables I have in the code. I have no idea why this is happening. and I have done tests and I can confirm that it’s the two variables in the code below.

Some help would be greatly appreciated as always!

local function RenderObjects()
	-- Update the picture

	for ObjectIndex, Object in pairs(Objects) do
		local TriangleFaces = Object.TriangleFaces
		local Vertices = Object.Vertices
		
		for FaceIndex, Triangle in pairs(TriangleFaces) do
			-- These are the variables that are causing lag in the code. If i comment these out the game will run 60 FPS
			local TriangleImg1 = Canvas["Triangle" .. ObjectIndex .. "|" .. FaceIndex .. "|1"] -- This is an ImageLabel in a frame in a GUI
			local TriangleImg2 = Canvas["Triangle" .. ObjectIndex .. "|" .. FaceIndex .. "|2"]	-- This is an ImageLabel in a frame in a GUI
			
			--[[
				Normally there would be over 100 lines of code here, but none of it seemed to affect the frame rate at all when I commented out the above variables.
			]]
		end
	end
end

How many items are in Canvas? Is it just the search time that causes the slow down? Might make sense to create a separate table of tables to store canvas items for fast lookup. So you can do CanvasLookup[ObjectIndex][FaceIndex] using numbers instead of string compares.

1 Like

there’s over 2000 gui objects in the canvas. But I have done some more tests, and i have discovered that that isn’t the issue. The issue is the string combinations. Example:

This causes no lag: (But of course I cant use this)

local TriangleImg1 =  Canvas["Triangle1|1|1"]

But this does:

local TriangleImg1 =  Canvas["Triangle" .. ObjectIndex .. "|" .. FaceIndex .. "|1"]

So same solution would apply. Make a table of tables to avoid the string concatenation.

1 Like

Wow. That has fixed all my issues. My framerate has gone from 13 FPS to 60 FPS. Thank you very much!

1 Like