I'm working on a Windows Phone 7 XNA game. It's a port of a game written in C++, and as such, I'm trying to do as little rewriting of gameplay code as possible.
Garbage is a huge problem on WP7, because the collector is nongenerational and slow, so a collection (which is triggered every 1MB) takes about 10ms per MB of allocations. I fully intend to be using the maximum 90MB available, so we're looking at a ~900ms stall every MB of allocation.
I've been able to rework things so that we don't have garbage generated per-frame, except for a few cases of strings.
It seems that StringBuilder.ToString() generates garbage, and the method described here doesn't work on WP7.
The two things I need to do are:
- Format minutes/seconds/hundreths as mm:ss.hh for display to the screen. Apparently I can do that with a StringBuilder (using extension methods that don't create garbage from boxing the ints) and display the StringBuilder directly with SpriteBatch.
- Split a string of the form "foo.bar.baz.qux" into an array on '.', i.e. {"foo", "bar", "baz", "qux"} and copy one element at a time into an array of strings. This is for setting the hierarchical state of game actors. It's also pretty much directly ported from the original game, and quite a bit depends on it working this way. I'd really like to avoid rewriting it.
Short of converting a lot of code to use char[] instead of string, is there any way to have truly garbage-free mutable strings in C#?