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

Apply basic optimizations in GDScript compiler #7823

Closed
YYF233333 opened this issue Sep 23, 2023 · 5 comments
Closed

Apply basic optimizations in GDScript compiler #7823

YYF233333 opened this issue Sep 23, 2023 · 5 comments
Labels

Comments

@YYF233333
Copy link

Describe the project you are working on

Currently looking around in godot source code and trying to contribute.

Describe the problem or limitation you are having in your project

Some quick test shows that GDScript compiler seems to lack the ability to do some simple optimizations.

optimizations
constant propagation, constant folding yes
copy propagation no
common subexpression elimination no
dead code elimination no
loop-invariant code motion no

Test code snippets attached below.

I find lots of PR and issues trying improve GDScript VM performance, however I don't see any about the compiler (Let me know if there is some).

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Implement these optimizations in GDScript compiler. They can help to improve overall performance of user scripts.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

These are standard compiler optimizations, you can find detailed description on wiki.
Constant Folding
Common subexpression elimination
Loop-invariant code motion

If this enhancement will not be used often, can it be worked around with a few lines of script?

While in simple cases, one paradigm alone can be overcome by manually optimize the source script, complex case or multiple paradigms combining together increase its difficulty. Also sometimes applying these paradigms may produce ugly or hard to maintain code. So developers may refuse to do these optimizations because this waste too much time with relative small peformance gain or they prefer cleaner code. Having these work done in compiler benifits all GDScript users.

Is there a reason why this should be core and not an add-on in the asset library?

This proposal is about GDScript compiler, which is part of Godot.

@AThousandShips
Copy link
Member

AThousandShips commented Sep 23, 2023

Thank you for your proposal, closing as a duplicate of:

This tracks the wider context of performance improvements of GDScript

@AThousandShips AThousandShips marked this as a duplicate of #6031 Sep 23, 2023
@YYF233333
Copy link
Author

Thank you for your proposal, closing as a duplicate of:

This tracks the wider context of performance improvements of GDScript

This is about compiler part, while Improve the performance of the GDScript VM #6031 mainly focus on the VM performance, I don't find any discussion about compile-time optimization in that proposal.

@AThousandShips
Copy link
Member

It does talk about the wider topic and not just the VM (I have discussed in it myself and those topics were brought up)

@YYF233333
Copy link
Author

It does talk about the wider topic and not just the VM (I have discussed in it myself and those topics were brought up)

Well I use Ctrl+F and miss your comment, sorry for that. But the majority of that proposal is still about compile to C, LLVM, wasm, ... and seems nobody reply to your comment, I wonder if they really want to talk about this topic after 100 floor of transpiling stuff.

I open this proposal because I want to take a try in contributing and want some advise, and this is definitely a small topic, not about a large gdscript rework.

Anyway I will copy above to that proposal since it is not totally unrelated.

@YYF233333
Copy link
Author

Test code

  • dead code elimination
func _process(delta: float) -> void:
	for i in range(1_000_000):
		pass

frame_time: 12ms

  • copy propgation
func _process(delta: float) -> void:
	var a := 0
	for i in range(100_000):
		var b := a

frame time: 1.6ms

func _process(delta: float) -> void:
	var a := 0
	for i in range(100_000):
		var b := a
		var c := b
		var d := c

frame time: 4.9ms

  • common subexpression elimination
func _process(delta: float) -> void:
	var a := 0
	var b := 1
	for i in range(100_000):
		var c := (a + b)

frame time: 2.2ms

func _process(delta: float) -> void:
	var a := 0
	var b := 1
	for i in range(100_000):
		var c := (a + b) + (a + b) + (a + b) + (a + b)

frame time: 6.9ms

  • loop-invariant code motion
func _process(delta: float) -> void:
	const a := 0
	for i in range(100_000):
		var b := a

frame time: 1.7ms

func _process(delta: float) -> void:
	for i in range(100_000):
		const a := 0
		var b := a

frame time: 2.1ms

I test these snippets on my dev build (production=yes debug_symbols=yes) so it is generally slower than official build, but the trends are the same. Since this is only a proof of concept test I think that's harmless.

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

No branches or pull requests

2 participants