-
Notifications
You must be signed in to change notification settings - Fork 4
Gendarme.Rules.Performance.AvoidLocalDataStoreSlotRule(git)
Sebastien Pouliot edited this page Mar 2, 2011
·
1 revision
Assembly: Gendarme.Rules.Performance
Version: git
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.
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);
}
}
- This rule is available since Gendarme 2.8
You can browse the latest source code of this rule on github.com
Note that this page was autogenerated (3/17/2011 1:55:44 PM) based on the xmldoc
comments inside the rules source code and cannot be edited from this wiki.
Please report any documentation errors, typos or suggestions to the
Gendarme Mailing List. Thanks!