Sign in to follow this  

Unity StackWalk is too slow

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have the following code:
bool walkStack(CONTEXT& context, bool toInit = false)
{
  static STACKFRAME frame;
  if(toInit)
  {
    ZeroMemory(&frame, sizeof(STACKFRAME));
    frame.AddrPC.Offset = context.Eip;
    frame.AddrStack.Offset = context.Esp;
    frame.AddrFrame.Offset = context.Ebp;
    frame.AddrFrame.Mode = frame.AddrStack.Mode = frame.AddrPC.Mode = AddrModeFlat;
  }

  if( !StackWalk(IMAGE_FILE_MACHINE_I386, GetCurrentProcess(), GetCurrentThread(),
                 &frame, &context, NULL, SymFunctionTableAccess, SymGetModuleBase, NULL) ||
      !frame.AddrFrame.Offset )
    return false;

  return true;
}

void getCallStack()
{
  CONTEXT context = {0};
  context.ContextFlags = CONTEXT_FULL;
  __asm
  {
     call x
  x: pop eax
     mov context.Eip, eax
     mov context.Ebp, ebp
     mov context.Esp, esp
  }

  bool ret = walkStack(context, true);
  for( ; ret; )
    ret = walkStack(context);
}


It's used for storing of call-stack on memory allocation requests (the code here does not contain data storing itself). It works correctly but VERY slow. I tried 64-bit variants of functions/structures as well, but it didn't improve the performance at all. The question of performance of stackwalking was discussed in this thread, but it was just about resolving stack frame addresses with source code symbols. What was wonderful from that thread for me - Visual Leak Detector. It works very fast! But I didn't find any significant differences between the two implementations of stackwalking.. Environment: Windows Vista, Visual Studio 2005. Any ideas are welcome!

Share this post


Link to post
Share on other sites
See Here.

I StackWalk() for 16 frames at allocation time, and then just lookup the symbols when I dump the leaks. I haven't seen any noticible performance problems with that code, but I'm only using it in debug, not release.

Share this post


