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

Improve ClassDB::_is_parent_class performance #95292

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

aaronp64
Copy link
Contributor

@aaronp64 aaronp64 commented Aug 8, 2024

Change ClassDB::_is_parent_class to use ClassInfo::inherits_ptr, instead of looking up each inherited class name.

This speeds up Array type checking - adding to a typed Array can be around 2x faster, depending on how many inherited classes are between the object being added and the Array's type. Also speeds up casting/type checking in gdscript with as/is for native types.

Comparison gdscript below:

func _ready() -> void:
	test_untyped_array()
	test_node_button_array()
	test_node_node3d_array()
	test_cast()

func test_untyped_array():
	var a : Array = []
	var start_time = Time.get_ticks_msec()
	for i in 1000000:
		a.push_back(i)
	var end_time = Time.get_ticks_msec()
	print("test_untyped_array: %dms" % (end_time - start_time))

func test_node_button_array():
	var a : Array[Node] = []
	var b := Button.new()
	var start_time = Time.get_ticks_msec()
	for i in 1000000:
		a.push_back(b)
	var end_time = Time.get_ticks_msec()
	print("test_node_button_array: %dms" % (end_time - start_time))

func test_node_node3d_array():
	var a : Array[Node] = []
	var n := Node3D.new()
	var start_time = Time.get_ticks_msec()
	for i in 1000000:
		a.push_back(n)
	var end_time = Time.get_ticks_msec()
	print("test_node_node3d_array: %dms" % (end_time - start_time))
	
func test_cast():
	var event : InputEvent = InputEventMouseMotion.new()
	var start_time = Time.get_ticks_msec()
	for i in 1000000:
		event as InputEventKey
	var end_time = Time.get_ticks_msec()
	print("test_cast: %dms" % (end_time - start_time))

Old

test_untyped_array: 89ms
test_node_button_array: 402ms
test_node_node3d_array: 242ms
test_cast: 508ms

New

test_untyped_array: 88ms
test_node_button_array: 155ms
test_node_node3d_array: 155ms
test_cast: 88ms

Change ClassDB::_is_parent_class to use ClassInfo::inherits_ptr, instead of looking up each inherited class name.
Copy link
Contributor

@dsnopek dsnopek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks good to me :-)

@akien-mga akien-mga modified the milestones: 4.x, 4.4 Sep 12, 2024
@akien-mga akien-mga merged commit cd9da33 into godotengine:master Sep 12, 2024
18 checks passed
@akien-mga
Copy link
Member

Thanks!

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

Successfully merging this pull request may close these issues.

4 participants