X Tutup
Skip to content

Avoid repeated _copy_on_write() calls in Array::resize()#110535

Merged
Repiteo merged 1 commit intogodotengine:masterfrom
aaronp64:array_resize_ptrw
Sep 16, 2025
Merged

Avoid repeated _copy_on_write() calls in Array::resize()#110535
Repiteo merged 1 commit intogodotengine:masterfrom
aaronp64:array_resize_ptrw

Conversation

@aaronp64
Copy link
Contributor

Updated Array::resize() to call ptrw() once before looping to initialize typed array elements, instead of accessing each through .write[].

This makes Array.resize() in gdscript around 2-4x faster for typed arrays, will vary depending on type and size. Compared with gdscript below:

func _ready() -> void:
	run_test("resize_int")
	run_test("resize_str")

func resize_int():
	var a: Array[int] = []
	for i in 10000:
		a.resize(i)
		a.resize(0)
		
func resize_str():
	var a: Array[String] = []
	for i in 10000:
		a.resize(i)
		a.resize(0)

func run_test(test_name: String):
	var start := Time.get_ticks_msec()
	call(test_name)
	var end := Time.get_ticks_msec()
	print("%s: %dms" % [test_name, end - start])

old:

resize_int: 319ms
resize_str: 415ms

new:

resize_int: 72ms
resize_str: 163ms

Updated Array::resize() to call ptrw() once before looping to initialize typed array elements, instead of accessing each through .write[].
@aaronp64 aaronp64 requested a review from a team as a code owner September 15, 2025 18:28
@clayjohn clayjohn added this to the 4.6 milestone Sep 15, 2025
@clayjohn clayjohn requested a review from Ivorforce September 15, 2025 18:59
Copy link
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

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

Looks good to me.
The function isnt optimal even with this change, but this is an obvious improvement. Good find!

@Mickeon Mickeon added the cherrypick:4.5 Considered for cherry-picking into a future 4.5.x release label Sep 15, 2025
Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

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

Tested locally, it works as expected. Code looks good to me.

PC specifications
  • CPU: AMD Ryzen 9 9950X3D
  • GPU: NVIDIA GeForce RTX 5090
  • RAM: 64 GB (2×32 GB DDR5-6000 CL30)
  • SSD: Solidigm P44 Pro 2 TB
  • OS: Linux (Fedora 42)

Using a release export template with LTO.

Before

resize_int: 125ms
resize_str: 191ms

After

resize_int: 35ms (3.57x faster)
resize_str: 112ms (1.71x faster)

@Repiteo Repiteo merged commit a298979 into godotengine:master Sep 16, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Sep 16, 2025

Thanks!

@Repiteo
Copy link
Contributor

Repiteo commented Sep 16, 2025

Cherry-picked to 4.5

@Repiteo Repiteo removed the cherrypick:4.5 Considered for cherry-picking into a future 4.5.x release label Sep 16, 2025
@aaronp64 aaronp64 deleted the array_resize_ptrw branch September 16, 2025 20:38
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.

6 participants

X Tutup