Author Topic: Performance critical loops  (Read 4486 times)

Offline TechSY730

  • Core Member Mark V
  • *****
  • Posts: 4,570
Performance critical loops
« on: February 04, 2011, 11:39:21 am »
So what performance critical loops and/or logic is currently in your code? You know, the stuff that gets run so often that every CPU instruction counts.

Of course, you don't need to post the code, just a high level description of what that loop/logic handles/computes.
« Last Edit: February 04, 2011, 11:43:47 am by techsy730 »

Offline x4000

  • Chris McElligott Park, Arcen Founder and Lead Dev
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 31,651
Re: Performance critical loops
« Reply #1 on: February 04, 2011, 11:45:50 am »
Everything.

That might sound silly, but when you have 100k or whatever ships in a game, everything in the simulation is performance critical.

That said, collision detection and targeting are the two biggest items of all.  And movement a bit behind that.
Have ideas or bug reports for one of our games?  Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline LordSloth

  • Sr. Member Mark III
  • ****
  • Posts: 430
Re: Performance critical loops
« Reply #2 on: February 04, 2011, 02:11:53 pm »
That said, I find that munitions and armor boosters seem to cause a significant FPS hit. It jumped about 20 FPS just by turning off drawing those lines in the options, and I've taken to playing with normal ship types for any multiplayer games I host.

Offline x4000

  • Chris McElligott Park, Arcen Founder and Lead Dev
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 31,651
Re: Performance critical loops
« Reply #3 on: February 04, 2011, 02:34:16 pm »
That said, I find that munitions and armor boosters seem to cause a significant FPS hit. It jumped about 20 FPS just by turning off drawing those lines in the options, and I've taken to playing with normal ship types for any multiplayer games I host.

That has nothing to do with the CPU, though.  That's your GPU being unable to keep up, and isn't really what was being asked about.  If your GPU is taking too long, then the CPU has to wait in terms of what it is able to pass it since it has to hand over data, let it get drawn, etc, etc.  So even non-critical-path parts of the code can cause a general slowdown if they remove processing capacity from the critical path part of the code.

But: yeah, in terms of boosting performance, there's more than one way to skin that cat. ;)
Have ideas or bug reports for one of our games?  Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline LordSloth

  • Sr. Member Mark III
  • ****
  • Posts: 430
Re: Performance critical loops
« Reply #4 on: February 04, 2011, 03:31:05 pm »
Well, there was still performance hit to the turn processing stats with with that graphic option turned off, according to the detailed information brought up with the F3 key. So yeah, it was partly graphical, but not entirely. The rendering hit was there, and I didn't do as careful testing and isolation as I would have needed to provide useful feedback for the tracker, but there is enough indication of it having a direct hit to the processing half of the process/render cycle that I should return to it and retest now that I'm thinking about it. For instance, I could get concrete data by tracking the FPS and process renders with the boosters in low power mode, off to the side, and actively boosting with and without drawing the boost lines.

This topic was enough of a reminder to me to that I decided to post on it and motivate myself to do some scientific testing of the issue.

Offline orzelek

  • Hero Member Mark III
  • *****
  • Posts: 1,096
Re: Performance critical loops
« Reply #5 on: February 04, 2011, 04:03:36 pm »
When we are talking performance I have question out of curiosity:

Keith said in parasites thread that there is a significant difference between shift operations and multiply ones - have you guys made some performance tests to compare than on actual live code?

Offline x4000

  • Chris McElligott Park, Arcen Founder and Lead Dev
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 31,651
Re: Performance critical loops
« Reply #6 on: February 04, 2011, 04:25:24 pm »
LordSloth -- it's possible that there is a hit to performance from calculating where the lines begin and end, which is part of the simulation cycle, that's true.  I wouldn't think that math for that few lines would impact things all that much, but you never know.  More likely, it's a factor of those detailed stats not being as reliable as I would like.  In SlimDX they were very reliable, but in Unity I don't even really look at them anymore, to be honest, after I found a number of discrepancies with them.  But point taken.

