Skip to content

Gendarme.Rules.Performance.AvoidLocalDataStoreSlotRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

AvoidLocalDataStoreSlotRule

Assembly: Gendarme.Rules.Performance
Version: git

Description

This rule warns if a method use LocalDataStoreSlot to store or retrieve data from Thread or Context Local Storage. The faster alternative is to use ThreadStatic or ContextStatic attributes to avoid extra calls and typecasts. Also ThreadStatic is available on Silverlight while the LocalDataStoreSlot API are not.

Examples

Bad example (Thread Local Storage):

static void SetSharedKey (byte[] key)
{
    LocalDataStoreSlot lds = Thread.AllocateNamedDataSlot ("shared-key");
    lds.SetData (key.Clone ());
}
public byte[] SignData (byte[] data)
{
    LocalDataStoreSlot lds = Thread.GetNamedDataSlot ("shared-key");
    using (HMACSHA1 hmac = new HMACSHA1 (Thread.GetData (lds) as byte[])) {
        return hmac.ComputeHash (data);
    }
}

Good example (Thread Local Storage):

[ThreadStatic]
static byte[] shared_key;
static void SetSharedKey (byte[] key)
{
    shared_key = (byte[]) key.Clone ();
}
public byte[] SignData (byte[] data)
{
    using (HMACSHA1 hmac = new HMACSHA1 (shared_key)) {
        return hmac.ComputeHash (data);
    }
}

Notes

  • This rule is available since Gendarme 2.8

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally