• Advertisement

Noob questions about performance

Recommended Posts

This question are so basic that I feel bad opening a topic for it, whish there was a chat instead, maybe I should propose that later :D

 

Anyway, here's my code:

// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	OpenDoor(DeltaTime);
}

void UOpenDoor::OpenDoor(float DeltaTime) {

	float RotationAmmount = RotationSpeed * DeltaTime;
	if (!FMath::IsNearlyEqual(CurrentYaw, OpenYaw, RotationAmmount))
	{
		float NewYaw = CurrentYaw - RotationAmmount;
		CurrentYaw += NewYaw < -180 ? NewYaw + 360 : -RotationAmmount;
		GetOwner()->SetActorRotation(FRotator(0.f, CurrentYaw, 0.f));
	}
}

My question pertains the OpenDoor() function called every frame.

As you see I'm creating 2 floats inside of it every frame(let's assume this door keep opening forever), is this a good practice or since it happens every frame I should rather have those 2 floats as persistent private variable of my class? Between the two methods, there can be any noticeable performance difference with many assets that create 2 floats at every frame?

My second question is similar to the first, but is about the GetOwner() function called every frame. Should I rather store a Owner pointer in my class? Can this become noticeably expensive if I have many objects calling GetOwner function at every frame?

Edited by MarcusAseth

Share this post


Link to post
Share on other sites
Advertisement
Just now, MarcusAseth said:

This question are so basic that I feel bad opening a topic for it, whish there was a chat instead, maybe I should propose that later

https://www.gamedev.net/community/chat/

 

Regarding the variables:

floats (and other built-in values) are trivial. Don't worry about them, unless you're creating an absolutely insane amount of stuff every frame. If you are, you're likely doing other stuff completely wrong in the first place.

 

Regarding GetOwner:

This could probably do with being cached. Look it up once at the start and store it.

 

Apart from that:

When dealing with performance, you should always be armed with a profiler. Measure, change, measure again. Additionally, worrying too much about performance when performance isn't an issue is very likely to be a waste of time. Focus on getting stuff working.

Share this post


Link to post
Share on other sites

Thanks Lactose, I'll store Owner pointer then :P 

I'm not ready yet to measure stuff I think, no idea of what I am doing, and I would probably end up measuring the wrong things and jumping to wrong conclusions based on that. Unfortunately from time to time the "fear of bad performance ghost" (which I never met) comes back up and distract me from the currently relevant stuff :S

Glad there is a chat, is it a voice chat only though? I can only type :/

 

Share this post


Link to post
Share on other sites
Just now, MarcusAseth said:

Thanks Lactose, I'll store Owner pointer then  

I'm not ready yet to measure stuff I think, no idea of what I am doing, and I would probably end up measuring the wrong things and jumping to wrong conclusions based on that. Unfortunately from time to time the "fear of bad performance ghost" (which I never met) comes back up and distract me from the currently relevant stuff

Glad there is a chat, is it a voice chat only though? I can only type

 

Learning to use a profile is, like learning to use a debugger, a very valuable skill. It might not be critical right now, but if you run into actual performance issues later on, I would highly recommend investing the time then.

 

The chat is text, using Discord.

Share this post


Link to post
Share on other sites

What is the chance that the compiler makes your float global for you ?, it could be possible you dont know how its being optimized.

 

About the GetOwner() , indeed is better to have a pointer at initialize.

 

The best optimization is to perform 1 door every frame, not all doors,

you have a list, call list->opendoor()

then set the pointer in the list to the next door, ready to check in the next frame.

You should do this for everything where possible btw, spread it out over more frames if timing is not crucial.

Share this post


Link to post
Share on other sites

That's interesting, though I have some doubts:

let's say someone PC is running the game at 30 fps and I have 100 doors in my list, let's say the pointer is at the first door of the list and the player is in front of the last door on the list, now they have to stand in front of this door little more than 3 seconds before something happens, right? And that would be considered a bug by the player

Oh well, I bet is the reason you said " if timing is not crucial" :P

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

Keep in mind this sort of logic is often trickier to write than the straight-forward alternative. And even correctly implemented, it might not have any noticable effect on your game's performance -- there might be other problems dwarfing whatever gain you can get from this setup.