Link to post
Share on other sites
There are about 3.5 million requests for memory allocation during just start and close of my app. And performance degrades approx. proportionally to the max stack depth I walk :(

Share this post


Link to post
Share on other sites
Walking the stack is only for debugging, so it should not matter if it is slow. In the final product you will remove all of that since it isn't needed any more.

But...

Quote:
Original post by diamant
There are about 3.5 million requests for memory allocation during just start and close of my app.


... how on earth?

Share this post


Link to post
Share on other sites
Quote:
Original post by diamant
There are about 3.5 million requests for memory allocation during just start and close of my app. And performance degrades approx. proportionally to the max stack depth I walk :(
3.5 million allocations!? What on earth are you doing?
StackWalk isn't the fastest function out there, but it's not that slow either. My engine takes approximately 3 seconds to start up (And makes around 6000 allocations to load a Quake I .bsp file with textures, shaders, etc etc) with my memory manager turned on, around 1.5 second with my memory manager turned off, and around 2 seconds with the memory manager turned on, but the stack walk code turned off.

However, as Zahlman said, it's a debug build, performance shouldn't be that much of an issue.

Share this post


Link to post
Share on other sites
In this case it may be advisable to walk the stack manually when running on IA-32 with frame pointers enabled. I vaguely recall VLD doing this as well.
All that needs to be done is grab the stack frame pointed to by EBP, read the return address, and 'disassemble' the code immediately before it. The latter reduces to divining whether the instruction bytes are one of the 9 forms of CALL.

> ... how on earth?
> 3.5 million allocations!? What on earth are you doing?
heh, never measured the number of allocations in large C++ apps? XML parsing and inefficient string processing are particularly swell for reaching large allocation counts.

Share this post


Link to post
Share on other sites

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      628732
    • Total Posts
      2984438
  • Similar Content

    • By INTwindwolf
      THE PROJECT

      INT is a 3D Sci-fi RPG with a strong emphasis on story, role playing, and innovative RPG features such as randomized companions. The focus is on the journey through a war-torn world with fast-paced combat against hordes of enemies. The player must accomplish quests like a traditional RPG, complete objectives, and meet lively crew members who will aid in the player's survival. Throughout the game you can side and complete missions through criminal cartels, and the two major combatants, the UCE and ACP, of the Interstellar Civil War.
      Please note that all of our current positions are remote work. You will not be required to travel.
      Talent Needed
       
      Unity Engine Programmer
      Website Administrator
      3D Animator
      We have made great strides in the year 2017! INT has received a comprehensive face-lift compared to the start of the year. We look forward to a productive, fruitful year 2018!
      Revenue-Share
      This is the perfect opportunity to get into the game development industry. Being an Indie team we do not have the creative restrictions often imposed by publishers or other third parties. We are extremely conscientious of our work and continuously uphold a high level of quality throughout our project.
      We are unable to offer wages or per-item payments at this time. However revenue-sharing from crowd-funding is offered to team members who contribute 15-20 hours per week to company projects, as well as maintain constant communication and adhere to deadlines. Currently the crowd-funding campaign is scheduled for the year 2018. Your understanding is dearly appreciated.
       
      Thank you for your time! We look forward to hearing from you!
       
      John Shen
      HR Lead
      Starboard Games LLC
    • By Apollo Cabrera
      Energy particles being harnessed by collection multi-hedron energy matrix. Whuuuttt?
      Love it :)
    • By AndySv
        Total Music Collection (http://u3d.as/Pxo)   THE COLLECTION CONTAINS:   Mega Game Music Collection   Universal Music Collection   Huge library of high quality music for any project! All at an incredibly low price!   - 2,5GB of high quality audio - 100+ different music tracks - Loop and short versions   Action, fantasy, casual, horror, puzzle, epic, dramatic, romantic, positive, inspiring, motivational and more!
    • By Dafu
      FES Retro Game Framework is now available on the Unity Asset Store for your kind consideration!
      FES was born when I set out to start a retro pixel game project. I was looking around for an engine to try next. I tried a number of things, from GameMaker, to Fantasy Consoles, to MonoGame and Godot and then ended up back at Unity. Unity is just unbeatable in it's cross-platform support, and ease of deployment, but it sure as heck gets in the way of proper retro pixel games!
      So I poured over the Unity pipeline and found the lowest levels I could tie into and bring up a new retro game engine inside of Unity, but with a completely different source-code-only, classic game-loop retro blitting and bleeping API. Months of polishing and tweaking later I ended up with FES.
      Some FES features:
      Pixel perfect rendering RGB and Indexed color mode, with palette swapping support Primitive shape rendering, lines, rectangles, ellipses, pixels Multi-layered tilemaps with TMX file support Offscreen rendering Text rendering, with text alignment, overflow settings, and custom pixel font support Clipping Sound and Music APIs Simplified Input handling Wide pixel support (think Atari 2600) Post processing and transition effects, such as scanlines, screen wipes, screen shake, fade, pixelate and more Deploy to all Unity supported platforms I've put in lots of hours into a very detail documentation, you can flip through it here to get an better glimpse at the features and general overview: http://www.pixeltrollgames.com/fes/docs/index.html
      FES is carefully designed and well optimized (see live stress test demo below). Internally it uses batching, it chunks tilemaps, is careful about memory allocations, and tries to be smart about any heavy operations.
      Please have a quick look at the screenshots and live demos below and let me know what you think! I'd love to hear some opinions, feedback and questions!
      I hope I've tickled your retro feels!



      More images at: https://imgur.com/a/LFMAc
      Live demo feature reel: https://simmer.io/@Dafu/fes
      Live blitting stress test: https://simmer.io/@Dafu/fes-drawstress
      Unity Asset Store: https://www.assetstore.unity3d.com/#!/content/102064

      View full story
    • By Dafu
      FES Retro Game Framework is now available on the Unity Asset Store for your kind consideration!
      FES was born when I set out to start a retro pixel game project. I was looking around for an engine to try next. I tried a number of things, from GameMaker, to Fantasy Consoles, to MonoGame and Godot and then ended up back at Unity. Unity is just unbeatable in it's cross-platform support, and ease of deployment, but it sure as heck gets in the way of proper retro pixel games!
      So I poured over the Unity pipeline and found the lowest levels I could tie into and bring up a new retro game engine inside of Unity, but with a completely different source-code-only, classic game-loop retro blitting and bleeping API. Months of polishing and tweaking later I ended up with FES.
      Some FES features:
      Pixel perfect rendering RGB and Indexed color mode, with palette swapping support Primitive shape rendering, lines, rectangles, ellipses, pixels Multi-layered tilemaps with TMX file support Offscreen rendering Text rendering, with text alignment, overflow settings, and custom pixel font support Clipping Sound and Music APIs Simplified Input handling Wide pixel support (think Atari 2600) Post processing and transition effects, such as scanlines, screen wipes, screen shake, fade, pixelate and more Deploy to all Unity supported platforms I've put in lots of hours into a very detail documentation, you can flip through it here to get an better glimpse at the features and general overview: http://www.pixeltrollgames.com/fes/docs/index.html
      FES is carefully designed and well optimized (see live stress test demo below). Internally it uses batching, it chunks tilemaps, is careful about memory allocations, and tries to be smart about any heavy operations.
      Please have a quick look at the screenshots and live demos below and let me know what you think! I'd love to hear some opinions, feedback and questions!
      I hope I've tickled your retro feels!



      More images at: https://imgur.com/a/LFMAc
      Live demo feature reel: https://simmer.io/@Dafu/fes
      Live blitting stress test: https://simmer.io/@Dafu/fes-drawstress
      Unity Asset Store: https://www.assetstore.unity3d.com/#!/content/102064
  • Popular Now