Def­i­n­i­tion:

CTP stands for Commu­nity Tech­nol­ogy Preview, another fancy term for a ‘beta’ or ‘prototype’.

RTM stands for Release To Manu­fac­tur­ing or Release To Market.

Ref­er­ences:

Soft­ware release life cycle

Share

I came across these two posts on Eric Lip­pert’s blog yes­ter­day which I found very interesting:

Rep­re­sen­ta­tion and Identity

Cast oper­a­tors do not obey the dis­trib­u­tive law

The blog posts go into a lot of details but long story short, if you box a value type you can’t unbox it to another type:

image

It would result in an Invalid­Cas­tEx­cep­tion, and the big ques­tion is, if there exists a con­ver­sion from int to long why can’t the run­time work it out?

Well, con­sider three sim­ple types A, B and C, where B inher­its from A and C pro­vides an explicit con­verter from A:

public class A {}
public class B : A {}
public class C 
{
    public static explicit operator C(A a)
    {
        return new C();
    }
}

There exists two types of con­ver­sion which you can achieve using the Cast­ing operator:

var b = new B();
var a = ( A ) b; // inheritance-based conversion, equivalent to var a = b as A;
var c = ( C ) a; // operator-based conversion

In essence, the inheritance-based con­ver­sion is just show­ing the same object in a ‘new light’, whereas the operator-based con­ver­sion requires a spe­cial treat­ment on a case-by-case basis in the form of con­ver­sion methods.

Inheritance-based Con­ver­sion

(representation-preserving)

Operator-based Con­ver­sion

(representation-changing)

New object is not con­structed con­structed
New vari­able points to orig­i­nal object new object
Chang­ing the new variable changes the orig­i­nal object doesn’t change the orig­i­nal object
Con­ver­sion is fast slow

A box value type is an object, so as far as the com­piler is con­cerned it could be any­thing. There­fore by allow­ing a boxed value type to be cast to a dif­fer­ent type you intro­duce new chal­lenges to the compiler:

  1. it needs to gen­er­ate more code at run­time (from CIL to machine code by the JIT-Compiler) because the com­piler needs to check if it needs to call a con­ver­sion method after unbox­ing the boxed value type.
  2. it also needs to work out which con­ver­sion method to call, this requires sig­nif­i­cant amount of analy­sis giv­ing that there can be an arbi­trary num­ber of con­ver­sion meth­ods (both built-in and user-defined) on arbi­trar­ily many types.

Unfor­tu­nately the cost of meet­ing these chal­lenges is per­for­mance, and the sen­si­ble default was to be “fast and pre­cise” but still allow­ing this type of con­ver­sion through the Con­vert class.

Part­ing thoughts…

Towards the end of the post there was a warn­ing to those look­ing to abuse the new dynamic type sup­port in C# 4 ;-)

“…if the argu­ment to the cast is of type “dynamic” instead of object. The com­piler actu­ally gen­er­ates code which starts a mini ver­sion of the com­piler up again at run­time, does all that analy­sis, and spits fresh code. This is NOT FAST, but it is accu­rate, if that’s what you really need. (And the spit code is then cached so that the next time this call site is hit, it is much faster.)…”

Share

If you try to serialize/deserialize a type which uses the generic Dictionary<TKey, TValue> type with the XmlSe­ri­al­izer then you’ll get an Invali­d­Op­er­a­tionEx­cep­tion, for instance:

Here’s my class:

public class MyClass
{
    // need a parameterless constructor for serialization
    public MyClass()
    {
        MyDictionary = new Dictionary<string, string>();
    }
    public Dictionary<string, string> MyDictionary { get; set; }
}

I’ll get an Invali­d­Op­er­a­tionEx­cep­tion with an inner excep­tion of type Not­Sup­port­edEx­cep­tion when I do this:

var xmlSerializer = new XmlSerializer(typeof(MyClass));

And the error mes­sage of the Not­Sup­port­edEx­cep­tion is:

