Help with mod optimization

Place to get help with not working mods / modding interface.
Bilka
Factorio Staff
Factorio Staff
Posts: 3684
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Help with mod optimization

Post by Bilka »

My mod currently takes 0.030 ms of calculation time only to check if some global/ locals exist. Is there a way to make this process less performance intensive?

Code: Select all

script.on_event(defines.events.on_tick, function(event)
	if event.tick % 5 == 0 then
		local p1 = global.portal1.entity 
		local p2 = global.portal2.entity 
		if p1 and p2 then
			if p1.valid and p2.valid then
				for index, player in pairs(game.players) do
					if player.connected and player.character and player.vehicle == nil then
						try_teleport_1(player)
						try_teleport_2(player)
					end
				end
			end 
		end
	end
end)
Even if global.portal1.entity/global.portal2.entity don't exist the process takes about 0.029 ms. If they exist but aren't valid, the process takes about 0.032 ms, so it feels weird to me that cheking if they exist takes much longer than checking if they are valid. Doing this less often also doesn't increase performancce, the number stays around 0.028-0.030 ms.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Rseding91
Factorio Staff
Factorio Staff
Posts: 16452
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Help with mod optimization

Post by Rseding91 »

0.030 ms is virtually nothing. Don't worry about it.
If you want to get ahold of me I'm almost always on Discord.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5432
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Help with mod optimization

Post by Klonan »

Bilka wrote:My mod currently takes 0.030 ms of calculation time only to check if some global/ locals exist. Is there a way to make this process less performance intensive?

Even if global.portal1.entity/global.portal2.entity don't exist the process takes about 0.029 ms. If they exist but aren't valid, the process takes about 0.032 ms, so it feels weird to me that cheking if they exist takes much longer than checking if they are valid. Doing this less often also doesn't increase performancce, the number stays around 0.028-0.030 ms.
While it probably won't make a huge difference,
This might be a little faster:

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  if event.tick % 5 ~= 0 then return end
  local p1 = global.portal1.entity 
  local p2 = global.portal2.entity 
  if (not p1) or (not p2) then return end
  if (not p1.valid) or (not p2.valid) then return end
  for index, player in pairs(game.connected_players) do
    if player.character and not player.vehicle then
    try_teleport_1(player)
    try_teleport_2(player)
    end
  end
end)
Bilka
Factorio Staff
Factorio Staff
Posts: 3684
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Help with mod optimization

Post by Bilka »

Thanks Klonan! While that code doesn't make a difference when global.portal1.entity/global.portal2.entity don't exist, it makes a difference when they do exist. It's also nice to know that I don't have to worry too much about the update time, otherwise I might have tinkered for ages.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Post Reply

Return to “Modding help”