Saturday, 10 May 2008

DDD Scotland: Second Session

[Apologies because my laptop blue screened and windows live writer doesn't do auto recovery:( I REALLY need a new laptop now! - everything up to abusing threads is from memory]

How to Write Crap Code in C#

Ben Lamb


Started the demo by showing an application which analyses all off Shakespeare's books to count every word and figure out how often the words are used. Initially he runs it and it takes 2 minutes 1 second which he then explains has a sleep.timer in there set to 2 minutes

Initial program: 1 second

Abusing Exceptions

Initially he checked a dictionary to see if a word existed before updating it. This was then changed to just try and update the dictionary, and if it doest exist handle the exception to add it in.

When he ran this he showed us the CPU maxing out, and performance monitor looking at the number of errors thrown a second (175000 was how many)

Exception flowing: 41 seconds

Ben 1 - Redmond 0

Abusing Threads
You can get a thread by:

  • Using async callbacks
  • Threadpool
  • manually using Thread()

to really screw up a program you have to use Lock()

modify the program so we have one thread for each text file, and then we lock on the dictionary.

Multi Threaded analysers 1 second

Ben 1- Redmond 1

Misuse of the heap (Fun with Strings)

Value objects get stored on the stack (ints etc), reference objects get stored on the head.

  • Avoid Boxing/UnBoxing
  • Use Generic classes
  • Cache Locality
  • Prefer code that works to fast code.

Garbage collection is designed to clean up code - it can use reference counters. This causes the counter to be locked which can cause performance problems. .net uses mark and sweep which walks through an object tree and looks and everything and object it comes across is marked because its being used. Anything not marked gets removed. Then it sweeps everything to group together memory (defraging basically).


Every time an object goes through a process like this its generation gets marked as either generation 0/1/2 - the ones which are old (gen 2) they get put to the bottom so the garbage collection doesn't worry about it too much. Pinned objects are objects which cant be moved because its unmanaged code - this confuses things a bit.

Garbage collection is not deterministic - but don't bother calling it yourself you just make things worse. Plus things like the virus checker could cause more issues.

Showed a normalise line which basically loops through an array of chars to check it is a letter or a space. String builder is then used to append the values together. He changes this to a string which concats itself with the char.

Ben explains that he uses system.diagnostics.stopwatch to show performance details.

Consider putting timer metrics into apps so you can track down issues.

There are some counters showing garbage collection information. % of time in GC is really what you are after. any more in 10% then you have issues.

String concatenation takes: 1 minute 24 seconds.

Ben 2 - Redmond 1.

No comments: