XNA Creators Club Online
Page 2 of 2 (30 items) < Previous 1 2
Sort Posts: Previous Next

Avoiding int.ToString() GC garbage

Last post 3/7/2010 5:41 AM by Matt Hughson. 29 replies.
  • 9/24/2008 5:20 AM In reply to
    • (71)
    • premium membership
    • Posts 69

    Re: Avoiding int.ToString() GC garbage

    nop:
    Come to think of it I should probably clean up the code and release it or something...

    Well, it took me almost a month, but I finally found some time and untangled the font code from my hideous mess of a utility library and put it up on Google code. Sorry for the lack of sample project, etc, work is killing me right now and I haven't had much time to get everything I want to release in a reasonably good state. You can look at the FrameStats component if you want to get an idea of where to start with it.

  • 1/26/2009 5:33 AM In reply to

    Re: Avoiding int.ToString() GC garbage

    I decided to use CLR Profiler on my emulator and came across the same problem as described in this thread. I threw together a quick and dirty StringBuilder extension method that verifies it can avoid generating garbage: 

        public static class StringBuilderExtensions  
        {  
            public static StringBuilder AppendWithoutGarbage(this StringBuilder stringBuilder, int number)  
            {  
                if (number < 0)  
                {  
                    stringBuilder.Append('-');  
                    number = Math.Abs(number);  
                }  
     
                int index = stringBuilder.Length;  
                do 
                {  
                    stringBuilder.Insert(index, _digits, number % 10, 1);  
                    number /= 10;  
                }  
                while (number > 0);  
     
                return stringBuilder;  
            }  
     
            private static readonly char[] _digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };  
        } 

    For some reason the StringBuilder.Insert(Int32, Char) overload calls ToString() internally, but this isn't in the Compact Framework anyway, so I use StringBuilder.Insert(Int32, Char[], Int32, Int32). Obviously there's a performance vs garbage tradeoff ... so consider this a starting point that eliminates the garbage with the least effort.

  • 1/26/2009 6:15 AM In reply to

    Re: Avoiding int.ToString() GC garbage

    Your function fails if I try to append the number -2147483648 :-)
    It's the only legal "int" that it fails for, though.
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 1/26/2009 9:05 AM In reply to

    Re: Avoiding int.ToString() GC garbage

    Doh! The following will handle that boundary case:

        public static class StringBuilderExtensions  
        {  
            public static StringBuilder AppendWithoutGarbage(this StringBuilder stringBuilder, int number)  
            {  
                if (number < 0)  
                {  
                    stringBuilder.Append('-');  
                }  
     
                int index = stringBuilder.Length;  
                do 
                {  
                    stringBuilder.Insert(index, _digits, (number % 10) + 9, 1);  
                    number /= 10;  
                }  
                while (number != 0);  
     
                return stringBuilder;  
            }  
     
            private static readonly char[] _digits = new char[]
            {
                '9', '8', '7', '6', '5', '4', '3', '2', '1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            };  
        } 

  • 3/7/2010 5:41 AM In reply to

    Re: Avoiding int.ToString() GC garbage

    Is this still the recommended way to handle String manipulation without generating garbage, or have there been any breakthroughs since this posting?  I haven't been able to find another solution yet (or one that supports floating point numbers, or even appending two StringBuilders).
    Matt Hughson
    [ Website ] [ Résumé ] [ Contact ]
    Playtest: Man Moments
Page 2 of 2 (30 items) < Previous 1 2 Previous Next