Optimizing a .NET application: how simple edits made it possible to speed up PVS-Studio and reduce memory consumption by 70%

Performance issues such as abnormally slow performance and high memory consumption can be detected in a variety of ways. Such shortcomings of the application are revealed by tests, by the developers or testers themselves, and in a less successful scenario, by users. Alas, detecting anomalies is only the first step. Further, the problem must be localized, otherwise it will not be possible to solve it. This raises the question - how to find in a large project the reasons leading to excessive memory consumption and slowdowns? Do they exist at all? Perhaps it’s not the application at all? This article is devoted to the story of how the developers of the PVS-Studio C # analyzer faced a similar problem and were able to solve it.





Endless analysis

C#- . – PVS-Studio , , .. , github, .





, , Roslyn. solution 200 , – C#. , , . PVS-Studio Roslyn 1,5-2 . , , , , .





. , ... 3 ! - . , .





, ?!

– ? , ? C#- PVS-Studio ?





! . , . , unit- C#-. , . , . - .





, , . , – .





, - . , , . .





. ? , β€” . , PVS-Studio. - , .





, , . - – .





, . , :). Visual Studio. :





  1. Visual Studio.





  2. File->Open->File ( Ctrl+O).





  3. .





:





. Debug With Managed Only.





. Visual Studio , .





, . β€” , :





  • - , ..;





  • Quick Watch Immediate Window . , File.WriteAllText "Caracteres no vΓ‘lidos en la ruta de acceso!". , , .





. :





  • : 1 500;





  • : 24 ;





  • : 12;





  • : 1060.





. , . , . , , , .





, . - , - . 2 .





, , . , . , . ... !





-,

, , "" . , . .





. – .





, :





  • ;





  • ;





  • .





, ...





. . . , , - . – ?





, . , . ! , , , . , !





– , . . ... .





, . . ?





. , .





, ?

32 . , 16 , – 32 . , 8 .





– ? , !





, . , , . – memory paging ( – "swapping"). (). . , . , .





. , . swapping .





, . , , , . , . .





dotMemory

dotMemory, JetBrains. .NET, Visual Studio, . dotMemory .





:





, "Run". :





. – "Memory Snapshots":





( ). :





dotMemory, , . sunburst , β€” , . "Dominators".





. :





, . , "". , ?





. , , .





(Data-Flow Analysis) . , , null, , . , PVS-Studio, .





, , , . ! , , .





? ?





, , . , 3 . . , - ?





. , PVS-Studio . , , ( ):





void MyFunction(int a, int b, int c ....)
{
  // a = ?
  // b = ?
  // c = ?
  ....
}

      
      



. , , !





– . ...





! , :





  • , ;





  • – ;





  • , .





, , . . .





, . – Flyweight. β€” , .





, , . – : , . C# . string.Intern string.IsInterned. . . , " , String C#".





, Flyweight. ?





! 14,55 4,73 . 68%! . – , , .





, ...





!

, . ! , , , . – . ... ?





dotTrace

, . , – ? ?





dotTrace – .NET , . dotMemory:





. dotMemory, dotTrace . . , , .





, dotTrace, . , CPU:





"" , Start ( ). , "Get Snapshot And Wait". . , :





. . . – View->Snapshot Overview Ctrl+Shift+O.





dotTrace? , -, , C#- . PVS-Studio C# – , , , . , 13β€”15% CPU. , , ?





dotTrace , , ! – ?





, . , – , PVS-Studio "".





, , , . .





, DisplayPart!

, DisplayPart. , , .





, , . C#- DisplayPart ! , Roslyn API.





Roslyn ( .NET Compiler Platform) C#- PVS-Studio. :





  • ;





  • ;





  • ( ) ;





  • ..





Roslyn – . , DisplayPart .





, DisplayPart . , – , . , , -, .. " Roslyn. ". , ( ).





, ToString. , , DisplayPart. , , ( ).





, = 90% . ToString , , , ?





, . ToString .





– . , . , , . , . , .





, , ( ). , PVS-Studio : 2,5 , 2. 20% .





Enumerator

List<T>.Enumerator, . , , . , , ! .





(boxing). object . IEnumerator, .





Enumerator GetEnumerator. , , IEnumerable. , , – IEnumerator. , GetEnumerator ?





! GetEnumerator, List, :





- ? , GetEnumerator:





. , , – . , .





, , Enumerator . - C#- . .





. , GetEnumerator . foreach. " " . foreach List, , foreach, . foreach IEnumerable, , foreach IEnumerator. , GetEnumerator, .





, IEnumerable. , IEnumerable, List.





, – , , IEnumerable, , - . , - .





, LINQ?!

, System.Linq, . . , - Where, Select . . C#- PVS-Studio – .





, LINQ- . , foreach. ?





, IEnumerator. LINQ-. :





List<int> sourceList = ....
var enumeration = sourceList.Where(item => item > 0)
                            .Select(item => someArray[item])
                            .Where(item => item > 0)
                            .Take(5);

      
      



? ! , , System.Linq. github .





Where WhereListIterator – Where-, List ( ). . WhereListIterator , . WhereListIterator , IEnumerator . WhereListIterator , , . , .





Select WhereSelectListIterator. , .





Where Take .





5 . , .





, foreach:





List<int> sourceList = ....
List<int> result = new List<int>();

foreach (var item in sourceList)
{
  if (item > 0)
  {
    var arrayItem = someArray[item];

    if (arrayItem > 0)
    {
      result.Add(arrayItem);

      if (result.Count == 5)
        break;
    }
  }
}

      
      



foreach LINQ.





  • LINQ-:





    • , ;





    • ;





    • ;





    • , , .





  • LINQ-:





    • : 5 , β€” 1 ( result);





    • . , , . , ToList, LINQ- ( ).





, LINQ- . , . , , , .





, . LINQ- - ToList, ( ).





. . , , yield. , . yield C# ( , ) " yield C#?".





, , foreach LINQ-. .





?

!

PVS-Studio ! , ( 20%, 70%!). , ! , , PVS-Studio.





, . . , , PVS-Studio . , . dotMemory dotPeek, , . , , .





, , ... , , . PVS-Studio . – . C#- , , PVS-Studio. , - , , , .





, , . " " ... ! , , . – Incredibuild. , C++ , - .





, , LINQ- - , . - "always true"-, , , – , .





IDE . , , - . . , . , , .





PVS-Studio – . , . , , , :). ! . !





, : Nikita Lipilin. .NET Application Optimization: Simple Edits Speeded Up PVS-Studio and Reduced Memory Consumption by 70%.








All Articles