“Can­not seri­al­ize mem­ber MyClass.MyDictionary of type System.Collections.Generic.Dictionary‘2[[System.String, mscor­lib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscor­lib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it imple­ments IDictionary.”

The rea­son why Dictionary<TKey, TValue> is not sup­ported by the XmlSe­ri­al­izer is that types such as Dic­tio­nary, HashTable, etc. needs an equal­ity com­parer which can’t be seri­al­ized into XML eas­ily and won’t be portable any­how. To get around this prob­lem, you gen­er­ally have two options, in the order of my per­sonal preference:

Use Dat­a­Con­tract­Ser­iza­l­izer

The eas­i­est, and clean­est solu­tion is to switch over to data con­tract seri­al­iza­tion, and the only change required would be to mark your class with the Dat­a­Con­trac­tAt­tribute and mark the prop­er­ties you want to seri­al­ize with the DataMem­ber­At­tribute. My class would now look like this:

[DataContract]
public class MyClass
{
    // need a parameterless constructor for serialization
    public MyClass()
    {
        MyDictionary = new Dictionary<string, string>();
    }
    [DataMember]
    public Dictionary<string, string> MyDictionary { get; set; }
}

And to seri­al­ize it into Xml, just use the Dat­a­Con­tract­Se­ri­al­izer and write the out­put with a Xml­Tex­tWriter like this:

var serializer = new DataContractSerializer(typeof(MyClass));
string xmlString;
using (var sw = new StringWriter())
{
    using (var writer = new XmlTextWriter(sw))
    {
        writer.Formatting = Formatting.Indented; // indent the Xml so it's human readable
        serializer.WriteObject(writer, marketplace);
        writer.Flush();
        xmlString = sw.ToString();
    }
}

Use a cus­tom Dic­tio­nary type

The alter­na­tive is to cre­ate your own Dic­tio­nary imple­men­ta­tion which is Xml seri­al­iz­able. See the ref­er­ences sec­tion for an imple­men­ta­tion Paul Wel­ter has created.

Ref­er­ences:

XML Seri­al­iz­able Generic Dictionary

Share

As good and inno­v­a­tive as WCF is, it also intro­duced a lot of new com­plex­i­ties and whilst it is easy to get some­thing up and run­ning quickly it takes much more under­stand­ing to make your ser­vice per­form as well as it could.

There are many things you need to con­sider such as bind­ing types, seri­al­iza­tion type, Datat­able or POCO, etc. etc. and any of these choices can have a telling effect on the over­all per­for­mance of your WCF ser­vice. Scott Wein­stein wrote a very good arti­cle on how to cre­ate a high per­for­mance WCF ser­vice (see ref­er­ence sec­tion) using 6 sim­ple steps.

With­out going into the sub­jects of bind­ing selec­tion and data nor­mal­iza­tion I want to just focus on how you can achieve greater con­cur­rency because for ser­vices hosted on the web you won’t be able to use NetTcp­Bind­ing and data nor­mal­iza­tion is almost irrel­e­vant because you won’t (or at least shouldn’t!) be send­ing large amounts of data back and forth.

Increase the throt­tling limits

Gen­er­ally speak­ing, the most com­mon way to improve the per­for­mance of a WCF ser­vice is to encour­age greater con­cur­rency and if you have used WCF before then chances are you’ve had to change the default throt­tling behav­iour con­fig­u­ra­tion because the defaults are too low for any real world appli­ca­tions to be useful.

These defaults are set to ensure your ser­vice is safe from DOS attacks but unfor­tu­nately also means your ser­vice will run in lock-down mode by default. They have since been raised to more sen­si­ble num­bers in the new WCF 4 release:

Max­Con­cur­rentSes­sions Max­Con­cur­rent­Calls Max­Con­cur­rentIn­stances
WCF 3.5 SP1 10 16 26
WCF 4 100 * Proces­sor Count 16 * Proces­sor Count 116 * Proces­sor Count

The new defaults in WCF 4 should pro­vide a good guide­line for you when con­fig­ur­ing the Ser­viceThrot­tling­Be­hav­ior of your ser­vice (assum­ing you’re not using WCF 4 already).

Use the Per­Call or PerS­es­sion instance con­text mode

The Instance­Con­textMode also plays a sig­nif­i­cant role in the over­all per­for­mance of your ser­vice, and of the three options avail­able to you – Per­Call, PerS­es­sion and Sin­gle­ton – you should con­sider Per­Call or PerS­es­sion for a highly scal­able WCF service.

Whilst the Per­Call instance con­text mode is gen­er­ally regarded as the most scal­able option it does carry with it the need to cre­ate a instance of your class for each request and you need to ensure that 1) you have a para­me­ter­less con­struc­tor, and 2) this con­struc­tor should do as lit­tle as pos­si­ble. If there are any sig­nif­i­cant steps that need to be per­formed, such as load­ing some ref­er­ence data, you should avoid doing these in the para­me­ter­less con­struc­tor so they aren’t per­formed for each request:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService
{
    public MyService()
    {
        ReferenceData = LoadReferenceData(); // will be called for EACH request…
    }

    public MyReferenceData ReferenceData { get; private set; }
    …
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService2 : IMyService
{
    // called the first time the reference data is used and shared across all instances
    private static MyReferenceData ReferenceData = LoadReferenceData();

    public MyService2()
    {
        // ideal constructor which does nothing
    }

    …
}

In cases where the ini­tial­iza­tion steps are lengthy and unavoid­able, or your class require a num­ber of para­me­ters in the con­struc­tor (for instance, when you pro­gram­mat­i­cally host a ser­vice retrieve from an IoC con­tainer) the para­me­ter­less con­struc­tor can become a prob­lem. To get around this, you could cre­ate a wrap­per for your class and expose the wrap­per as the ser­vice instead but hold a sta­tic instance of the under­ly­ing ser­vice which all the requests are passed on to:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyServiceWrapper : IMyServiceWrapper
{
    // get the underlying service from the container
    private static IMyService MyService = _container.Resolve<IMyService>();

    public MyServiceWrapper()
    {
        // parameterless constructor which does nothing, so easy to constructor
    }
    public void DoSomething()
    {
        MyService.DoSomething();
    }
}

// dummy interface to ensure the wrapper has the same methods as the underlying service
// but helps to avoid confusion
public interface IMyServiceWrapper : IMyService
{
}

For a ses­sion­ful ser­vice, the PerS­es­sion instance con­text mode gives you all the ben­e­fit of the Per­Call instance con­text mode and at the same time reduces the over­head you pay for that extra con­cur­rency because new instances of your class are no longer cre­ated for each request but for each ses­sion instead.

If your ser­vice is ses­sion depen­dant then you should def­i­nitely go with PerS­es­sion, but beware, if the chan­nel does not cre­ate a ses­sion your ser­vice will behave as if it was a Per­Call service.

Increase the num­ber of idle IO threads in the thread pool

Per­haps the most over­looked aspect when it comes to increas­ing con­cur­rency of a WCF ser­vice. If you’ve set your ser­vice to use Per­Call or PerS­es­sion instance con­text mode and upped the throt­tling set­tings, but are still not get­ting the response times you’re look­ing for, then it’s worth inves­ti­gat­ing whether the calls are being queued because there is not enough IO threads in the Thread­Pool to han­dle the requests.

You can estab­lish whether or not the requests are actu­ally tak­ing longer to process under load (as opposed to being queued at a ser­vice level) either by pro­fil­ing locally or using some form of run-time log­ging (I wrote a LogEx­e­cu­tion­Time attribute which might come in handy). If the calls aren’t tak­ing longer to process and you’re not see­ing very high CPU util­i­sa­tion then the increase in response time is likely a result of the request being queued whilst WCF waits for a new IO thread to be made avail­able to han­dle the request.

WCF uses the IO threads from the Thread­Pool to han­dle requests and by default, the Thread­Pool keeps one IO thread around for each CPU. So on a sin­gle core machine that means you only have ONE avail­able IO thread to start with, and when more IO threads are needed they’re cre­ated by the Thread­Pool with a delay:

The thread pool main­tains a min­i­mum num­ber of idle threads. For worker threads, the default value of this min­i­mum is the num­ber of proces­sors. The Get­MinThreads method obtains the min­i­mum num­bers of idle worker and I/O com­ple­tion threads.

When all thread pool threads have been assigned to tasks, the thread pool does not imme­di­ately begin cre­at­ing new idle threads. To avoid unnec­es­sar­ily allo­cat­ing stack space for threads, it cre­ates new idle threads at inter­vals. The inter­val is cur­rently half a sec­ond, although it could change in future ver­sions of the .NET Framework.

If an appli­ca­tion is sub­ject to bursts of activ­ity in which large num­bers of thread pool tasks are queued, use the Set­MinThreads method to increase the min­i­mum num­ber of idle threads. Oth­er­wise, the built-in delay in cre­at­ing new idle threads could cause a bottleneck.”

How­ever, as Wen­Long Dong pointed out in his blog (see ref­er­ences sec­tion), rais­ing the Min­IO­Threads set­ting in the Thread­Pool doesn’t work as you’d expect in .Net 3.5 because of a known issue with the Thread­Pool which has since been fixed in .Net 4. So if you’re still run­ning .Net 3.5 like most of us, then you will need to go and grab the hot­fix from here:

http://support.microsoft.com/kb/976898

Part­ing thoughts:

Per­for­mance tun­ing isn’t an exact sci­ence, and you have to make a case by case judge­ment on how best to approach your per­for­mance issues. Encour­ag­ing greater con­cur­rency is just one of the ways you can improve per­for­mance, but it’s by no means a sil­ver bul­let! In fact, if you go too far down the con­cur­rency route you could find your­self fac­ing a num­ber of problems:

  • 100% CPU – exces­sive num­ber of con­cur­rent threads can max out your CPU and a lot of CPU time can be wasted on con­text switch­ing whilst your ser­vice becomes less responsive.
  • DOS attack – same as above, but intended by a would be attacker.
  • Out­OfMem­o­ryEx­cep­tion – if your ser­vice is return­ing a large set of data from database/committing large amount of data­base writes within a trans­ac­tion, it’s not unthink­able that you might run into the dreaded Out­OfMem­o­ryEx­cep­tion given that: 1) in prac­tice you only have a per process cap of around 1.2 ~ 1.5GB of mem­ory and each active thread is allo­cated a 1MB of mem­ory space (regard­less of how much it actu­ally uses); 2) each con­cur­rent call per­forms large num­ber of object cre­ations which takes up avail­able mem­ory space until they’re garbage col­lected 3) writ­ing to the Data­base within a trans­ac­tion adds to the trans­ac­tion log which also eats into the avail­able memory.