It might very well be a good option in a lot cases (it's a fairly common technique) where it's applicable, but when it comes to anything relating to performance, the best option is always to profile and go from there. Measure, change, measure & compare. Repeat until good enough.

 

1 minute ago, the incredible smoker said:

No, you have only the doors of the current zone in the list.

It's easy to imagine a scenario where the design calls for 100 doors on-screen and wanting to open them all at once, in perfect sync. Or a scenario where timing of two different doors must be the same.

Your suggestion isn't a solution for every single case -- like you said, only if timing is not crucial. Sometimes, timing is crucial.

Share this post


Link to post
Share on other sites

Going on a bit, since this is For Beginners and you write that you aren't really sure what to do, the answer is that you shouldn't be worried about performance yet.

Anything you write as an individual is unlikely to tax your computer.  The only real exception to that is things that are breathtakingly wrong from a programming perspective. In that case the problem creates a learning exercise for you.  If you encounter those, people on the forum can usually help you spot them easily. Also, they'll usually be the things you've changed most recently.

Your computer is able to perform many hundred million operations every frame.  Consider the biggest video games your computer can handle, then realize that it can handle all of that work.  Creating a variable is approximately nothing.  Sorting hundreds of game objects in the world based on distance is approximately nothing. 

 

After you gain experience, when you understand more about programming and know several variations and assorted algorithms to accomplish a task, and when you're in a position to understand the tradeoffs between those algorithms, that's when you'll be ready to start having good conversations about performance.

Share this post


Link to post
Share on other sites
On 27/08/2017 at 0:09 AM, MarcusAseth said:

My second question is similar to the first, but is about the GetOwner() function called every frame. Should I rather store a Owner pointer in my class?

Do you have the source code there?

If you do, you can see that GetOwner() is a FORCEINLINE function which returns a copy of a cached 'OwnerPrivate' pointer. It's already pretty much as fast as it can be, so there's no need to cache it yourself.

Share this post


Link to post
Share on other sites
7 minutes ago, Kylotan said:

Do you have the source code there?

If you do, you can see that GetOwner() is a FORCEINLINE function which returns a copy of a cached 'OwnerPrivate' pointer. It's already pretty much as fast as it can be, so there's no need to cache it yourself.

That's really interesting and good to know :o

 Thanks! :)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By sergio2k18
      Hi all
      this is my first post on this forum.
      First of all i want to say you that i've searched many posts on this forum about this specific argument, without success, so i write another one....
      Im a beginner.
      I want use GPU geometry clipmaps algorithm to visualize virtual inifinte terrains. 
      I already tried to use vertex texture fetch with a single sampler2D with success.
       
      Readed many papers about the argument and all speak about the fact that EVERY level of a geometry clipmap, has its own texture. What means this exactly? i have to 
      upload on graphic card a sampler2DArray?
      With a single sampler2D is conceptually simple. Creating a vbo and ibo on cpu (the vbo contains only the positions on X-Z plane, not the heights)
      and upload on GPU the texture containing the elevations. In vertex shader i sample, for every vertex, the relative height to te uv coordinate.
      But i can't imagine how can i reproduce various 2d footprint for every level of the clipmap. The only way i can imagine is follow:
      Upload the finer texture on GPU (entire heightmap). Create on CPU, and for each level of clipmap, the 2D footprints of entire clipmap.
      So in CPU i create all clipmap levels in terms of X-Z plane. In vertex shader sampling these values is simple using vertex texture fetch.
      So, how can i to sample a sampler2DArray in vertex shader, instead of upload a sampler2D of entire clipmap?
       
       
      Sorry for my VERY bad english, i hope i have been clear.
       
    • By mangine
      Hello. I am developing a civ 6 clone set in space and I have a few issues. I am using Lua for the logic and UI of the game and c++ directx 12 for the graphics. I need a way to send information between Lua and c++ occasionally and was wondering what is the best and most flexible (and hopefully fast) way to do this. Don't forget that I also need to send things from c++ back to Lua. I know of a lua extension called "LuaBridge" on github but it is a little old and I am worried that it will not work with directx 12. Has anybody done something similar and knows a good method of sending data back and forth? I am aware that Lua is used more and more in the industry and surely plenty of AAA game programmers know the answer to this. I want a good solution that will hopefully still be viable code in a couple of years...
    • By owenjr
      Hi there.
      I'm pretty new to this and I don't know if it has been asked before, but here I go.
      I'm developing a game using SFML and C++.
      I would like to use the "Tiled" tool to load maps into my game but I don't actually find any tutorial or guide on how to exaclty use it (I know that I have to read an XML file and stuff). I just step into diverse projects that make all a mess. 
      Anyone knows where can I find good information to make my map loader by myself?
      Thanks in advantage!!
    • By MHG OstryTM
      Hello guys,
      I've released my game for the first time. I'm very excited about it and I hope you'll enjoy the game - Beer Ranger. It's a retro-like puzzle-platfromer which makes you think a lot or die trying. You have a squad of skilled dwarfs with special powers and your goal is tasty beer. There is a lot of traps as well as many solutions how to endure them - it is up to your choice how to complete the level! 
      Link to the project: Project site
      Link to the Steam site with video: Beer Ranger
      Have fun and please write feedback if you feel up to.
      Some screens: 




    • By RJSkywalker
      Hello, I'm trying to design a maze using a mix of procedural and manual generation. I have the maze already generated and would like to place other objects in the maze. The issue is the maze object is created on BeginPlay and so I'm unable to view it in the Editor itself while dragging the object to the Outliner. Any suggestions?
      I'm thinking of doing something in the Construction Script or the object Constructor but not not sure if that would be the way to go.
      I'm still getting familiar with the Engine code base and only have a little experience in Maya or Blender since I'm a programmer.
  • Advertisement