Gendarme.Rules.Performance Namespace

Classes

TypeReason
AvoidConcatenatingCharsRule This rule will warn you if boxing is used to concatenate a string since this will slow down your code and can be easily avoided. This often happen when concatenating System.String and System.Char values together. However the rule is not limited to check boxing on System.Char since compilers often transform a character into its integer value (e.g. 'a' == 61) and the same boxing issue exists on integers.
AvoidLargeNumberOfLocalVariablesRule This rule warns when the number of local variables exceed a maximum value (default is 64). Having a large amount of local variables makes it hard to generate code that performs well and, likely, makes the code harder to understand.
AvoidLargeStructureRule This rule will fire if a value type (struct in C#) is larger than a maximum value (16 bytes by default). This is a problem because, unlike reference types, value types are bitwise-copied whenever they are assigned to a variable or passed to a method. If the type cannot be reduced in size then it should be turned into a reference type (class in C#).
AvoidLocalDataStoreSlotRule 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.
AvoidMethodWithLargeMaximumStackSize This rule fires if a method has a large maximum stack size (default is 100). Having a large maximum stack size makes it hard to generate code that performs well and, likely, makes the code harder to understand.
AvoidRepetitiveCallsToPropertiesRule The rule warn if virtual, or unlikely to be inline-able, property getters are called several times by a method. In most cases repetitive calls simply requires more time without any gains since the result will always be identical. You should ignore the reported defects if a different value is expected each time the property is called (e.g. calling DateTime.Now).
AvoidRepetitiveCastsRule This rule fires if multiple casts are done on the same value, for the same type. Casts are expensive so reducing them, by changing the logic or caching the result, can help performance.
AvoidReturningArraysOnPropertiesRule This rule check for properties which return arrays. This can be a problem because properties are supposed to execute very quickly so it's likely that this property is returning a reference to the internal state of the object. This means that the caller can change the object's internal state via a back-door channel which is usually a very bad thing and it means that the array's contents may change unexpectedly if the caller holds onto the array. The preferred approach is to either return a read-only collection or to change the property to a method and return a copy of the array (it's important to use a method so that callers are not misled about the performance of the property).
AvoidTypeGetTypeForConstantStringsRule This rule warns when a method use Type.GetType(string) with a constant string. Such calls requires reflection in order to return a Type instance and, for known types, can be replaced with a much faster typeof(x).
AvoidUncalledPrivateCodeRule This rule will check for internally visible methods which are never called. The rule will warn you if a private method isn't called in its declaring type or if an internal method doesn't have any callers in the assembly or isn't invoked by the runtime or a delegate.
AvoidUninstantiatedInternalClassesRule This rule will fire if a type is only visible within its assembly, can be instantiated, but is not instantiated. Such types are often leftover (dead code) or are debugging/testing code and not required. However in some case the types might by needed, e.g. when accessed thru reflection or if the [InternalsVisibleTo] attribute is used on the assembly.
AvoidUnneededCallsOnStringRule This rule detects when some methods, like Clone(), Substring(0), ToString() or ToString(IFormatProvider), are being called on a string instance. Since these calls all return the original string they don't do anything useful and should be carefully reviewed to see if they are working as intended and, if they are, the method call can be removed.
AvoidUnneededFieldInitializationRule This rule looks for constructors that assign fields to their default value (e.g. 0 for an integer, null for an object or a string). Since the CLR zero initializes all values there is no need, under most circumstances, to assign default values. Doing so only adds size to source code and in IL.
AvoidUnneededUnboxingRule This rule checks methods which unbox the same value type multiple times (i.e. the value is copied from the heap into the stack). Because the copy is relatively expensive, the code should be rewritten to minimize unboxes. For example, using a local variable of the right value type should remove the need for more than one unbox instruction per variable.
AvoidUnsealedConcreteAttributesRule This rule fires if an attribute is defined which is both concrete (i.e. not abstract) and unsealed. This is a performance problem because it means that System.Attribute.GetCustomAttribute has to search the attribute type hierarchy for derived types. To fix this either seal the type or make it abstract.
AvoidUnsealedUninheritedInternalTypeRule This rule will fire for classes which are internal to the assembly and have no derived classes, but are not sealed. Sealing the type clarifies the type hierarchy and allows the compiler/JIT to perform optimizations such as eliding virtual method calls.
AvoidUnusedParametersRule This rule is used to ensure that all parameters in a method signature are being used. The rule wont report a defect against the following: [The '' type of list has not been implemented in the ECMA stylesheet.]
AvoidUnusedPrivateFieldsRule This rule checks all private fields inside each type to see if some of them are not being used. This could be a leftover from debugging or testing code or a more serious typo where a wrong field is being used. In any case this makes the type bigger than it needs to be which can affect performance when a large number of instances exist.
CompareWithEmptyStringEfficientlyRule This rule will fire if a string is compared to "" or String.Empty. Instead use a String.Length test which should be a bit faster. Another possibility (with .NET 2.0) is to use the static String.IsNullOrEmpty method. String.IsNullOrEmpty.
ConsiderCustomAccessorsForNonVisibleEventsRule This rule looks for non-visible events to see if their add/remove accessors are the default ones. The default, compiler generated, accessor is marked as synchronized which means that the runtime will bracket them between Monitor.Enter and Monitor.Exit calls. This is the safest approach unless, for non-visible events, you have a performance bottleneck around the events. In this case you should review if your code needs the locks or if you can provide an alternative to them.
DoNotIgnoreMethodResultRule This rule fires if a method is called that returns a new instance but that instance is not used. This is a performance problem because it is wasteful to create and collect objects which are never actually used. It may also indicate a logic problem. Note that this rule currently only checks methods within a small number of System types.
ImplementEqualsTypeRule This rule looks for types that override Object.Equals(object) but do not provide a Equals(x) overload using the type. Such an overload removes the need to cast the object to the correct type. For value types this also removes the costly boxing operations. Assemblies targeting .NET 2.0 (and later) should also implement System.IEquatable<T>.
MathMinMaxCandidateRule This rule checks methods for code which seems to duplicate Math.Min or Math.Max. The JIT can inline these methods and generate better code for, at least some types, than it can for a custom inline implementation.
OverrideValueTypeDefaultsRule This rule checks all value types, except enumerations, to see if they use the default implementation of Equals(object) and GetHashCode() methods. While ValueType implementations work for any value type they do so at the expense of performance (the default implementation uses reflection to access fields). You can easily override both methods with much faster code since you know all meaningful fields inside your structure. At the same time you should also provide, if your language allows it, operator overloads for equality (op_Equality, ==) and inequality (op_Inequality, !=).
PreferCharOverloadRule This rule looks for calls to String methods that use String parameters when a Char parameter could have been used. Using the Char overload is preferred because it will be faster. Note, however, that this may result in subtly different behavior on versions of .NET before 4.0: the string overloads do a culture based comparison using CultureInfo.CurrentCulture and the char methods do an ordinal comparison (a simple compare of the character values). This can result in a change of behavior (for example the two can produce different results when precomposed characters are used). If this is important it is best to use an overload that allows StringComparison or CultureInfo to be explicitly specified see [http://msdn.microsoft.com/en-us/library/ms973919.aspx#stringsinnet20_topic4] for more details. With .NET 4.0 String's behavior will change and the various methods will be made more consistent. In particular the comparison methods will be changed so that they all default to doing an ordinal comparison.
PreferLiteralOverInitOnlyFieldsRule This rule looks for InitOnly fields (readonly in C#) that could be turned into Literal (const in C#) because their value is known at compile time. Literal fields don't need to be initialized (i.e. they don't force the compiler to add a static constructor to the type) resulting in less code and the value (not a reference to the field) will be directly used in the IL (which is OK if the field has internal visibility, but is often problematic if the field is visible outside the assembly).
RemoveUnneededFinalizerRule This rule looks for types that have an empty finalizer (a.k.a. destructor in C# or Finalize method). Finalizers that simply set fields to null are considered to be empty because this does not help the garbage collection. You should remove the empty finalizer to alleviate pressure on the garbage collector and finalizer thread.
RemoveUnusedLocalVariablesRule This rule looks for unused local variables inside methods. This can leads to larger code (IL) size and longer JIT time, but note that some optimizing compilers can remove the locals so they won't be reported even if you can still see them in the source code. This could also be a typo in the source were a value is assigned to the wrong variable.
ReviewLinqMethodRule Linq extension methods operate on sequences of values so they generally have linear time complexity. However you may be able to achieve better than linear time performance if you use a less general method or take advantage of a method provided by an Sytem.Collections.Generic.IEnumerable<T> subclass.
UseIsOperatorRule This rule looks for complex cast operations (e.g. a as with a null check) that can be simplified using the is operator (C# syntax). Note: in some case a compiler, like [g]mcs, can optimize the code and generate IL identical to a is operator. In this case the rule will not report an error even if you could see one while looking the at source code.
UseStringEmptyRule This rule checks for methods that are using the literal "" instead of the String.Empty field. You'll get slighly better performance by using String.Empty. Note that in some cases, e.g. in a switch/case statement, you cannot use a field, so "" must be used instead of String.Empty.
UseSuppressFinalizeOnIDisposableTypeWithFinalizerRule This rule will fire if a type implements System.IDisposable and has a finalizer (called a destructor in C#), but the Dispose method does not call System.GC.SuppressFinalize. Failing to do this should not cause properly written code to fail, but it does place a non-trivial amount of extra pressure on the garbage collector and on the finalizer thread.
UseTypeEmptyTypesRule This rule fires if a zero length array of System.Type is created. This value is so often required by the framework API that the System.Type includes an EmptyTypes field. Using this field avoids the memory allocation (and GC tracking) of your own array.