Hello there,
I was following the “CryptRider” tutorial of the “Unreal 5.0 C++ Developer: Learn C++ and Make Video Games” course after the “GetWorld()” video.
I see that the GetWorld() method gets called in Tick(), and then the UWorld* ptr is used to print some data. I don’t remember if the result gets stored in a UWorld* local variable, but this doesn’t change much, as GetWorld() is still called every frame, and returns a pointer very frame.
The operation itself is not expensive, as GetWorld() is just returning a pointer to “World”, but it’s getting called every frame.
Shouldn’t it be better to just declare a UWorld* pointer variable inside our class and use the BeginPlay method to assign it to the GetWorld() returned ptr only once?
We should be able to access the UWorld using our pointer, but without recalculating it every time.
The compiler will almost certainly optimise the function call away and just access it directly, and indeed, trying just now to step into GetWorld() on that line with the debugger just steps over it.
So you would just end up storing two copies of World, one inherited from UActorComponent and the one you would be storing in UGrabber
Which if I’m not mistaken (x86 noob), put the value of WorldPrivate into the rax register then test it with itself which would clear the Zero Flag (ZF) if it’s 0 (null here), then if ZF is 0 it will jump to the second to last line, which puts TimeSeconds into a register.
So if you made a member variable for it then you would save the test and jump (unless you then add checks to see if your variable is non-null).
I’m not sure if this is what you’re looking for exactly but it’s a great talk nonetheless
There’s also (all of Scott’s CPPCon and GoingNative talks are great)
I can’t find it now but I remember seeing a talk by Herb Sutter in a similar vein to the last one
Thank you very much for the explanation and the resources!
I will try to learn understanding and reading the disassembly as well, as it seems useful when doing performance fine-tuning and/or learning what’s happening under the hood.
Also thanks for the videos, I will watch them for sure!