{"id":386433,"date":"2024-06-29T06:42:00","date_gmt":"2024-06-29T06:42:00","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=386433"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T06:42:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=386433","title":{"rendered":"<span>Optimization of .NET applications: a big result of small edits<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/4f4\/53f\/118\/4f453f1183ece492558bec6218169e91.png\" alt=\"0852_NETAppsPerf_MinChangesMajorRes\/image1.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/4f4\/53f\/118\/4f453f1183ece492558bec6218169e91.png\"\/><\/p>\n<p>  <\/p>\n<p>Today we&#8217;re going to discuss how small optimizations in the right places of the application can improve its performance. Imagine: we remove the creation of an extra iterator in one place, get rid of boxing in the other. As a result, we get drastic improvements because of such small edits.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>One old and simple idea runs like a golden thread through the article. Please, remember it. <\/p>\n<p>  <\/p>\n<p>Premature optimizations are evil.<\/p>\n<p>  <\/p>\n<p>Sometimes it happens that optimization and readability go in slightly different directions. Code may work better, but it is harder to read and maintain. And vice versa \u2014 code is easy to read and modify but has some performance problems. Therefore, it is important to understand what we are willing to sacrifice in such cases.<\/p>\n<p>  <\/p>\n<p>A developer may read the article, rush to edit a project&#8217;s code base and\u2026 get no performance improvements. And code becomes more complex.<\/p>\n<p>  <\/p>\n<p>That&#8217;s why it is important to (always) approach the case with a cool head. It is great if you know your application&#8217;s bottlenecks where optimization can help. Otherwise, various profilers are here to the rescue. They can provide a large amount of information about the application. In particular, describe its behavior in dynamics. For example, instances of what types are created the most frequently, how much time the application spends on garbage collection, how long a particular code fragment is executed etc. Two JetBrains tools are worth mentioning: <a href=\"https:\/\/www.jetbrains.com\/profiler\/\">dotTrace<\/a> and <a href=\"https:\/\/www.jetbrains.com\/dotmemory\/\">dotMemory<\/a>. They are convenient and collect a lot of information. Besides, it is perfectly visualized. JetBrains, you&#8217;re cool!<\/p>\n<p>  <\/p>\n<p>But let&#8217;s get back to the optimizations. Throughout the article we&#8217;ll analyze several cases that we have faced and that seemed the most interesting. Each of described edits gave positive result, as it was made in the bottlenecks marked by the profilers. Unfortunately, I did not record the results of each change I made, but I&#8217;ll show the general optimization result at the end of the article.<\/p>\n<p>  <\/p>\n<p><strong>Note<\/strong>: This article is about working with .NET Framework. As experience shows (see the example with <a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/csharp\/0844\/\">Enum.GetHashCode<\/a>), sometimes the same C# code fragment may demonstrate a more optimal performance on .NET Core \/ .NET than on .NET Framework.<\/p>\n<p>  <\/p>\n<h2 id=\"and-what-in-fact-are-we-optimizing\">And what, in fact, are we optimizing?<\/h2>\n<p>  <\/p>\n<p>The tips, described in the article, are relevant to every .NET application. Again, the edits are the most useful when done in the bottlenecks.<\/p>\n<p>  <\/p>\n<p>Beware that we are not going to delve into any abstract theoretical reasoning. In this context, the &#171;change the code to avoid the creation of one iterator&#187; type of tips would look bizarre. All the problems listed in this article were identified after I profiled the <a href=\"https:\/\/pvs-studio.com\/en\/pvs-studio\/\">PVS-Studio static analyzer<\/a> for C#. The main purpose of profiling was to reduce the analysis time.<\/p>\n<p>  <\/p>\n<p>After the work started, it quickly became clear that the analyzer had serious problems with garbage collection. It took a significant amount of time. In fact, we&#8217;d known this before, just made sure of it once again. By the way, earlier we had done several optimizations of the analyzer and we have <a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/csharp\/0836\/\">a separate article<\/a> on that.<\/p>\n<p>  <\/p>\n<p>However, the problem was still relevant.<\/p>\n<p>  <\/p>\n<p>Look at the screenshot below (<a href=\"https:\/\/import.viva64.com\/docx\/blog\/0852_NETAppsPerf_MinChangesMajorRes\/image3.png\">full-size image is here<\/a>). This is the result I got after profiling the PVS-Studio C#. It used 8 threads \u2014 8 lines in the screenshot. The garbage collection clearly took a significant time in each thread.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/62b\/d51\/acb\/62bd51acb8b5ee38f28f452c3f96dde0.png\" alt=\"0852_NETAppsPerf_MinChangesMajorRes\/image3.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/62b\/d51\/acb\/62bd51acb8b5ee38f28f452c3f96dde0.png\"\/><\/p>\n<p>  <\/p>\n<p>We rejected &#171;rewrite everything in C&#187; advice and got to work. In particular, we examined the results of the profiling and locally deleted unnecessary extra\/temporary objects. To our luck, this approach immediately showed results.<\/p>\n<p>  <\/p>\n<p>This is going to be the main topic of the article.<\/p>\n<p>  <\/p>\n<p>What did we gain? Let&#8217;s keep the intrigue until the end of the article.<\/p>\n<p>  <\/p>\n<h2 id=\"calling-methods-with-a-params-parameter\">Calling methods with a params parameter<\/h2>\n<p>  <\/p>\n<p>Methods with a <em>params<\/em> parameter declared in their signature, can take the following as an argument:<\/p>\n<p>  <\/p>\n<ul>\n<li>no values;<\/li>\n<li>one or more values.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>For example, here is a method with a signature like this:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static void ParamsMethodExample(params String[] stringValue)<\/code><\/pre>\n<p>  <\/p>\n<p>Let&#8217;s look at its IL code:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">.method private hidebysig static void   ParamsMethodExample(string[] stringValue) cil managed {   .param [1]   .custom instance void    [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 )    .... }<\/code><\/pre>\n<p>  <\/p>\n<p>This is a simple method with one parameter, marked with the <em>System.ParamArrayAttribute<\/em>. The strings array is specified as the parameter type.<\/p>\n<p>  <\/p>\n<p><strong>Fun fact<\/strong>. The compiler issues the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/misc\/cs0674\">CS0674<\/a> error and forces you to use the <em>params<\/em> keyword \u2014 no way to use this attribute directly.<\/p>\n<p>  <\/p>\n<p>A very simple conclusion follows from the IL code. Each time we need to call this method, the caller code has to create an array. Well, almost.<\/p>\n<p>  <\/p>\n<p>Let&#8217;s look at the following examples to better understand what&#8217;s happening when you call this method with various arguments.<\/p>\n<p>  <\/p>\n<p>The first call is without arguments.<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">ParamsMethodExample()<\/code><\/pre>\n<p>  <\/p>\n<p>IL code:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">call       !!0[] [mscorlib]System.Array::Empty&lt;string>() call       void Optimizations.Program::ParamsMethodExample(string[])<\/code><\/pre>\n<p>  <\/p>\n<p>The method expects an array as input, so we need to get it somewhere. In this case we use a result of calling the static <em>System.Array.Empty&lt;T><\/em> method as an argument. This allows us to avoid creating empty collections and reduce the pressure on GC<em>.<\/em><\/p>\n<p>  <\/p>\n<p>And now the sad part. Older versions of the compiler can generate different IL code. Like this:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ldc.i4.0 newarr     [mscorlib]System.String call       void Optimizations.Program::ParamsMethodExample(string[])<\/code><\/pre>\n<p>  <\/p>\n<p>In this case a new empty array is created every time we call a method that has no corresponding argument for the <em>params<\/em> parameter.<\/p>\n<p>  <\/p>\n<p>Time to test yourself. Do the following calls differ? If so, in what way?<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">ParamsMethodExample(null);  ParamsMethodExample(String.Empty);<\/code><\/pre>\n<p>  <\/p>\n<p>Found an answer? Let&#8217;s figure it out.<\/p>\n<p>  <\/p>\n<p>Let&#8217;s start with the call when the argument is an explicit <em>null<\/em>:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">ParamsMethodExample(null);<\/code><\/pre>\n<p>  <\/p>\n<p>IL code:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ldnull call       void Optimizations.Program::ParamsMethodExample(string[])<\/code><\/pre>\n<p>  <\/p>\n<p>The array is not created in this case. The method takes <em>null<\/em> as an argument.<\/p>\n<p>  <\/p>\n<p>Let&#8217;s look at the case when we pass a non-null value to the method:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">ParamsMethodExample(String.Empty);<\/code><\/pre>\n<p>  <\/p>\n<p>IL code:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">ldc.i4.1 newarr     [mscorlib]System.String dup ldc.i4.0 ldsfld     string [mscorlib]System.String::Empty stelem.ref call       void Optimizations.Program::ParamsMethodExample(string[])<\/code><\/pre>\n<p>  <\/p>\n<p>Here the code is already longer than in the previous example. An array gets created before calling the method. All the arguments, that had been passed to the method&#8217;s <em>params<\/em> parameter, get into the array. In this case an empty string is written to the array.<\/p>\n<p>  <\/p>\n<p>Note that an array is also created if there are several arguments. It&#8217;s created even if the arguments are explicit <em>null<\/em> values.<\/p>\n<p>  <\/p>\n<p>Thus, calling methods with the <em>params<\/em> parameters can play a trick on you if you do not expect the implicit array creation. In some cases the compiler can optimize the method call \u2014 remove the creation of an extra array. But overall, remember about temporary objects.<\/p>\n<p>  <\/p>\n<p>The profiler spotted several places where many arrays were created and collected by GC.<\/p>\n<p>  <\/p>\n<p>In the corresponding methods, the code looked approximately like this:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">bool isLoop = node.IsKindEqual(SyntaxKind.ForStatement,                                SyntaxKind.ForEachStatement,                                SyntaxKind.DoStatement,                                SyntaxKind.WhileStatement);<\/code><\/pre>\n<p>  <\/p>\n<p>The <em>IsKindEqual<\/em> method looked like this:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">public static bool IsKindEqual(this SyntaxNode node, params SyntaxKind[] kinds) {   return kinds.Any(kind => node.IsKind(kind)); }<\/code><\/pre>\n<p>  <\/p>\n<p>We need to create an array to call the method. After we traverse the array, it becomes unnecessary.<\/p>\n<p>  <\/p>\n<p>Can we get rid of creating unnecessary arrays? Easy:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">bool isLoop =    node.IsKind(SyntaxKind.ForStatement)               || node.IsKind(SyntaxKind.ForEachStatement)               || node.IsKind(SyntaxKind.DoStatement)               || node.IsKind(SyntaxKind.WhileStatement);<\/code><\/pre>\n<p>  <\/p>\n<p>This edit reduced the number of the temporary arrays needed \u2014 and eased the pressure on GC.<\/p>\n<p>  <\/p>\n<p><strong>Note<\/strong>: Sometimes .NET libraries use a clever trick. Some methods with <em>params<\/em> parameters have overloads that take 1,2,3 parameters of the corresponding type instead of the <em>params<\/em> parameter. This trick helps avoid creating temporary arrays from the caller side.<\/p>\n<p>  <\/p>\n<h2 id=\"enumerableanyt\">Enumerable.Any&lt;T><\/h2>\n<p>  <\/p>\n<p>We have seen many times the <em>Any<\/em> method call in the profiling results. What&#8217;s wrong with it? Let&#8217;s look at the real code: the <em>IsKindEqual<\/em> method we&#8217;ve mentioned earlier. Previously we placed more emphasis on the <em>params<\/em> parameter. Now let&#8217;s take a closer look at the method&#8217;s code from the inside. <\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">public static bool IsKindEqual(this SyntaxNode node, params SyntaxKind[] kinds) {   return kinds.Any(kind => node.IsKind(kind)); }<\/code><\/pre>\n<p>  <\/p>\n<p>To understand what the problem is with <em>Any<\/em>, we&#8217;ll look &#171;under the hood&#187; of the method. We take the source code from our beloved <a href=\"https:\/\/bit.ly\/36vRkKo\">referencesource.microsoft.com<\/a><\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">public static bool Any&lt;TSource>(this IEnumerable&lt;TSource> source,                                  Func&lt;TSource, bool> predicate)  {   if (source == null)      throw Error.ArgumentNull(\"source\");    if (predicate == null)      throw Error.ArgumentNull(\"predicate\");    foreach (TSource element in source)    {     if (predicate(element))        return true;   }    return false; }<\/code><\/pre>\n<p>  <\/p>\n<p>The <em>foreach<\/em> loopiterates through the original collection<em>.<\/em> If the <em>predicate<\/em> call has returned the <em>true<\/em> value for at least one element, then the result of the method&#8217;s work is <em>true<\/em>. Otherwise, it&#8217;s <em>false<\/em>.<\/p>\n<p>  <\/p>\n<p>The main problem is that any input collection is actually interpreted as <em>IEnumerable&lt;T><\/em>. Any optimizations for specific types of collections don&#8217;t exist. A little reminder that we are working with an array in this case.<\/p>\n<p>  <\/p>\n<p>You may have already guessed that the main problem with <em>Any<\/em> is that it creates an excess iterator to traverse the collection. If you got a little lost \u2014 don&#8217;t worry, we&#8217;re going to figure it out.<\/p>\n<p>  <\/p>\n<p>Let&#8217;s cut off the extra fragments of the <em>Any<\/em> method and simplify it. However, we&#8217;ll save the necessary code: the <em>foreach<\/em> loop and the declaration of collection, with which the loop works.<\/p>\n<p>  <\/p>\n<p>Let&#8217;s look at the following code:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static void ForeachTest(IEnumerable&lt;String> collection) {   foreach (var item in collection)     Console.WriteLine(item); }<\/code><\/pre>\n<p>  <\/p>\n<p>The IL code:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">.method private hidebysig static void   ForeachTest(   class    [mscorlib]System.Collections.Generic.IEnumerable`1&lt;string> collection)  cil managed {   .maxstack  1   .locals init (     [0] class          [mscorlib]System.Collections.Generic.IEnumerator`1&lt;string> V_0)    IL_0000:  ldarg.0   IL_0001:  callvirt   instance class      [mscorlib]System.Collections.Generic.IEnumerator`1&lt;!0> class      [mscorlib]System.Collections.Generic.IEnumerable`1&lt;string>::GetEnumerator()    IL_0006:  stloc.0   .try   {     IL_0007:  br.s       IL_0014      IL_0009:  ldloc.0     IL_000a:  callvirt   instance !0 class        [mscorlib]System.Collections.Generic.IEnumerator`1&lt;string>::get_Current()      IL_000f:  call       void [mscorlib]System.Console::WriteLine(string)      IL_0014:  ldloc.0     IL_0015:  callvirt   instance bool        [mscorlib]System.Collections.IEnumerator::MoveNext()      IL_001a:  brtrue.s   IL_0009     IL_001c:  leave.s    IL_0028   }   finally   {     IL_001e:  ldloc.0     IL_001f:  brfalse.s  IL_0027      IL_0021:  ldloc.0     IL_0022:  callvirt   instance void        [mscorlib]System.IDisposable::Dispose()      IL_0027:  endfinally   }   IL_0028:  ret }<\/code><\/pre>\n<p>  <\/p>\n<p>See, a lot of things are happening here. Since the compiler doesn&#8217;t know anything about the actual collection type, it has generated general-purpose code to iterate through the collection. The iterator is obtained by calling the<em> GetEnumerator<\/em> method (the IL_0001 label). If we obtain the iterator via the <em>GetEnumerator <\/em>method call, it will be created on the heap. All further interaction with the collection is based on the use of this object.<\/p>\n<p>  <\/p>\n<p>The compiler can use a special optimization while obtaining an iterator for an empty array. In this case, the <em>GetEnumerator<\/em> call doesn&#8217;t create a new object. This topic deserves a separate note. In general case, don&#8217;t count on this optimization. <\/p>\n<p>  <\/p>\n<p>Now let&#8217;s change the code a little, so the compiler knows that we&#8217;re working with the array. <\/p>\n<p>  <\/p>\n<p>C# code:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static void ForeachTest(String[] collection) {   foreach (var item in collection)     Console.WriteLine(item); }<\/code><\/pre>\n<p>  <\/p>\n<p>The corresponding IL code:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">.method private hidebysig static void   ForeachTest(string[] collection) cil managed {   \/\/ Code size       25 (0x19)   .maxstack  2   .locals init ([0] string[] V_0,                 [1] int32 V_1)   IL_0000:  ldarg.0   IL_0001:  stloc.0   IL_0002:  ldc.i4.0   IL_0003:  stloc.1   IL_0004:  br.s       IL_0012   IL_0006:  ldloc.0   IL_0007:  ldloc.1   IL_0008:  ldelem.ref   IL_0009:  call       void [mscorlib]System.Console::WriteLine(string)   IL_000e:  ldloc.1   IL_000f:  ldc.i4.1   IL_0010:  add   IL_0011:  stloc.1   IL_0012:  ldloc.1   IL_0013:  ldloc.0   IL_0014:  ldlen   IL_0015:  conv.i4   IL_0016:  blt.s      IL_0006   IL_0018:  ret }<\/code><\/pre>\n<p>  <\/p>\n<p>The compiler generated simpler code since it knows the collection type we&#8217;re working with. Besides, all the work with the iterator has disappeared \u2014 the object is not even created. We reduce the pressure on GC.<\/p>\n<p>  <\/p>\n<p>By the way, here&#8217;s a &#171;check yourself&#187; question. If we restore C# code from this IL code, what kind of a language construction do we get? The code is obviously different from what was generated for the <em>foreach<\/em> loop earlier.<\/p>\n<p>  <\/p>\n<p>Here&#8217;s the answer.<\/p>\n<p>  <\/p>\n<p>Below is the method in C#. The compiler will generate the same IL code as the one above, except for the names:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static void ForeachTest2(String[] collection) {   String[] localArr;   int i;    localArr = collection;    for (i = 0; i &lt; localArr.Length; ++i)     Console.WriteLine(localArr[i]); }<\/code><\/pre>\n<p>  <\/p>\n<p>If the compiler knows that we&#8217;re working with an array, it generates more optimal code by representing the <em>foreach<\/em> loop as the <em>for <\/em>loop.<\/p>\n<p>  <\/p>\n<p>Unfortunately, we lose such optimizations when working with <em>Any<\/em>. Besides, we create an excess iterator to traverse the sequence.<\/p>\n<p>  <\/p>\n<h2 id=\"lambda-expressions\">Lambda expressions<\/h2>\n<p>  <\/p>\n<p>Lambdas are highly convenient things that make the developer&#8217;s life so much easier. Until someone tries to put a lambda inside a lambda inside a lambda\u2026 Fans of doing that \u2014 please rethink this, seriously.<\/p>\n<p>  <\/p>\n<p>In general, the use of lambda expressions eases the developer&#8217;s life. But don&#8217;t forget that there are entire classes &#171;under the hood&#187; of a lambdas. This means, the instances of those classes still need to be created when your application uses lambdas.<\/p>\n<p>  <\/p>\n<p>Let&#8217;s get back to the <em>IsKindEqual<\/em> method.<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">public static bool IsKindEqual(this SyntaxNode node, params SyntaxKind[] kinds) {   return kinds.Any(kind => node.IsKind(kind)); }<\/code><\/pre>\n<p>  <\/p>\n<p>Now let&#8217;s look at the corresponding IL code:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">.method public hidebysig static bool   IsKindEqual(   class    [Microsoft.CodeAnalysis]Microsoft.CodeAnalysis.SyntaxNode      node,   valuetype    [Microsoft.CodeAnalysis.CSharp]Microsoft.CodeAnalysis.CSharp.SyntaxKind[]      kinds) cil managed {   .custom instance void      [mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::       .ctor() = ( 01 00 00 00 )    .param [2]   .custom instance void      [mscorlib]System.ParamArrayAttribute::       .ctor() = ( 01 00 00 00 )    \/\/ Code size       32 (0x20)   .maxstack  3   .locals init      (class OptimizationsAnalyzer.SyntaxNodeUtils\/'&lt;>c__DisplayClass0_0' V_0)   IL_0000:  newobj     instance void      OptimizationsAnalyzer.SyntaxNodeUtils\/'&lt;>c__DisplayClass0_0'::.ctor()   IL_0005:  stloc.0   IL_0006:  ldloc.0   IL_0007:  ldarg.0   IL_0008:  stfld           class [Microsoft.CodeAnalysis]Microsoft.CodeAnalysis.SyntaxNode      OptimizationsAnalyzer.SyntaxNodeUtils\/'&lt;>c__DisplayClass0_0'::node   IL_000d:  ldarg.1   IL_000e:  ldloc.0   IL_000f:  ldftn      instance bool      OptimizationsAnalyzer.SyntaxNodeUtils\/'&lt;>c__DisplayClass0_0'       ::'&lt;IsKindEqual>b__0'(         valuetype [Microsoft.CodeAnalysis.CSharp]Microsoft.CodeAnalysis                                                           .CSharp.SyntaxKind)   IL_0015:  newobj     instance void      class [mscorlib]System.Func`2&lt;       valuetype [Microsoft.CodeAnalysis.CSharp]       Microsoft.CodeAnalysis.CSharp.SyntaxKind,bool>::.ctor(         object, native int)   IL_001a:  call       bool      [System.Core]System.Linq.Enumerable::Any&lt;       valuetype [Microsoft.CodeAnalysis.CSharp]Microsoft.CodeAnalysis                                                         .CSharp.SyntaxKind>(          class [mscorlib]System.Collections.Generic.IEnumerable`1&lt;!!0>,          class [mscorlib]System.Func`2&lt;!!0,bool>)   IL_001f:  ret }<\/code><\/pre>\n<p>  <\/p>\n<p>There&#8217;s a little more code here than in C#. Note instructions for creating objects on labels IL_0000 and IL_0015. In the first case, the compiler creates an object of the type that it generated automatically (under the lambda&#8217;s &#171;hood&#187;). The second <em>newobj<\/em> call is the creation of the delegate instance that performs the <em>IsKind<\/em> check.<\/p>\n<p>  <\/p>\n<p>Bear in mind, that in some cases the compiler may apply optimizations and not add the <em>newobj<\/em> instruction to create the generated type instance. Instead, the compiler can, for example, create an object once, write it to a static field and continue working with this field. The compiler behaves this way when there are no captured variables in lambda expressions.<\/p>\n<p>  <\/p>\n<h2 id=\"a-rewritten-iskindequal-variant\">A rewritten IsKindEqual variant<\/h2>\n<p>  <\/p>\n<p>Several temporary objects are created for every <em>IsKindEqual<\/em> call. As experience (and profiling) shows, sometimes this can play a significant role in terms of the pressure on GC.<\/p>\n<p>  <\/p>\n<p>One of the variants is to avoid the method at all. The caller can simply call the <em>IsKind<\/em> method several times. Another option is to rewrite the code.<\/p>\n<p>  <\/p>\n<p>The &#171;before&#187; version looks like this:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">public static bool IsKindEqual(this SyntaxNode node, params SyntaxKind[] kinds) {   return kinds.Any(kind => node.IsKind(kind)); }<\/code><\/pre>\n<p>  <\/p>\n<p>One of the possible &#8216;after&#8217; versions looks like this:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">public static bool IsKindEqual(this SyntaxNode node, params SyntaxKind[] kinds) {   for (int i = 0; i &lt; kinds.Length; ++i)   {     if (node.IsKind(kinds[i]))       return true;   }    return false; }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>Note<\/strong>: You can rewrite the code with <em>foreach<\/em>. When the compiler knows that we are working with the array, it generates IL code of the <em>for<\/em> loop &#8216;under the hood&#8217;.<\/p>\n<p>  <\/p>\n<p>As a result, we got a little more code, but we got rid of the temporary objects creation. We can see this by looking at IL code \u2014 all of the <em>newobj<\/em> instructions have disappeared.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">.method public hidebysig static bool   IsKindEqual(class Optimizations.SyntaxNode node,             valuetype Optimizations.SyntaxKind[] kinds) cil managed {   .custom instance void     [mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::     .ctor() = ( 01 00 00 00 )    .param [2]   .custom instance void      [mscorlib]System.ParamArrayAttribute::     .ctor() = ( 01 00 00 00 )    \/\/ Code size       29 (0x1d)   .maxstack  3   .locals init ([0] int32 i)   IL_0000:  ldc.i4.0   IL_0001:  stloc.0   IL_0002:  br.s       IL_0015   IL_0004:  ldarg.0   IL_0005:  ldarg.1   IL_0006:  ldloc.0   IL_0007:  ldelem.i4   IL_0008:  callvirt   instance bool              Optimizations.SyntaxNode::IsKind(valuetype Optimizations.SyntaxKind)   IL_000d:  brfalse.s  IL_0011   IL_000f:  ldc.i4.1   IL_0010:  ret   IL_0011:  ldloc.0   IL_0012:  ldc.i4.1   IL_0013:  add   IL_0014:  stloc.0   IL_0015:  ldloc.0   IL_0016:  ldarg.1   IL_0017:  ldlen   IL_0018:  conv.i4   IL_0019:  blt.s      IL_0004   IL_001b:  ldc.i4.0   IL_001c:  ret }<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"redefining-base-methods-in-value-types\">Redefining base methods in value types<\/h2>\n<p>  <\/p>\n<p>Sample code:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">enum Origin { } void Foo() {   Origin origin = default;   while (true)   {     var hashCode = origin.GetHashCode();   } }<\/code><\/pre>\n<p>  <\/p>\n<p>Does this code exhibit pressure on GC? Okay-okay, given that the code is in the article, the answer is quite obvious.<\/p>\n<p>  <\/p>\n<p>Believed it? Everything is not that simple. To answer this question, we need to know if the application works on .NET Framework or .NET. By the way, how did the pressure on GC appear here at all? No objects seem to be created on the managed heap.<\/p>\n<p>  <\/p>\n<p>We had to look into IL code and read the specifications to understand the topic. I covered this issue in more detail in <a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/csharp\/0844\/\">a separate article<\/a>.<\/p>\n<p>  <\/p>\n<p>In short, here are some spoilers:<\/p>\n<p>  <\/p>\n<ul>\n<li>Object boxing for the <em>GetHashCode<\/em> method call may take place; <\/li>\n<li>If you want to avoid the boxing, redefine the base methods in the value types.<\/li>\n<\/ul>\n<p>  <\/p>\n<h2 id=\"setting-collections-initial-capacity\">Setting collections&#8217; initial capacity<\/h2>\n<p>  <\/p>\n<p>Some people might say: &#171;Why do we need to set the initial capacity of the collection, everything is already optimized under the &#171;hood&#187;&#187;. Of course, something is optimized (and we&#8217;ll see what exactly). But let&#8217;s talk about the application&#8217;s places where creating almost every object can play a trick on us. Don&#8217;t neglect the opportunity to tell the application the collection size you need.<\/p>\n<p>  <\/p>\n<p>Let&#8217;s talk about why it is useful to set the initial capacity. We&#8217;ll use the <em>List&lt;T><\/em> type as an example. Let&#8217;s say we have the following code:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static List&lt;Variable> CloneExample(IReadOnlyCollection&lt;Variable> variables) {   var list = new List&lt;Variable>();   foreach (var variable in variables)   {     list.Add(variable.Clone());   }    return list; }<\/code><\/pre>\n<p>  <\/p>\n<p>Is it obvious what the problem is with this code? If yes \u2014 congratulations. If no, then let&#8217;s figure it out.<\/p>\n<p>  <\/p>\n<p>We are creating an empty list and gradually filling it. Accordingly, every time the list runs out of capacity, we need:<\/p>\n<p>  <\/p>\n<ul>\n<li>allocate memory for a new array, into which the list elements are added;<\/li>\n<li>copy the elements from the previous list to the new one.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>Where&#8217;s the array coming from? The array is the base of the <em>List&lt;T><\/em> type. Check <a href=\"https:\/\/bit.ly\/3Bfs1KK\">referencesource.microsoft.com<\/a>.<\/p>\n<p>  <\/p>\n<p>Obviously, the larger the size of the <em>variables<\/em> collection, the greater the number of such operations is performed.<\/p>\n<p>  <\/p>\n<p>The list growth algorithm in our case (for .NET Framework 4.8) is 0, 4, 8, 16, 32\u2026 I.e. if the <em>variables<\/em> collection has 257 elements, it requires 8 arrays to be created, and 7 copy operations.<\/p>\n<p>  <\/p>\n<p>You can avoid all of these unnecessary procedures if you set the list capacity at the beginning:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">var list = new List&lt;Variable>(variables.Count);<\/code><\/pre>\n<p>  <\/p>\n<p>Don&#8217;t neglect that opportunity.<\/p>\n<p>  <\/p>\n<h2 id=\"linq-miscellaneous\">LINQ: miscellaneous<\/h2>\n<p>  <\/p>\n<h3 id=\"enumerablecountt\">Enumerable.Count&lt;T><\/h3>\n<p>  <\/p>\n<p>Depending on the overload, the <em>Enumerable.Count<\/em> method can:<\/p>\n<p>  <\/p>\n<ul>\n<li>calculate the number of items in a collection;<\/li>\n<li>calculate the number of elements in the collections that satisfy a predicate.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>Moreover, the method offers several optimizations\u2026 but there&#8217;s a catch.<\/p>\n<p>  <\/p>\n<p>Let&#8217;s look inside the method. We take the source code as usual, from <a href=\"https:\/\/bit.ly\/3xA5LJx\">referencesource.microsoft.com<\/a><\/p>\n<p>  <\/p>\n<p>The version that does not accept the predicate looks like this:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">public static int Count&lt;TSource>(this IEnumerable&lt;TSource> source) {   if (source == null)      throw Error.ArgumentNull(\"source\");    ICollection&lt;TSource> collectionoft = source as ICollection&lt;TSource>;   if (collectionoft != null)      return collectionoft.Count;    ICollection collection = source as ICollection;   if (collection != null)      return collection.Count;    int count = 0;   using (IEnumerator&lt;TSource> e = source.GetEnumerator())    {     checked      {       while (e.MoveNext()) count++;     }   }    return count; }<\/code><\/pre>\n<p>  <\/p>\n<p>And here&#8217;s the version with the predicate:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">public static int Count&lt;TSource>(this IEnumerable&lt;TSource> source,                                   Func&lt;TSource, bool> predicate)  {   if (source == null)      throw Error.ArgumentNull(\"source\");    if (predicate == null)      throw Error.ArgumentNull(\"predicate\");    int count = 0;   foreach (TSource element in source)    {     checked      {       if (predicate(element))          count++;     }   }    return count; }<\/code><\/pre>\n<p>  <\/p>\n<p>Good news: the no-predicate version has an optimization that allows to efficiently calculate the number of elements for collections that implement <em>ICollection<\/em> or <em>ICollection&lt;T><\/em>.<\/p>\n<p>  <\/p>\n<p>However, if a collection does not implement any of these interfaces, the entire collection would be traversed to get the number of elements. This is especially interesting in the predicate method.<\/p>\n<p>  <\/p>\n<p>Suppose we have the following code:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">collection.Count(predicate) > 12;<\/code><\/pre>\n<p>  <\/p>\n<p>And <em>collection<\/em> has 100 000 elements. Get it? In order to check this condition, it would have been enough for us to find 13 elements for which <em>predicate(element)<\/em> would return <em>true<\/em>. Instead, <em>predicate<\/em> is applied to all of the 100 000 elements in the collection. It becomes extremely inconvenient if <em>predicate<\/em> performs some relatively heavy operations.<\/p>\n<p>  <\/p>\n<p>There is a way out \u2013 go ahead and reinvent the wheel. Write your own <em>Count<\/em> analog(s). It&#8217;s up to you to decide which method signature to make (and whether to make them at all). You can write several different methods. Or you can write a method with a tricky signature that would help determine which comparison you need (&#8216;>&#8217;, &#8216;&lt;&#8216;, &#8216;==&#8217; etc). If you have identified <em>Count<\/em>-related bottlenecks, but there are only a couple of them \u2013 just use the <em>foreach<\/em> loop and rewrite them.<\/p>\n<p>  <\/p>\n<h3 id=\"any---count--length\">Any -> Count \/ Length<\/h3>\n<p>  <\/p>\n<p>We have already determined that calling the <em>Any<\/em> method may require one extra iterator. We can avoid the creation of an extra object by using the specific collections&#8217; properties. <em>List&lt;T>.Count<\/em> or <em>Array.Length<\/em>, for example.<\/p>\n<p>  <\/p>\n<p>For example:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static void AnyTest(List&lt;String> values) {   while (true)   {     \/\/ GC     if (values.Any())       \/\/ Do smth      \/\/ No GC     if (values.Count != 0)       \/\/ Do smth   } }<\/code><\/pre>\n<p>  <\/p>\n<p>Such code is less flexible and maybe a little less readable. But at the same time, <em>it might<\/em> help avoid the creating of extra iterator. Yes, <em>it might<\/em>. Because this depends on whether the <em>GetEnumerator<\/em> method returns a new object. When I examined the issue more closely, I found some interesting moments. Maybe I&#8217;ll write an article on them later.<\/p>\n<p>  <\/p>\n<h3 id=\"linq---loops\">LINQ -> loops<\/h3>\n<p>  <\/p>\n<p>As experience shows, in places where each temporary object can slow performance, it makes sense to abandon LINQ in favor of simple loops. We&#8217;ve already talked about it when we reviewed examples with <em>Any<\/em> and <em>Count<\/em>. The same applies to other methods.<\/p>\n<p>  <\/p>\n<p>Example:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">var strings = collection.OfType&lt;String>()                         .Where(str => str.Length > 62);  foreach (var item in strings) {   Console.WriteLine(item); }<\/code><\/pre>\n<p>  <\/p>\n<p>You can rewrite the code above like this:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">foreach (var item in collection) {   if (item is String str &amp;&amp; str.Length > 62)   {     Console.WriteLine(str);   } }<\/code><\/pre>\n<p>  <\/p>\n<p>This is a primitive example where the difference is not very significant. Although there are also cases where LINQ queries are much easier to read than similar code in loops. So, bear in mind that it&#8217;s a dubious idea to just abandon LINQ everywhere.<\/p>\n<p>  <\/p>\n<p><strong>Note<\/strong>: If you have forgotten why LINQ causes the creation of objects on the heap, go through <a href=\"https:\/\/youtu.be\/lVbXyeNp3yI\">this video<\/a> or <a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/csharp\/0836\/\">this article<\/a>.<\/p>\n<p>  <\/p>\n<h3 id=\"buffering-linq-requests\">Buffering LINQ requests<\/h3>\n<p>  <\/p>\n<p>Don&#8217;t forget that every time you traverse a sequence, LINQ queries with deferred calculation are executed all over again.<\/p>\n<p>  <\/p>\n<p>The following example clearly demonstrates this:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static void LINQTest() {   var arr = new int[] { 1, 2, 3, 4, 5 };    var query = arr.Where(AlwaysTrue);    foreach (var item in query) \/\/ 5   { \/* Do nothing *\/}    foreach (var item in query) \/\/ 5   { \/* Do nothing *\/}    foreach (var item in query) \/\/ 5   { \/* Do nothing *\/}    bool AlwaysTrue(int val) => true; }<\/code><\/pre>\n<p>  <\/p>\n<p>In this case the <em>AlwaysTrue<\/em> method is executed 15 times. At the same time if we had buffered the request (added the <em>ToList<\/em> method call to the LINQ call chain), the <em>AlwaysTrue<\/em> method would have been called only 5 times.<\/p>\n<p>  <\/p>\n<h2 id=\"changing-the-garbage-collection-mode\">Changing the garbage collection mode<\/h2>\n<p>  <\/p>\n<p>I have mentioned above that we have already done a number of optimizations in the PVS-Studio C# analyzer. We even wrote an article about that. After publishing it on habr.com, the article prompted a heated discussion <a href=\"https:\/\/habr.com\/en\/company\/pvs-studio\/blog\/562894\/comments\/\">in the comments<\/a>. One of the suggestions was to change the garbage collector settings.<\/p>\n<p>  <\/p>\n<p>Can&#8217;t say we didn&#8217;t know about them. Moreover, when I was doing optimizations and reading a book &#171;Pro .NET Performance: Optimize Your C# Applications&#187;, I also read about the GC settings. But somehow I didn&#8217;t catch on to the fact that changing the garbage collection mode can bring any benefits. My bad.<\/p>\n<p>  <\/p>\n<p>During my vacation, my colleagues did a very cool thing: they took the advice from the comments and decided to try altering the GC working mode. The result was impressive \u2014 the time that PVS-Studio C# required to analyze big projects (like Roslyn) was significantly reduced. At the same time, PVS-Studio used up more memory when analyzing small projects, but that was acceptable. <\/p>\n<p>  <\/p>\n<p>After we altered the GC working mode, the analysis time decreased by 47%. Before, the analysis on this machine took 1 hour and 17 minutes. After \u2014 only 41 minutes.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/d6c\/32b\/3f1\/d6c32b3f18af2803d929eb27b3f168d4.png\" alt=\"0852_NETAppsPerf_MinChangesMajorRes\/image4.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/d6c\/32b\/3f1\/d6c32b3f18af2803d929eb27b3f168d4.png\"\/><\/p>\n<p>  <\/p>\n<p>I was excited to see Roslyn analysis take under 1 hour.<\/p>\n<p>  <\/p>\n<p>We were so pleased with the results that we included the new (server) garbage collection mode in the C# analyzer. This mode will be enabled by default starting from the PVS-Studio 7.14.<\/p>\n<p>  <\/p>\n<p>Sergey Tepliakov described the different garbage collection modes in more detail in <a href=\"https:\/\/devblogs.microsoft.com\/premier-developer\/understanding-different-gc-modes-with-concurrency-visualizer\/\">this article<\/a>.<\/p>\n<p>  <\/p>\n<h2 id=\"pvs-studio-c-analyzer-optimization-results\">PVS-Studio C# analyzer optimization results<\/h2>\n<p>  <\/p>\n<p>We have made a number of other optimizations.<\/p>\n<p>  <\/p>\n<p>For example:<\/p>\n<p>  <\/p>\n<ul>\n<li>we got rid of the bottlenecks in some diagnostics (and <a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/csharp\/0823\/\">rewrote<\/a> one);<\/li>\n<li>we optimized the objects used in data-flow analysis: simplified copying, included additional caching, eliminated temporary objects on the managed heap;<\/li>\n<li>optimized the comparison of tree nodes;<\/li>\n<li>etc.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>We began to add all these optimizations slowly, starting from the PVS-Studio version 7.12. By the way, during this time, we also <a href=\"https:\/\/pvs-studio.com\/en\/docs\/manual\/0010\/\">added<\/a> new diagnostics, .NET 5 support, and taint analysis.<\/p>\n<p>  <\/p>\n<p>For the sake of curiosity, I measured the analysis time of our open-source projects from out tests using PVS-Studio 7.11 and 7.14. I compared the analysis results of the projects that PVS-Studio took the longest to process.<\/p>\n<p>  <\/p>\n<p>On the graph below, you can see the analysis time (in minutes):<\/p>\n<p>  <\/p>\n<ul>\n<li>the Juliet Test Suite project;<\/li>\n<li>the Roslyn project;<\/li>\n<li>the total time of analysis of all projects from the tests.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>The graph itself:<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/4fd\/c4d\/e38\/4fdc4de3802f397335170781b24e0a7e.png\" alt=\"0852_NETAppsPerf_MinChangesMajorRes\/image5.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/4fd\/c4d\/e38\/4fdc4de3802f397335170781b24e0a7e.png\"\/><\/p>\n<p>  <\/p>\n<p>The increase in performance is clearly very significant. So, if you&#8217;re not content with the operating speed of PVS-Studio for C#, come <a href=\"https:\/\/pvs-studio.com\/en-dotnet\">try<\/a> again. By the way, you can get an extended trial version for 30 days \u2013 just follow the link \ud83d\ude42<\/p>\n<p>  <\/p>\n<p>If you encounter any problems \u2014 <a href=\"https:\/\/pvs-studio.com\/en\/about-feedback\/\">contact our support<\/a>, we will sort it out. <\/p>\n<p>  <\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>  <\/p>\n<p>Premature optimization is evil. Long live optimization based on profiling results! And remember that every small change in the right place of a reusable block of code can drastically affect performance.<\/p>\n<p>  <\/p>\n<p>As always, subscribe to <a href=\"https:\/\/twitter.com\/_SergVasiliev_\">my Twitter<\/a> so as not to miss anything interesting.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/572306\/\"> https:\/\/habr.com\/ru\/articles\/572306\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/4f4\/53f\/118\/4f453f1183ece492558bec6218169e91.png\" alt=\"0852_NETAppsPerf_MinChangesMajorRes\/image1.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/4f4\/53f\/118\/4f453f1183ece492558bec6218169e91.png\"\/><\/p>\n<p>  <\/p>\n<p>Today we&#8217;re going to discuss how small optimizations in the right places of the application can improve its performance. Imagine: we remove the creation of an extra iterator in one place, get rid of boxing in the other. As a result, we get drastic improvements because of such small edits.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-386433","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/386433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=386433"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/386433\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=386433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=386433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=386433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}