Комментарии:
Wow, that was really interesting, I thought the impact in performance would ve minimal.
Thanks for this video! ❤
Hey swydev, I was on a business trip and couldn't verify the impact instantly, that's the reason I didn't reply yet. Was going tonreply and saw this video, hehe😅
No need to blur the name, but thanks that you did without showing clear name without asking!
I also wanted to mention that the line counters indicate if all values are statically known for godot or not. Green line indicator means better performance than grey. This indicator was introduced witj 4.x i think.
WOW actual stats!
But, now my brain wonders about strings, and functions and function returns.... And if Godot does the Java trick I've heard about called inline functions? (As in the performance drop for splitting your functions toooo much) 😅 Yea it doesn't matter for most code. Cool preso!
Something you didn't mention in your video, but i think is equally just as important, is the developer experience when working with statically typed variables. Using statically typed variables allows Godots autocomple to work WAAAAY better, helping the developer write better, less buggy code.
For example, if you create your own class with its own functions and properties, the autocomplete will not show you those things sometimes unless you use static typing (in combination with type casting sometimes)
Better way for a lot of reasons is to specify the type. Eg:
var health: int
var velocity: Vector2
func get_health() -> int:
return health
func get_velocity() -> Vector2:
return velocity
if it’s that simple, it begs the question: why doesn’t the language always put the colon in by default, aka, under the hood, = just always equals := ?
There must be a reason it’s seperate, other than just something benign like “it only works with simple types”
for me, I just love how static type is waaay more readable, look at this
func test(a, b):
return a + b
now using types:
func test(a:float, b:float)->float:
return a+b
I also like to use it as it allows for better type hinting in the ide and if I forget the type of a var and do something silly, it throws an error.
ОтветитьHi as a complete beginner to programming, how does one learn static typing from the get go? Is it the same as oop? Should I start with gdscript or python? Or something else. I don't have a galaxy brain 😁
Ответитьfor me, the performance benefit is not really what I care but using static hints it's much easier to find/prevent runtime errors and you get autocomplete which are awesome things
Ответитьi'm fascinated by this, given that i've never seen anyone ever actually run stats for static typing's effect on compilation/performance. for the benefit of the audience, what i would love to have seen would have been a brief explanation of why there is a performance increase.
the increase arises from the fact that the compiler has to 'figure out' the type of every variable used in a codebase, and then allocate the corresponding type/memory, before then performing any calculations using those variables. and then those calculations need to be validated against its initial guesses, before it goes back and corrects them as needed. this is a lot of complex, time-consuming work that happens mainly during initial parsing/compilation, but also partially at runtime. by clearly indicating to the compiler what your intent is, and then reliably sticking with that intent, it removes this unnecessary work, and thus dramatically reduces processing time.
Not to mention the warm fuzzy feeling of compile time safety 🥰
One question I have in my mind: With := Godot still has to do type inference (guessing the type, as you called it). Would declaring the type (var health: float = 50.0) be even more efficient?
wow, thats cool, but sometimes you want to keep dynamic type, for instance - collider of raycast3d
ОтветитьOnce you get into static typing, you never go back
ОтветитьTo note here, you could have static typed your for loop as well
for i: int in range(LOOPS)
Not sure how much this would have affected the outcome, but something to point out
You can also tell your function what to expect to be returned with -> type before the colon when declaring a function. For example
func _process(delta: float) -> void:
void means to expect nothing to be returned but you can easily replace this with another data type for example
-> float
-> int
-> Vector2
and so on. Just two more things to share
Very interesting thanks.
ОтветитьStatic type in general programming is very recommended. It helps the compiler a lot when generating and optimizing the machine code (assembly) and resulting generally better execution as well.
ОтветитьGodot Mono Still Faster btw
ОтветитьThere's almost no reason to ever use dynamically typed variables. There's never a good reason to change the type of a variable after it's defined.
ОтветитьThe walrus operator!
ОтветитьThat's it, statically types your dynamic programming language
ОтветитьPerformance improvements are a nice plus, but the real advantage of types is documentation. Especially when working in a team, using a dynamically typed language is a recipe for disaster in the long run.
The type checker goes not away by using a dynamically typed language. The type checker is now you. So you will have cognitive overhead and have to reverse engineer the code base when you don't have types and come back to your project 6 months later.
The best example would be add_address(user) vs add_address(user: User): User.
With the second function, you will see on the first glance that the function will take a User and return a User. You can jump to the definition with your editor/IDE of choice, to see what a User actually is.
With the first function, you will have to read the code and usages to get a clue what's going on.
So besides cognitive overhead you have to have good documentation, which has to be revisited on refactoring and to be honest, documentation will diverge with the code, when time passes.
There is also a setting in the editor that will automatically include these type hints in the default functions. Highly recommend everyone use it
ОтветитьDang, you got a new subscriber
ОтветитьCan you make tutorial about shop system
ОтветитьStatic typing isn't used for performance, it's used to catch errors at compile time instead of runtime.
I mandate for my team, the only time := is used is if the type is named immediately after. Eg var v:= Vector3(0,0,0). Otherwise we always use the full format for code readability. Eg var v: float = 1.0.
You won't see a significant performance boost unless your code is bound by GDScript, which it should not be if you're programming optimally. Your GDScript should be mostly event driven, pushing the work onto the engine. If your game is GDScript bound, static typing is only a bandaid for poor design. Greater improvements will come from restructuring your code.
Im finally sold. My small game just happens to run 1m computations (3D procedural dungeon). And yeah it's slow. But now it can be 20% faster than slow!
ОтветитьAnother consideration is if your end user is on mobile on anything with a battery such as a laptop. The less work the computer is doing, the better the battery life.
ОтветитьFor someone who originally learned programming from languages such as C# and Java - Static typing just feels more right for me!
ОтветитьIn my opinion, the performance of static vs. dynamic typing does not matter, even in the high-performance context of a game. 99.9% of the time, the performance bottleneck comes from an inefficient implementation, never the language itself being slow. The biggest benefit of static typing in my eyes is the ability to implicitly assert the type of a variable and ensure that it will not change. It makes bugs in the form of accidental redefinition and misspelled variables more likely to be caught earlier in the call stack, it helps make common programming idioms easier to notice, as you do not ever have to worry about the type of a given variable changing, and you won't ever have to do explicit type assertion in your functions.
ОтветитьI feel like this video is missing the entire point of statically typed languages. It's mainly about type safety, and makes it a lot easier to keep track of what functions and operations you should be allowed to perform with your variables. I find this especially useful when variables can be transferred across different scripts, like in functions and signals. Also makes writing code easier with autocomplete suggestions.
ОтветитьFaster or not, static typing is the way to go. This helps you prevent bugs and unknown errors later down the line. Take your example of an int becoming a float later in the code; sure, it could be handy in a certain situation, but in that case, change the underlying type. Imagine you're creating a grid system, and use ints for the position. It would be strange if you suddenly got a bug where the grid position is 6.423 no? That is the strength of static typing, as it makes that impossible.
(Not hating on the video, just a little comment :D)
As a c# web developer static typing is my jam, to default to non static is crazy 😮 and lt can lead to less readable code. Always use Typescript for the frontend
ОтветитьI just can't look at "var dookie=1",
I love typing "var dookie: int = 1
Accurate benchmarking and profiling is more complex than the very simplistic code you showed. For example, for most scripting languages you'll want to exclude outliers caused by garbage collection. Not to mention that micro-benchmarks like yours that show 20% improvement may well have negligible impact on more realistic code. That said, static typing all the way for me, I would not go back to dynamic typing for anything more than 10 lines of code.
ОтветитьSick 2j though
ОтветитьThere's no actual static typing in GDScript, just type locking. Any piece of data is technically a Variant.
ОтветитьHey guys, if you keep forgetting to static type your variables (or just want godot to yell at you), there's a project setting in the Debug > GDScript > Untyped Declaration option to change into Error. Hope this helps~
p.s:
funcs also need to be type declared like, func example() -> void:
Disliked cause of AI thumbnail lol.
ОтветитьStatic typing is not really for the speed, but to help write organized code and to pinpoint bugs faster. Like someone said, once you use it and understand why, you never go back.
ОтветитьBonus: it's just more convenient for self-documentation, and Godot can autocomplete functions for you if you tell explicitly which class/type is put into a variable.
ОтветитьI use c=3 it works much better
ОтветитьI wasn't actually aware of the performance benefit. I use static typing so that I can avoid bugs and easily identify mistakes when I forget what data type I need.
ОтветитьAs a java engineer during the day, my brain could not code without it. Now I discover that it even makes the game faster, great!
ОтветитьWell imagine my confusion coming from a Pascal background where := means something entirely different :-) -- Honestly I don't know why dynamic typing is a thing anyways. As you showed not only is it slower, but you might be more prone to errors if you're just making types anyways. I understand, for instance, if you're using an integer for a state machine, you might want to make it more explicit by later changing it to a string, however changing a variable definition to use a different type is not really that hard, and even in here it's almost too easy. The only thing is I might would recommend against the := because you may forget, and in some cases not even remember that you made it a static type. Might be a better deal to just do it long hand var myVar : string = "hello"
ОтветитьNot only the performance is better, but also the autocompletion and the legibility of the code. Actually, I don't think you should use dynamic type at all, it is much more prone to error and it allows clumsier programming (which is a problem in the long run). And, as you said, it's just a ":" for the simpler and more used types.
In Godot, however, the first approach was dynamic typing because GDS is based on python (a dynamic typing language). The static typing was implemented for Godot 4 (I think), that's why some documentation does not use static typing.