Boost Godot Project Performance With This Simple Trick
Vložit
- čas přidán 22. 08. 2024
- Join the community discord! / discord
Play my games on itch: breckheck.itch...
Here's a quick tip in the Godot universe I think the world should know about. GDScript static typing performance gains. Enjoy!
Even if your game runs fine static typing helps so much when debugging issues and can even prevent you from creating bugs. There's really no reason not to use it even in small projects.
I didn't even know there were performance benefits to type hinting in GDScript... I use it because it's so much easier to write code with intellisense that knows the types of variables and outputs and it highlights the irritating type mismatch bugs before compilation even.
didn't realize functions could be statically typed for a performance boost too... Very useful.
I'm not 100% sure if typing functions gives the same performance increase as for variables. My understanding is that statically typing all the things lets the interpreter take shortcuts when running the code, increasing the performance. Thanks for watching!
One thing I've wondered is if typing void or Variant improves performance. Both are implicit. Such as no return = void. And Explicit and implicit Variant has the same results. So I've always wondered if typing either leads to better performance or just unnecessary and more verbose code (bad).
I did statically type all the functions in my project where I could see an obvious return type - mostly voids, Arrays, bools and ints, and I have already noticed reduced loading times, and was able to slightly increase the frame rate on my most laggy level... There are still some hiccups here and there I need to work on - most likely due to textures... But it does seem to run more smoothly overall. I'll have to look into Variant because one of my most used functions returns different objects or null. Thanks.
@@ShiloBuff it's not necessarily bad even if it's not functionally necessary for the interpreter. Typing a method as void will indicate to you or another programmer that the method shouldn't return anything -- and also if you make the mistake of returning something it will allow the environment to complain at you. To answer your implicit question though, typically void typing won't see any performance gains.
Why typing gives you performance gains in the first place is because the more refined your type the more specific the handling can be. If I have an untyped value it could be literally anything at all so the receiving side has to account for "What if it's a list or a string or a hashtable or a ..." and has to have code to handle all of those circumstances to prevent crashing. If instead you type a value as a "Number" now it only has to think about "Is it an int? Is it a float? Is it signed or unsigned? How big is it?.." and the more refined you get your typing, say to "uint8" now the receiver is certain, the value I'm working with is going to be an unsigned 8 bit wide integer, all the code that would manage receiving arrays and strings and floats doesn't need to exist anymore. When the return type is void, not only will you be returning nothing... there will be no receiver to even make an optimization decision about it. -- That said it's still worth it because it adds human understanding and will be optimized out at runtime for free.
Search this and Godot docs says "Also, typed GDScrypt improves performance by using optimized opcodes when operand/argument types are known at compile time.", which means that this should indeed work, given that documentation is accurate.
Also it continues in the same paragraph "More GDScripts optimizations are planned in the future, such as JIT/AOT compilation.".
Also apparently there are "Type Hints".
You access them through "Editor" -> "Text Editor" -> "Completion" -> "Add Type Hints".
Bro WHAT I thought programmers were just being fancy and organized by doing that, nobody told me it boosted performance by 60% lmao
Thank you!
Awesome vid! I'm a C dev so I just prefer this way lol, but I can confirm the speed and bug improvements after doing this
You can also check the "Add Type Hints" box under editor settings to have autocompletion add the static types for you in some cases. Instantly subscribed after seeing the debug option. Really great video thanks.
I've been hammering my head against this wall trying to learn programming as a hobby for 5-6 years now. I've run across this concept of static vs dynamic typing so often and it always confused me so I just thought "I'll worry about it tomorrow I guess." Somehow this video just really simplified it and something clicked for me. Thank you!
Thanks! I didn't realize you could make it give a warning in the project settings. 👍👍
Thank you for showing us the real performance gain in numbers. Other videos on the subject are missing that
There's one more important benefit, namely you will get autofill depending of type of variable/packed_scenes/texture/resource/nodes/etc. It really helps when you are using resources and classes and passing them from editor
- i think its on by default but If you dont see them
You access them through "Editor" -> "Text Editor" -> "Completion" -> "Add Type Hints".
Omg my prediction was spot on. Static type EVERYTHING!
EDIT: Looks like you struck gold with this topic. If you ever feel like talking about the debugger in more detail (for dummies like me who are 99% reliant on prints like a caveperson) I'll be there to watch it. 👍
Funnily enough, I already started using static types for my exported variables to get the proper inputs in the inspector, but hearing they also help with performance is good to know and I'll really have to get used to using return types again.
Thanks!
Oh and the := operator is great for primitive types. Yes, I am *that* lazy of a typer. :D
This is something I had been doing a bit, but since the last couple of months I've been fully static typing variables and functions.
This simple example should be in the godot documentation with an explanation - you can use the Variant type, but with a 30% performance loss.
You can avoid explicit types at assignment, and still get static typing, by using the shortcut:
var total := 100
Instead of
var total : int = 100
A little tip that reduces boiler plate when the types are already easily readable in code. ( This pattern is also in other popular languages: Kotlin, C++ (auto), etc)
Still don't understand the point of dynamic type. Just to save seconds of typing time and waste hours or days to hunt down bugs relating to mismatch type? That sounds like an unworthy tradeoff. The fact that most dynamic typing languages had to introduce static typing at some point already proved my point.
It's sad to say that most dynamic programming is done because of the inability to teach typing, or learning typing. So our answer was to automate it.
In other words, it's lazy way to solve a trivial problem.
Hey that's super cool, I thought these are just type hints like in python. I guess I didn't bother reading the full manual and didn't realize these are proper static type declarations. Thanks!
Wow that’s crazy ! Now I finally understand why some coders like to declare variable types, I’ll be doing it from now on !
Let's hope GDScript gets array typing soon.
You actually can type arrays in GDScript now.
var my_array: Array[int] = [1,2,3,4]
Although, it doesn't have the best support and can be a little finicky. Hopefully that will be addressed in the near future, along with nested array and Dictionary types like you see in TS
worth the watch for the knowledge that I can set untyped declarations to throw errors alone, knowing it improves performance is the icing on the cake 😂 great video, thank you for the info.
3:09
If you press CTRL+R, you will enable "replace".
You can simply replace all "=" with ":=", and anything after a paranthesis "()" with "() -> void" to fix all errors.
I watched a couple of GDQuest videos, and he does that, but I never heard explanation. Thank you!
I wondered what are the implications of using the ':'. Now I know. Thank You!
Holy shit! Thanks man this has drastically improved my performance
Coming from Unity I already did this as standard practice, good to know this actually helps performance lol!
thanks for the tip. i knew of static typing i just didn't know why i should use it. i have a small game underway but luckily i'm not very deep yet so i can change the code to static typing :)
Another advantage of static typing is intellisense support.
Thank you for this tip!! I heard about it before, but didn't know it made such a difference :D
1:20 the first thing I noticed was that the movement was done in _process and not _physics_process
It's like remembering to put % after everything in QBASIC that should be an int.
I'd say a good rule of thumb is to almost always use static types unless the function will work with generic types or you're writing an addon (types are erased (duplicated but not equal) when passed to those it seems).
Thanks for this, but you forgot something useful!
Editor Settings -> Text Editor -> Completion -> Add Type Hints.
With this setting, selecting functions via auto complete that can be overwritten (like _ready() etc.), they automatically get their type hint.
personally I don't like the untyped declaration as an error since it doesn't work nicely with some things like Array.map/filter/reduce, or dictionaries. you can also just look at the line number, if it's green that means that line is completely typesafe, otherwise it's not
Great Video!
The issue is when you use arrays. And i dont mean simple ones like an array of ints. I mean stuff like an array of array when each element is an array where the first element is an int, the second a node2d, the third a string.
I do miss the days when you had no "variable" types and had to follow strict typing.
Coming from Python Godot's type hints felt very natural. The colon equals syntax is interesting, but I'm not convinced of its usefulness when assigning to a $ operation or a function call. For assignments to literal objects it's more worthwhile as it's as obvious there to the reader as the engine.
Thanks for the video 🤙🤙 I will start with this even for a minor performance boost
I am a C++ dev who uses godot. Since I learned c++ before gdscript, I can't stop declaring datatypes😅
I'm still in college, but I can feel you being a Java programmer 😅
I wouldn't have thought of this - python has a very similar syntax called 'type annotation' but it doesn't seem to do much except for add additional checking. interesting!
I came from Java(my first language), it's the only one I know before trying and started to learn Godot and GDScript a week ago.
Coming from a C based language, I'm too used in declaring the data types, so I started doing that right away after finding out I can do it 😅
I'm learning Python alongside GDScript right now… I hate Py.
Very good tip, I never thought about it, oops a lot of work is waiting for me, thank you :)
Thank you so much !
I always use static typing (Haskell refugee), but it never appeared to me how much of a performance increase it is for Godot.
Huh, I thought my code was running faster because coding skills were improving. Might just have been because I swapped fully to static typing as the other languages I use also use it (TypeScript, Swift).
any hints for statically typing a dictionary? in c# you would Dictionary but I can't figure this out for gdscript
there is no static typing for dictionaries in Godot currently
oh, well, I already prefered static typing everything so *this video was in fact clickbait technically for me*
(this is just because I chose to follow a tutorial that showed me how to static type before and if I can static type, I will)
Could you share the link or title of the tutorial
Nice video! But how are you calculating your speedup at 2:33? 1 - static_time / dynamic_time is not the speedup, idk know what that is lol. I guess the phrase A is x% faster than y is somewhat ambiguous. I guess a reasonable way to interpret that is to look at the amount of time it takes less than the base case (as a percentage of the base case). So then 40% faster than 100 would be 100 - 40%*100 = 60. Defined like that you would calculate it like this: (static_time - dynamic_time )/static_time = 1 - dynamic_time /static_time.
So I guess your version is like how much more time does dynamic take than static as a percentage of static? I don't even know at this point lol, my head hurts.
Also, generally speed tests in debug mode can behave pretty different than in release mode, so testing is release mode would be better. Idk how much that applies to godot, but its probably better to test for the case of a released game than a debug build.
But as I said, nice video :)
Oh shoot! Thanks for letting me know I got the formula wrong. I must've rushed through that part of the code.
The new formula I came up with for percent faster is:
(dynamic_time / static_time - 1) * 100
I reran the performance tests and here are the new results, which have numbers that make more sense I think:
dynamic add nums: 0.66875791549683
static add nums: 0.44424700737
static is 51% faster
dynamic mult nums: 0.63372111320496
static mult nums: 0.44116997718811
static is 44% faster
dynamic vector2 dist: 1.77509212493896
static vector2 dist: 1.18353796005249
static is 50% faster
dynamic vector2 add: 0.85133600234985
static vector2 add: 0.35749793052673
static is 138% faster
And yeah you're right about the debug vs release build performance. I just focused on the debug build for this vid, but I found some articles that cover the numbers for that if you are interested.
I prefer not to use := because if I don't understand the type I'll probably create bugs later. Coming from making typescript servers, love static types for reducing coding errors.
yes, its hard to go back to JS and dynamic types after trying out typescript!
I don’t like that it can’t auto-infer type of `var a = 5` it’s obviously capable when you use `:=`
I enabled all static-type-related features as compiler errors. GDScript is cool, but I HATE Type inferencing. That being said, I picked up C# and am learning Rust to add to the arsenal. I'm pretty sure there's also a setting to autocomplete the static types for variants, static return types for methods, and when droppping nodes from the inspector.
Editor Settings > Text Editor > Completion > Type Hints
Is there an option to make them be treated as compiler error
@@alexstone691 In the options shown in this video, you have the option to disable them, treat them as warnings, or treat them as an error. It's the drop down menu to the right of each of the settings.
@@robertwaremusic thank you!
nice vid bud, audio quality needs a bit of work: if you toss a blanket over your head and mic, it can cut out the hovercraft in the background
Lol, yeah my PC fans are kinda loud. Thanks for the feedback!
Liked and subscribed! Can you do one about finite state machine setup in godot4?
That's a good one. I'll add that to my list. Thanks for watching!
Nice work, Ill try it
C# isn't hard at all, though one problem I have with Godot is its poor support for it (especially on Linux). Its still interesting that you can get such a boost by just by properly defining your variable types, static typing ftw (I never liked "var" type declarations in any language).
How else do you declare variables?
Worst case of declaring types is probably python imo
Yeah I think C++ is the more difficult one for sure. When I first got into godot I did C# for the extra performance, though I was scared away by the lack of support (I'm on linux too).
this helped, thanks
Thank a lot.. very informative..
As a self taught absolute newb of a coder, could you please explain how when I would/wouldn’t set a function to void? That part has always tripped me up. Declaring the types seems super straightforward though, so thanks for the great video!
void is the function return type when you dont return any value(s) in the function's code. The _process() and _ready() node functions are some examples of this. If you need a value from that function (let's say a string) somewhere else you can set the return type as String. This is only needed to satisfy static typing with the setting.
@@DeepDiveDevelop So… if you don’t return anything at all, use Void. That makes sense… but does that only mean when you literally have “return X” in your code? Or are there other examples when you need to return something? Thanks for the reply!
Woa, thanks!
bro inherits from characterbody2d and uses _process function instead _physics_process where all physics objects should be, but shows amazing tip LMAO
I would suggest you should have also gone over the differences from the explicit and inferred type declarations instead of just randomly interspersing their use in the video without explanation.
Also making the _process delta typed could have used a bit of a highlight. Using const will statically type a variable too.
Nice overview though.
Thanks for the feedback!
subbed and liked, thanks
Thanks for the sub!
I have quite a lot nodes(objects) per scene on a 2d game, this chance would benefit mainly the process loop(s) most? What is actually under the hood when comparind difference on dynamic / non dynamic vars?
does giving static return types to functions also help?
Interesting
I don't use godot, i clicked for the cat
But dictionaries are still untyped 😥
It will be a glorious day when they add typed dictionaries and generics
What do I do in something like my case... I've custom built Godot 3.1 with export templates for windows XP, there isnt untyped warning in the settings, plus they're not a radio button, but a boolean, so if I have to take the logic from the newer version yall are using, it'd take some time.. Is it really worth it? I just want to make the best out of my retro machine games...
I believe the performance increases are only available after godot 4.0. I can't speak too much about the prior settings before Godot 4, but still plenty of reasons to static type in 3.1!
godotengine.org/article/gdscript-progress-report-typed-instructions/
@@DeepDiveDevelop okay, thank you, that's really appreciated! 🙏🙏🙏
I always used static types since I was already used to it for using C++, honestly I never understood what real uses dynamic typing could have
Wait, you Neanderthals seriously program without declaring variable types?
How would that make things faster? (Not saying it doesn’t I just don't understand how that would effect it)
My guess is it's trying to figure out the data type first everytime they are used
Which what makes it slow.
So if the data type is already known, they don't need to figure it out anymore.
C# will plunder your performances. I went there and thrust me, that cake is a lie.
Garbage in garbage out. The performance gains have already been tested. You need to refactor your code structure
Someone know if there is a way to set the default script of godot with ready and process func with static declaration for all the projects ?
Looks like this is possible, Check out docs.godotengine.org/en/stable/tutorials/scripting/creating_script_templates.html
next time just state what you're doing up-front, then elaborate on it, and i might actually believe you're a genuine person