Jiaozi (1) [Avatar] Offline
Hello Erik,

I'm sorry, I don't read your book and was unable to find your email. So I am writing here to talk you a bit about your article on MSDN magazine (Rediscover the Lost Art of Memory Optimization in Your Managed Code).

For your example, you may define your enum like this and gains an extra byte.

enum AddressType : byte

I am currently writing a C# UI library. I have noticed two things Microsoft guys has developed to improve memory space. The first is System.Collections.Specialized.BitVector32 class. This is the .Net equivalent of the bit fields from the C language.

Example with the checkbox:

public class CheckBox : StateLabelBase
private static readonly BitVector32.Section StateThreeState = BitVector32.CreateSection(1);
private static readonly BitVector32.Section StateCheckState = BitVector32.CreateSection(2, StateReadOnly);

private BitVector32 cbState = new BitVector32();


public bool ThreeState
get{ return cbState[StateThreeState] != 0; }

set{ cbState[StateThreeState] = value? 1:0; }

public System.Windows.Forms.CheckState CheckState
get{ return (System.Windows.Forms.CheckState) cbState[StateCheckState]; }

if(value == CheckState) return;

cbState[StateCheckState] = (int) value;

Comments :

This class offers 32 bits, accessible via sections or the number of a target bit. The sections are just structure which contains a mask and an offset to manipulate the bits.

The first section BitVector32.CreateSection(1) starts the booking. 1 is not the number of the bits we want to reserve but the maximum value this section may contains. Here, this is typically a boolean store. But in the second section, we claims 2 bits to be able to put an enum in it. This is an easy way to optimize memory space for low speed cost.

The second point I want to talk you is about the events and delegates. This case is really focused on UserControl because the involved class (System.ComponentModel.EventHandlerList) become profitable when :

1) you define some events you know lots of them will not be listened at each time. Else, use the traditional way :
public event EventHandler CheckStateChanged = null; and save the extra variable.

2) As you define that extra variable, the class (here CheckBox) will be instanced several times.

3) the number of events you store in the EventHandlerList is not too big because the search is linear (this remarks comes from the MSDN. This is strange because I have noticed the Framework stores an average of 50 events. Further performance tests is required...)

Let see an example with the CheckBox control :

public class CheckBox : StateLabelBase

private static readonly object EVENT_CHECKSTATECHANGED = new object();

public event EventHandler CheckStateChanged
add{ Events.AddHandler(EVENT_CHECKSTATECHANGED, value); }
remove{ Events.RemoveHandler(EVENT_CHECKSTATECHANGED, value); }

protected virtual void OnCheckStateChanged(EventArgs e)
// Events is a property inherited from Component. So, it exists anyway.

EventHandler handler = (EventHandler) Events[EVENT_CHECKSTATECHANGED];
if(handler != null) handler(this, e);

Ok, I have finished. I hope you learn something smilie


[edit] damn, no way to write spaces in the code !
eebrown (89) [Avatar] Offline
Re: Critics about your article published in MSDN Magazine

I was in job flux at the time I wrote the article, so I didn't want to include an email address that might later be invalid.

Thanks for your comments on the article,