Friday, 18 March 2016

I Stepped on a Performance Landmine

Today I finally got to the bottom of a strange case of the jitters that Blackshift would often come down with on Windows.

I'd just rewritten my profiling code, because it's time to get serious about performance, and here is the graph it presented me with. Can you see the problem?


If you're not used to reading this sort of chart, it shows how long the game engine spends doing each task for every frame. The Y axis is in milliseconds, and the coloured areas are different tasks. Blackshift runs at 60 frames per second so ideally each frame completes in 1000 / 60 = 16.6ms, which as you can see is usually the case.

As you can see, the graph is dominated by the render task (violet) waiting for vsync, and a rather heavy maroon task which turns out to be...  wait... that can't be right. "Read gamepads"?

I did some research and it turns out [1] [2] that, yes, windows' gamepad routines can take a shocking 8ms just to tell you if any buttons are being pressed. 8ms! If you've got a decent Internet connection you can play Counterstrike with an 8ms ping, which means your inputs are going out of your computer, all the way to s0ulb0y_3edgy420me's house and back quicker than they can get from your joystick, through winmm into Blackshift.


In fact, that's not quite accurate. It turns out, this delay is only incurred if there isn't a gamepad plugged in to your computer. If you actually use one, winmm behaves like a sane library designed by proper people at an actual company and returns its results almost instantaneously.

My solution: If winmm ever reports that there is no gamepad plugged in, simply stop calling joyGetPosEx, which is the devil, until you get a WM_DEVICECHANGE message. You'd think Windows could do something like that internally.

Anyway. Suffice it to say the next Blackshift update will contain Windows performance fixes.


25 comments:

  1. Can't exactly tell by the colours but it looks like "Draw Game" is whackin' the processor. Either way, still having trouble with the pick up an item and going left as I stall. I think I tried it with WASD and it was better. Not the keyboard because I have the game loaded on 3 computers.

    ReplyDelete
    Replies
    1. If you mean the big spikes those are just when levels load, you can ignore those.

      That going left thing... I moved it to Done because I thought I'd fixed it but I guess not. Does it only happen with the arrow keys?

      Delete
    2. I am not good at WASD as I am right handed. You will have ask Moviette Goaway (Bibbythedark) about that but I think it works better that way.

      Delete
    3. Did you notice the latest update has redefinable keys?

      Delete
    4. Play "Bibby the Hard". When you pick up something and have to go left I stall and have to double click to move. Bibby does it with ease so, just wondering! He beat my score by 2 minutes! Your program is right oriented and I have to force the issue to make enemies go left. Just sayin'.

      Delete
    5. Sure, but what I'm saying is, if you set your keys to, say IJKL, which you can still use with your right hand, does the problem still happen?

      Delete
    6. Redefinable keys - nope - will check! Obviously in options but didn't look - building a new left handed level for the noobs!

      Delete
    7. Ha ha ha, just looked at a replay on Obie2 which I messed up. Bobby found the secret (which I messed up) He will soon take top score.

      Delete
    8. I will try that Rob - get back to you later. Need bacon now! Being Canadian eh!

      Delete
    9. BTW, Obie2 is "Adam", slightly modified (which I screwed up) from the old Blackshift game. I forgot to fix levels 2 and 3 with directional gun in 2 and a deflector in 3 where the permanent guns keep shooting. Damn!

      Delete
    10. Nice work on Obie2 Moviette, but you could have done better! Let's see what Bobby can do!

      Delete
  2. I've just noticed a minor bug while playing the new "Sorry4delay".
    When you walk on a "zen" tile (the blue neon yin-yang one) while carrying a key, you got a shadow surrounding you... It only happen when you're carrying a key, I've tried with other items available on that level.

    ReplyDelete
    Replies
    1. Eagle eyes! Thanks, I'll get this fixed.

      Delete
  3. Crashed 3 times this morning. Don't know why. Using OpenGL so will try something else.

    ReplyDelete
  4. DX11 didn't crash but then had a problem with instancing!

    ReplyDelete
  5. Hell yeah! I've completed Impossible City. That was truly a journey!
    Btw, this prove that the save function in the level pit really do work. Because I've used a lot of saves and restarted many times to complete this level (without mentioning that my score is almost 1 hour long).

    ReplyDelete
    Replies
    1. Whoa, nice one Adam! I was wondering if anyone would. Yeah, the main reason I made it was to exercise the Pit's handling of big levels and long plays. One day I'll introduce prizes for difficult feats, but for now you'll have to make do with bragging rights :)

      Delete
    2. Just looking at your replay, you found a very laborious solution, much harder than the one I had in mind.

      Delete
    3. I know! xD
      I'll redo it with a better solution one day.
      While doing it I didn't think of a better way, but now that I know the level, I could certainly do better ;P

      Delete
    4. That one needs to be Level 100! I think Level 100 of the original Blackshift took me a week to finish Ha ha ha Been crashing all day on all computers since the latest update. Have no idea why. Used OpenGL and DX11, I am sure you got the crash reports. Figure it out Rob!

      Delete
    5. Done it!
      With a improvement of... 30 minutes ;P

      Delete
  6. New weapon Rob - machine gun!

    ReplyDelete
    Replies
    1. Like one of the regular guns except it keeps firing regular bullets if you stay on it?

      Delete
  7. Ha ha ha Wunk, I spend half my time figuring out how to stop Bibbythedark and Bobby from committing suicide for the win!

    Crashed again tonite Rob on the AcerWin8.1 on my new level. Usually happens when a rolling sausage kills me. I am sure you got the crash reports.

    ReplyDelete

Note: only a member of this blog may post a comment.