orzelek -- we haven't done a whole lot of exact testing on that side by side, but we've definitely seen a generalized improvement by shifting them.  The reason it matters enough to notice with AI War is HOW MANY loops and such there are, with so many ships.  If there are 10k ships all moving around, and all of them have a couple of division operators that gets run 20-60 times per second per ship, then shifting that to a shift operation can make a noticeable difference.  Nothing enormous, but a few milliseconds shaved per game turn (four game turns per second), which is always a good thing to accomplish, because milliseconds shaved here and there are always a good thing.

Our performance improvements are often measured in milliseconds shaved, honestly.  If something takes 3ms under light load, and 45ms under heavy load, and we can get that heavy load case down to 20 or 17 or whatever, then the performance impact is immensely huge.   If we can then further reduce it from 17 to 16 in those heavy cases by using shift operators instead of multiplication in a few cases, then that's a lot less notable but still something like a 6% speed improvement on that operation.  Enough 6% operations, and suddenly we're at 10ms instead of 17ms, and we've really got something.

Anyway, so my point is that a lot of the optimizations we make only matter because of the scale of the game.  When the game is in really low-scale mode, with only a few thousand ships (such as in the tutorial), all the processing for each cycle might be taking 1-3ms max.  In those situations, there's no real point to optimization because even a 50% improvement in speed, if we could find a way to get that, would have no effect on anything, literally (because the game is framerate-limiting itself, below a certain ms it doesn't pay any benefits to make it faster, it just gives the CPU more idle time). 

So we always look for those extreme cases, and optimize with them in mind.  That's usually big battles or really high ship count galaxies, and in those cases the economy of scale kicks in and we can make some "small" optimizations that pay big dividends in aggregate.  But for your run of the mill other computer game, or applications other than games, that would be utterly pointless to do.  Like with the tutorials in AI War itself, as an example.
Have ideas or bug reports for one of our games?  Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline TechSY730

  • Core Member Mark V
  • *****
  • Posts: 4,570
Re: Performance critical loops
« Reply #7 on: February 04, 2011, 04:59:27 pm »
I am very impressed that you have gotten such great performance while keeping decent code modularization and a pretty good class structure. Many high performance coders would argue that the inherent overhead of abstraction, objects, and code reuse makes these "good coding practices" infeasible forperformance sensitive code. You have shown it can be done if you do it right. Great job. :)
« Last Edit: February 04, 2011, 05:02:27 pm by techsy730 »

Offline x4000

  • Chris McElligott Park, Arcen Founder and Lead Dev
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 31,651
Re: Performance critical loops
« Reply #8 on: February 04, 2011, 05:06:02 pm »
Well, we have cut a lot of corners on things like encapsulation, etc.  We don't use properties much -- never on performance-critical stuff -- and we don't have a rich object tree for things like the ships.  All of them share one single class type, and it's method-and-variable-based in its differences per ships, rather than inheritance-based.

To some extent that's really different from how I used to write software, but it keeps the general spirit of good OOP design while adjusting for the realities of performance requirements.  I'm really pleased with the result, anyway. :)

Thanks for the kudos!
Have ideas or bug reports for one of our games?  Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline TechSY730

  • Core Member Mark V
  • *****
  • Posts: 4,570
Re: Performance critical loops
« Reply #9 on: February 04, 2011, 05:48:13 pm »
Well, we have cut a lot of corners on things like encapsulation, etc.  We don't use properties much -- never on performance-critical stuff -- and we don't have a rich object tree for things like the ships.  All of them share one single class type, and it's method-and-variable-based in its differences per ships, rather than inheritance-based.

To some extent that's really different from how I used to write software, but it keeps the general spirit of good OOP design while adjusting for the realities of performance requirements.  I'm really pleased with the result, anyway. :)

Thanks for the kudos!

I don't blame you for the Ship object and ship type flag thing. It seems much more scalable and understandable than a gigantic class hierarchy. Personally, I would have made the ShipType type a class, with itself containing variables specifying ship type specific properties, and have the Ship class read its ShipType object for that stuff, but whatever. What you did is more or less the same thing. (I looked it up, and it doesn't look like C# enums can be used as full fledged objects :()

Offline NickAragua

  • Sr. Member
  • ****
  • Posts: 281
Re: Performance critical loops
« Reply #10 on: February 04, 2011, 06:00:54 pm »
When we are talking performance I have question out of curiosity:

