Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom objects are slower to instantiate in Godot 4.0 GDScript compared to Godot 3.5.1 #73936

Closed
MichaelReel opened this issue Feb 25, 2023 · 1 comment · Fixed by #81037
Closed

Comments

@MichaelReel
Copy link

MichaelReel commented Feb 25, 2023

Godot version

4.0 RC 3 4.0 RC 5

System information

Ubuntu 22.04.2 LTS, Intel® Core™ i7-8700K CPU @ 3.70GHz × 12, AMD® Radeon rx 6700 xt

Issue description

Apologies if this is a duplicate issue or a known feature of the new engine, I didn't discover anything with the same description in the bug tracker or the noted in the documentation.

I noticed that some code I ported from Godot 3.5 was significantly slower in Godot 4.0 RC 3. I whittled down the issue to the instantiation of a large number of custom defined objects that I was using.

Instantiating 10000 custom objects in Godot 3.5.1 might take around 15 milliseconds. Doing the same thing in Godot 4.0 RC 3 takes over a second on my hardware. The difference seems significant enough to me to wonder if this something that I will need to work around in Godot 4, or if it will be reverted in future.

Steps to reproduce

Writing a script in godot 3.5 that instantiates 10000 objects, custom objects (and for comparison custom resources), out puts some reasonable times:

Godot 3.5.1 Script:

class CustomObject extends Object:
	pass

class CustomResource extends Resource:
	pass

func _ready():
	var test_func_names = [
		"test_10000_objects",
		"test_10000_custom_objects",
		"test_10000_custom_resources",
	]
	for test_func_name in test_func_names:
		var test_func = funcref(self, test_func_name)
		var start_time: int = Time.get_ticks_usec()
		test_func.call_func()
		print("%s ran in %d usecs" % [test_func_name, Time.get_ticks_usec() - start_time])

func test_10000_objects():
	var test_array: Array = []
	for _i in range(10000):
		test_array.append(Object())

func test_10000_custom_objects():
	var test_array: Array = []
	for _i in range(10000):
		test_array.append(CustomObject.new())

func test_10000_custom_resources():
	var test_array: Array = []
	for _i in range(10000):
		test_array.append(CustomResource.new())

Godot 3.5.1 Output:

Godot Engine v3.5.1.stable.official.6fed1ffa3 - https://godotengine.org
OpenGL ES 3.0 Renderer: AMD Radeon RX 6700 XT (navi22, LLVM 15.0.6, DRM 3.47, 5.19.0-32-generic)
Async. shader compilation: OFF
 
test_10000_objects ran in 784 usecs
test_10000_custom_objects ran in 15344 usecs
test_10000_custom_resources ran in 25674 usecs

Running a similar script in Godot 4.0:

Godot 4.0 Script:

class CustomObject extends Object:
	pass

class CustomResource extends Resource:
	pass

func _ready():
	for test_func in [
		test_10000_objects,
		test_10000_custom_objects,
		test_10000_custom_resources,
	]:
		var start_time: int = Time.get_ticks_usec()
		test_func.call()
		print("%s ran in %d usecs" % [test_func.get_method(), Time.get_ticks_usec() - start_time])

func test_10000_objects():
	var test_array: Array = []
	for _i in range(10000):
		test_array.append(Object())

func test_10000_custom_objects():
	var test_array: Array = []
	for _i in range(10000):
		test_array.append(CustomObject.new())

func test_10000_custom_resources():
	var test_array: Array = []
	for _i in range(10000):
		test_array.append(CustomResource.new())

Godot 4.0 RC 5 Output:

Godot Engine v4.0.rc5.official.6296b4600 - https://godotengine.org
OpenGL API 4.6 (Core Profile) Mesa 22.2.5 - Compatibility - Using Device: AMD - AMD Radeon RX 6700 XT (navi22, LLVM 15.0.6, DRM 3.47, 5.19.0-32-generic)
 
test_10000_objects ran in 1435 usecs
test_10000_custom_objects ran in 1147367 usecs
test_10000_custom_resources ran in 12831 usecs

Minimal reproduction project

It's probably unnecessary, but I've included small projects that reproduce the issue with both Godot 4.0 RC 3 Forward+ and Compatibility set:

And I've also included a version of (more or less) the same code in Godot 3.5 as a comparison:

@MichaelReel
Copy link
Author

I initially submitted this against RC3, but have just confirmed that I get the same results with RC5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants