I recently took the time to take a look inside the Microsoft.Sharepoint.dll using reflector. I’m not sure I have the right to do that. And I’m pretty sure I don’t have the right to publish any code extracted from it, so I won’t show any.

# Using SPSite(s) and SPWeb(s)

If you do some timing on the SPWeb creation call (SPSite.OpenWeb), you will find out that it’s freaking fast (less then 1 ms on my server). The reason is that the most heavy object, the SPRequest class, is shared among SPWebs of a SPSite. The Dispose call only “Invalidate” the SPWeb, and if this SPWeb is the owner of the SPRequest (which is SPContext.Current.Web object in most of the cases), it releases it.

Personally, I like to have something like that when I use a particular SPWeb along my code in a WebPart:

private SPWeb _rootWeb;
public SPWeb RootWeb {
get {
if ( _rootWeb == null ) {
_rootWeb = SPContext.Current.Site.RootWeb;
if ( _rootWeb != SPContext.Current.Web )
}
return _rootWeb;
}
}

private List<IDisposable> _toDispose = new List<IDisposable>();
foreach( var disp in _toDispose )
disp.Dispose();
}


But the code above won’t make you gain a 1 ms compared to this code (which is shorter and potentially safer, you don’t need to dispose everything) :

var rootWeb = SPContext.Current.Site;
try {
}
finally {
if ( rootWeb != SPContext.Current.Web )
rootWeb.Dispose();
}


If you had to access some more indirect objects, you certainly should keep the code showed earlier. For instance to use a SuperToken Web :

private SPSite _stSite;
SPSite STSite {
get {
if ( _stSite == null ) {
_stSite = new SPSite( SPContext.Current.Site.Url, SPContext.Current.Site.SystemAccount.UserToken );
if ( _stSite != SPContext.Current.Site )
}
return _stSite;
}
}

private SPWeb _stWeb;
SPWeb STWeb {
get {
if ( _stWeb == null ) {
_stWeb = STSite.OpenWeb( SPContext.Current.Web.Url );
if ( _stWeb != SPContext.Current.Web )
}
return _stWeb;
}
}

private List<IDisposable> _toDispose = new List<IDisposable>();
foreach( var disp in _toDispose )
disp.Dispose();
}


Here, opening these new SPSite and SPWeb takes 200 ms on my server. Making sure this only happen once per webpart (or better, per page) can really boost your performances.

# The SPRequest object

Well… I was very disapointed, the SPRequest object references an SPRequestInternalClass from the Microsoft.Sharepoint.Library assembly which only uses interrop COM+ methods. So, it’s basically wrapping COM+ methods. The SPRequest does a lot of exception handling and it keeps track of where (with the stacktrace) it has been created and the size of the “unmanaged stack”.

# Optimization

I like how they did their code. It’s pretty optimized (they even use a bunch of gotos). But sometimes there is some weird things and I don’t know if it’s the compiler’s or the developer’s fault. If you look at the code, they have an SPRequestManager.SPRequestsPerThreadWarning property, which gets (in the Registry, at “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings\LocalSPRequestWarnCount”) the max number of opened SPRequest objects opened before logging it (or 8 if this entry doesn’t exist), that takes twice the amount of code it would normaly require.

# The limit of disassembling

The most frustrating part is there are tons of really important methods that are obfuscated (and that .Net reflector doesn’t disassemble, it could at least give the IL code in comments). I just have :

private static void xxx() {
// This item is obfuscated and can not be translated.
}


For instance, I really would have liked to see how works the SPList.Update() method, but it’s also obfuscated.

# You should explore it too

In the Microsoft.Office.Server.Search assembly, you will also find some pretty interesting things. If you look how works the standard search webparts, well you will be pretty disappointed. The advanced search uses an internal (in the C# meaning) shared object.

Before doing your own webpart, you should take a look at the overridden methods of some sharepoint webparts. It can show you, for instance, how to create your own toolparts. In my last project, I used a stupid text property instead of using a DropDownList in a ToolPart, I was very disappointed with myself when I discovered this.