Keith said in parasites thread that there is a significant difference between shift operations and multiply ones - have you guys made some performance tests to compare than on actual live code?

At a basic logic circuit level, bit shifting is basically the same as multiplying or dividing by 2 (depending on which
direction). A bit shift is basically a 1 cpu cycle operation while * and / are "many-cycle" operations, with division taking much longer than multiplication. So, on just a basic theoretical level it's always advantageous to replace division/multiplication operations by bit shifts, you don't need performance tests to tell you that. The trick is getting the math to line up.

Offline TechSY730

  • Core Member Mark V
  • *****
  • Posts: 4,570
Re: Performance critical loops
« Reply #11 on: February 04, 2011, 06:06:41 pm »
When we are talking performance I have question out of curiosity:

Keith said in parasites thread that there is a significant difference between shift operations and multiply ones - have you guys made some performance tests to compare than on actual live code?

At a basic logic circuit level, bit shifting is basically the same as multiplying or dividing by 2 (depending on which
direction). A bit shift is basically a 1 cpu cycle operation while * and / are "many-cycle" operations, with division taking much longer than multiplication. So, on just a basic theoretical level it's always advantageous to replace division/multiplication operations by bit shifts, you don't need performance tests to tell you that. The trick is getting the math to line up.

I think for sufficiently awkward and big divisors, you will eventually hit a point where a division algorithm will take less cycles than their equivalent bunch of shifts and additions.

But for many small cases, yea they can be expressed as a nice small sum of bit shifts.

Offline LordSloth

  • Sr. Member Mark III
  • ****
  • Posts: 430
Re: Performance critical loops
« Reply #12 on: February 04, 2011, 06:44:21 pm »
Don't worry about that booster thing I mentioned. I'm not noticing it in my small scale tests of 5.0 (last played with armor boosters on before the various performance tweaks). I'll bring it up again if I actually notice it being a problem this Sunday, if I can pull off this co-op match I'm trying to organize.

edit: or before then, if I manage to get this new game sp game going quick enough. I happen to be without my previous save files, so it'll take a while to get anywhere.

edit: okay, I'm still seeing a 20 FPS drop from munition booster lines when they're on screen. There is no difference between DRAW OFF and booster in low power and DRAW ON in full power mode. So, if anything, it is purely graphical after all. It may be worth submitting a tracker issue with my setup, however. It is an unusual hit in FPS for one graphics effect.
« Last Edit: February 04, 2011, 10:54:23 pm by LordSloth »

Offline KingIsaacLinksr

  • Master Member
  • *****
  • Posts: 1,332
  • A Paladin Without A Crusade...
Re: Performance critical loops
« Reply #13 on: February 04, 2011, 11:07:33 pm »
Don't worry about that booster thing I mentioned. I'm not noticing it in my small scale tests of 5.0 (last played with armor boosters on before the various performance tweaks). I'll bring it up again if I actually notice it being a problem this Sunday, if I can pull off this co-op match I'm trying to organize.

edit: or before then, if I manage to get this new game sp game going quick enough. I happen to be without my previous save files, so it'll take a while to get anywhere.

edit: okay, I'm still seeing a 20 FPS drop from munition booster lines when they're on screen. There is no difference between DRAW OFF and booster in low power and DRAW ON in full power mode. So, if anything, it is purely graphical after all. It may be worth submitting a tracker issue with my setup, however. It is an unusual hit in FPS for one graphics effect.

What is your GPU? 

King
Casual reviewer with a sense of justice.
Visit the Arcen Mantis to help: https://www.arcengames.com/mantisbt/
A Paladin's Blog. Long form videogame reviews focusing on mechanics and narrative analyzing. Plus other stuff. www.kingisaaclinksr.com

Offline Invelios

  • Jr. Member Mark III
  • **
  • Posts: 88
Re: Performance critical loops
« Reply #14 on: February 04, 2011, 11:10:45 pm »
Yeah, I had the same slowdown with the munitions booster lines on my Laptop. However, my new desktop doesn't slow down at all. I'm just gonna suggest you turn them off in the settings if it causes a slowdown. I don't find them all that helpful for my units, but they can be helpful for knowing when the enemy is getting boosted. (More noticeable than looking at tooltips) Maybe there should be an option for only drawing boosting lines for enemies, if that can be done.