Ref­er­ences:

Wen­Long Dong’s post on WCF responses being slow and Set­MinThreads does not work

Wen­Long Dong’s post on WCF request throt­tling and Server Scalability

Wen­Long Dong’s post on WCF becom­ing slow after being idle for 15 seconds

Scott Weinstein’s post on cre­at­ing high per­for­mance WCF services

Dan Rigsby’s post on throt­tling WCF ser­vice and main­tain­ing scalability

Share

Default con­text = unchecked

By default, arith­metic oper­a­tions and con­ver­sions in C# are exe­cuted in an unchecked con­text. This means that for a signed inte­ger it over­flows from int.MaxValue to int.MinValue and under­flows from int.MinValue to int.MaxValue, hence both state­ments below eval­u­ates to true:

(int.MinValue – 1) == int.MaxValue;
(int.MaxValue + 1) == int.MinValue;

Sim­i­larly, for an unsigned inte­ger it will under­flow from 0 to uint.MaxValue and over­flow from uint.MaxValue to 0:

(0 – 1) == uint.MaxValue; // uint.MinValue = 0
(uint.MaxValue + 1) == 0;

This also applies to con­ver­sions too:

(int)((long) int.MaxValue + 1) == int.MinValue; // true

Pit­falls

This default behav­iour of swal­low­ing the excep­tions seems rather strange and unex­pected to me, given that when an over­flow hap­pens it’s usu­ally of inter­est to me as a devel­oper to know about it and deal with it as appro­pri­ate because:

  • if an over­flow is to hap­pen dur­ing nor­mal usage of the appli­ca­tion it prob­a­bly means there’s some­thing wrong with the design/assumptions of the applicatoin
  • if an over­flow is to hap­pen and the appli­ca­tion is allowed to con­tinue and per­sist the overflowed/underflowed value it could mean impor­tant pieces of data are left in an invalid state which is difficult/impossible to revert
  • the jump is large! Imag­ine see­ing your account bal­ance go from £2147483647 to £–2147483648 after cred­it­ing £1 into it..

