Replies...
2) Sorry I was being overly simplistic. Yes, there are special cases when it is deterministic from analyzing the code that it is impossible for you to walk off the end of the array, because as I stated it's the CLRs job to guarantee that you can not, so it always bounds checks (either physically every single access, or logically by pre-checking when it knows the code can't walk off in subsequent accesses). For example when loops are unrolled this can happen depending on what you are doing in the loop.
If you are trying to nail down the precise conditions of when it makes these determinations so that it can affect how you craft your code, I suggest you think twice about it at any point, and most definately at any point before the tuning phase. I say this because leveraging unpublished, priviliged information is not always wise. Due to it's very nature it is also subject to change without notice since it never was published. There is no contract in play to be honored.
The 360 hardware may be static, but the 360CF doesn't have to be. In fact it will possibly change every time the XNA Framework revs. If an implementation detail were to change, your specially crafted loop may then run slower; not faster, especially across CLR impl's. Therefore it is wise to construct your code in a manner that is consistent with what the language and designers intended. Over time this is likely to be the best performing code, and will certainly always be the most maintable. Crafting non-intuitive code to take advantage of an unpublished implementation detail to eek out a few extra cycles is not wise. The only time I would consider doing this is during tuning phase after I had hard evidence that said code was a major bottleneck and rewriting it non-intuitively and taking advantage of unpublished implementation details would reap a significant performance boost.
3) Have you tried unsafe code on the xbox I have not, but I have definately read that it can not be done; I'm pretty sure here on the forums. Just because you can check the "unsafe" check box does not mean the code will run. I would be *amazed* that it would. When you check the unsafe checkbox it causes the unsafe flag to be set in the manged module metadata. If the 360CF allows such assemblies to load it's a whopper of a secuirty hole that would probably be exploited eventually. I would consider it a defect if the 360CF allowed unsafe code to load.
The bottom line is any XNA game on the 360 bypasses an exhaustive Certification Process (CP). One major goal for the exhaustive CP is to find any bad code (intentional or not) that can affect the users experience, not only for that game, but for the integrity of the entire system including other saved games, digital media, etc. However, since XNA itself went through the CP, it should not be possible to write games with it that defeat the spirit and one of the primary goals of the CP to begin with. I assume one of the reasons the CP folks allowed XNA through is a) it's sandboxed and b) it's locked down. Unsafe code violates this, and therefore XNA games would not be "certified" safe by-proxy.
If unsafe code is allowed to run on the 360, then the CP is truly bypassed. In theory, somebody would be able to implement a buffer overflow exploit and gains control of the system and could then do something malicous outside the sandbox. The chances of this happening accidentally are low. However, the chances of this happening intentionally from a hacker approaches 100% given enough time and motivation. After all, the #1 rule in secure programming is you assume the worst up front.
One could incorrectly argue that "Well, only Creators Club members can be affected, they take on extra risk by running XNA games". In addition to being incorrect, that's simply not the right attitude; it's dangerous. The 360 is marketed as a family entertainment device and has multiple profiles, removeable media, and competitive network play. Unsafe code buffer overrun exploits could be used to destory digital media that does not belong to the Creators Club member, or could be used to give the Creators Club member an unfair advantage (ie cheating).
I'm not saying that it would be easy for somebody to find and take advantage of these exploits. But it would not even be possible if the unsafe is not allowed (shy of a bug in the CLR). Again, using proper approch to security programming, you assume these bugs exist and the exploits are possible. Therefore you do not allow unsafe code.
If unsafe code is allowed today on the 360, it's either
a) due to a bug and will be fixed
b) due to a poor design decision by somebody who wasn't concerned about security, and will be fixed once exploited becuase somebody didn't follow the first rule of secure programming.
As such, if this works today I would not count on it working forever and as such would not recommend using unsafe anywhere. The bottom line is if you pay attention to the things that really matter, it shouldn't be unnecessary to use unsafe to get a high-performance game.
In Summary, as I mentioned before, worrying about bounds checks (and using unsafe code and pointers to avoid them) and inline functions (and using parameterized public fields to allow inlining where it normally would not occur) is inherently non-productive; at least up front.
In my experience it is far more effective use of ones time to concentrate on good algorithms and reducing GC impact, and don't worry about the other stuff until the tuning phase, once you can measure how much of a problem these issues really are (or are not!).
- Ryan