Pithy Perf Patterns

Maoni Stephens, Perf PM on the CLR team sent me some key “patterns”... You will see I have not expanded on any of them, I am sure we can talk Rico into blogging on any of these at length if we ask nicely....

  1. avoid finalizers; use the Dispose pattern.
  2. avoid the urge of writing your own memory allocator (which is a common thing to do in native world)
  3. avoid having the object pattern of lots of midlife-ed objects
  4. use chunky instead of chatty interfaces in interop, and avoid marshaling complex types if you can
  5. use the ThreadPool instead of managing your own threads
  6. use caches to avoid doing the same things again and again (of course this applies to native world as well but we've seen so many cases where people do this - like fusion parsing the same manifest again and again and etc)
  7. don't throw exceptions when you don't have to - throwing exceptions is expensive in managed world
  8. know the hidden cost (examples include using foreach; when boxing happens; when reflection is involved)
  9. don't use XML without thinking! (again, also applies to native - DOM is slow too but I guess the managed xml classes are so easy to use and we've seen so many cases where people abuse using xml).
  10. And this important thing applies to all: "pay for play" - only do things when you need to.

UPDATE: Maoni pointed out to me that I left off her most important rule... "With perf there are rarely absolutely rules so we need to tell people why they want (or not want) to do these so they can decide for themselves " It is a good rule indeed.

Also, notice you can always find good perf info on Rico's blog.