With that said, unchecked con­text per­forms sig­nif­i­cantly bet­ter than checked imple­men­ta­tion, which is prob­a­bly why it was cho­sen as the default.

Also, you will prob­a­bly want to use unchecked blocks for cal­cu­lat­ing hash codes where it’s the bit pat­terns that mat­ters not the mag­ni­tude of the inte­ger value, i.e.:

public class MyClass
{
    …
    public override int GetHashCode()
    {
        unchecked
        {
            return ….
        }
    }
}

Scope

There are two things to keep in mind when using checked/unchecked blocks:

They’re always LOCAL to the method

Which means if you call another method from within a checked block the method will still exe­cute in the default con­text (unchecked):

checked
{
    // this method will still execute in unchecked context
    DoSomethingThatOverflows();
}
...
public void DoSomethingThatOverflows()
{
    // no overflow exception is thrown...
    var overflowed = int.MaxValue + 1;
}

The con­text a line of code exe­cutes in is deter­mined by the MOST inner checked/unchecked state­ment block

Hence the fol­low­ing code will exe­cute in unchecked context:

checked
{
    unchecked
    {
        var over = int.MaxValue + 1;
    }
}

Project-wide arith­metic overflow/underflow checks

If you require arith­metic overflow/underflow check­ing on a project-wide scale, there is a prop­erty you can set from within Visual Stu­dio. Go to project prop­er­ties and find the Build tab, click “Advanced…” and tick the “Check for arith­metic overflow/underflow” box. But remem­ber, you would prob­a­bly still want to make sure GetH­ash­Code is exe­cuted in an unchecked context.

Share