Go.set_position behaviour

Calling go.set_position(pos) is times faster than go.set_position(pos, nil) or go.set_position(pos, instance_id), where instance_id is the instance id of the calling script.
What is the reason for this behavior?
And this, by the way, is the main reason that the “update_single” test at https://github.com/britzl/defold-bunnymark is so slow.

p.s. I think this indicates what gameobject lookup by id is super slow & inefficient & unoptimized.

1 Like

A bit more info by @dmitriy from the RU community telegram chat:

In bunny mark test in update_many example on iPhone 5s:
go.set_position(pos) ~60 FPS
go.set_position(pos, nil) ~40 FPS
go.set_position(pos, self.gameobject_id) ~37 FPS

bunny_update.script, line 17:

1023 objects (script instances limit), iPhone 5s

go.set_position(pos) ~60 FPS
or
go.set_position(pos, nil) ~37..44 FPS
or
go.set_position(pos, self.gameobject_id) ~37..44 FPS
or
go.set_position(pos, self.url) ~50..52 FPS <-- !!??

where:
self.gameobject_id = go.get_id()
self.url = msg.url(".")

function init(self)
	self.gameobject = go.get_id()
	self.url = msg.url(".")
	self.velocity = -math.random(100)
	go.set_position(vmath.vector3(math.random(640), math.random(1000, 1100), 0))
end

function update(self, dt)
	self.velocity = self.velocity - 1200 * dt

	local pos = go.get_position()
	pos.y = pos.y + self.velocity * dt
	if pos.y < 60 then
		self.velocity = -self.velocity
		pos.y = 60
	end
	go.set_position(pos) -- < -- change this
end
1 Like

Correct, the go.set_position(pos, nil) was missing a nil check, so it wen’t through the “url” code path. Easy fix (I think @agulev is on that).

Secondly, as you know, we use urls to address game objects and components, and this is true here as well. We convert the input to an url. So, if you already have an url, that code path is faster than not passing a url.

5 Likes

Ok, now I understand.
I think we need to add information to a manual (messaging or performance optimization, not sure) that under the hood we use URL for a game object or a component searching. That means if you send something else (string or hash id) we need to create msg.url(). If you need to send many messages it makes sense to pre-cache those URLs.

5 Likes

I added a section about this: Optimizing a Defold game

3 Likes