performance 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. * Writing better performing .NET and Mono applications
  2. <center>
  3. Miguel de Icaza ([email protected])<br>
  4. Ben Maurer ([email protected])
  5. </center>
  6. The following document contains a few hints on how to improve
  7. the performance of your Mono/.NET applications.
  8. These are just guidelines, and you should still profile your
  9. code to find the actual performance problems in your
  10. application. It is never a smart idea to make a change with the
  11. hopes of improving the performance of your code without first
  12. measuring. In general, these guidelines should serve as ideas
  13. to help you figure out `how can I make this method run faster'.
  14. It is up to you to figure out, `Which method is running slowly.'
  15. ** Using the Mono profiler
  16. So, how does one measure what method are running slowly? A profiler
  17. helps with this task. Mono includes a profiler that is built
  18. into the runtime system. You can invoke this profiler on your program
  19. by running with the --profile flag.
  20. <pre class="shell">
  21. mono --profile program.exe
  22. </pre>
  23. The above will instruct Mono to instrument your application
  24. for profiling. The default Mono profiler will record the time
  25. spent on a routine, the number of times the routine called,
  26. the memory consumed by each method broken down by invoker, and
  27. the total amount of memory consumed.
  28. It does this by asking the JIT to insert a call to the profiler
  29. every time a method is entered or left. The profiler times the
  30. amount of time elapsed between the beginning and the end of the
  31. call. The profiler is also notified of allocations.
  32. When the program has finished executing, the profiler prints the
  33. data in human readable format. It looks like:
  34. <pre class="shell">
  35. Total time spent compiling 227 methods (sec): 0.07154
  36. Slowest method to compile (sec): 0.01893: System.Console::.cctor()
  37. Time(ms) Count P/call(ms) Method name
  38. ########################
  39. 91.681 1 91.681 .DebugOne::Main()
  40. Callers (with count) that contribute at least for 1%:
  41. 1 100 % .DebugOne::Main(object,intptr,intptr)
  42. ...
  43. Total number of calls: 3741
  44. ...
  45. Allocation profiler
  46. Total mem Method
  47. ########################
  48. 406 KB .DebugOne::Main()
  49. 406 KB 1000 System.Int32[]
  50. Callers (with count) that contribute at least for 1%:
  51. 1 100 % .DebugOne::Main(object,intptr,intptr)
  52. Total memory allocated: 448 KB
  53. </pre>
  54. At the top, it shows each method that is called. The data is sorted
  55. by the total time that the program spent within the method. Then
  56. it shows how many times the method was called, and the average time
  57. per call.
  58. Below this, it shows the top callers of the method. This is very useful
  59. data. If you find, for example, that the method Data::Computate () takes
  60. a very long time to run, you can look to see if any of the calls can be
  61. avoided.
  62. Two warnings must be given about the method data. First,
  63. the profiler has an overhead associated with it. As such,
  64. a high number of calls to a method may show up as consuming
  65. lots of time, when in reality they do not consume much time
  66. at all. If you see a method that has a very high number of
  67. calls, you may be able to ignore it. However, do consider
  68. removing calls if possible, as that will sometimes help
  69. performance. This problem is often seen with the use
  70. of built in collection types.
  71. Secondly, due to the nature of the profiler, recursive calls
  72. have extremely large times (because the profiler double counts
  73. when the method calls itself). One easy way to see this problem
  74. is that if a method is shown as taking more time than the Main
  75. method, it is very likely recursive, and causing this problem.
  76. Below the method data, allocation data is shown. This shows
  77. how much memory each method allocates. The number beside
  78. the method is the total amount of memory. Below that, it
  79. is broken down into types. Then, the caller data is given. This
  80. data is again useful when you want to figure out how to eliminate calls.
  81. You might want to keep a close eye on the memory consumption
  82. and on the method invocation counts. A lot of the
  83. performance gains in MCS for example came from reducing its
  84. memory usage, as opposed to changes in the execution path.
  85. ** Profiling without JIT instrumentation
  86. You might also be interested in using mono --aot to generate
  87. precompiled code, and then use a system like `oprofile' to
  88. profile your programs.
  89. ** Memory Management in the .NET/Mono world.
  90. Since Mono and .NET offer automatic garbage collection, the
  91. programmer is freed from having to track and dispose the
  92. objects it consumes (except for IDispose-like classes). This
  93. is a great productivity gain, but if you create thousands of
  94. objects, that will make the garbage collector do more work,
  95. and it might slow down your application.
  96. Remember, each time you allocate an object, the GC is forced
  97. to find space for the object. Each object has an 8 byte overhead
  98. (4 to tell what type it is, then 4 for a sync block). If
  99. the GC finds that it is running out of room, it will scan every
  100. object for pointers, looking for unreferenced objects. If you allocate
  101. extra objects, the GC then must take the effort to free the objects.
  102. Mono uses the Boehm GC, which is a conservative collector,
  103. and this might lead to some memory fragmentation and unlike
  104. generational GC systems, it has to scan the entire allocated
  105. memory pool.
  106. *** Boxing
  107. The .NET framework provides a rich hierarchy of object types.
  108. Each object not only has value information, but also type
  109. information associated with it. This type information makes
  110. many types of programs easier to write. It also has a cost
  111. associated with it. The type information takes up space.
  112. In order to reduce the cost of type information, almost every
  113. Object Oriented language has the concept of `primitives'.
  114. They usually map to types such as integers and booleans. These
  115. types do not have any type information associated with them.
  116. However, the language also must be able to treat primitives
  117. as first class datums -- in the class with objects. Languages
  118. handle this issue in different ways. Some choose to make a
  119. special class for each primitive, and force the user to do an
  120. operation such as:
  121. <pre class="shell">
  122. // This is Java
  123. list.add (new Integer (1));
  124. System.out.println (list.get (1).intValue ());
  125. </pre>
  126. The C# design team was not satisfied with this type
  127. of construct. They added a notion of `boxing' to the language.
  128. Boxing preforms the same thing as Java's <code>new Integer (1)</code>.
  129. The user is not forced to write the extra code. However,
  130. behind the scenes the <em>same thing</em> is being done
  131. by the runtime. Each time a primitive is cast to an object,
  132. a new object is allocated.
  133. You must be careful when casting a primitive to an object.
  134. Note that because it is an implicit conversion, you will
  135. not see it in your code. For example, boxing is happening here:
  136. <pre class="shell">
  137. ArrayList foo = new ArrayList ();
  138. foo.Add (1);
  139. </pre>
  140. In high performance code, this operation can be very costly.
  141. *** Using structs instead of classes for small objects
  142. For small objects, you might want to consider using value
  143. types (structs) instead of object (classes).
  144. However, you must be careful that you do not use the struct
  145. as an object, in that case it will actually be more costly.
  146. As a rule of thumb, only use structs if you have a small
  147. number of fields (totaling less than 32 bytes), and
  148. need to pass the item `by value'. You should not box the object.
  149. *** Assisting the Garbage Collector
  150. Although the Garbage Collector will do the right thing in
  151. terms of releasing and finalizing objects on time, you can
  152. assist the garbage collector by clearing the fields that
  153. points to objects. This means that some objects might be
  154. eligible for collection earlier than they would, this can help
  155. reduce the memory consumption and reduce the work that the GC
  156. has to do.
  157. ** Common problems with <tt>foreach</tt>
  158. The <tt>foreach</tt> C# statement handles various kinds of
  159. different constructs (about seven different code patterns are
  160. generated). Typically foreach generates more efficient code
  161. than loops constructed manually, and also ensures that objects
  162. which implement IDispose are properly released.
  163. But foreach sometimes might generate code that under stress
  164. performs badly. Foreach performs badly when its used in tight
  165. loops, and its use leads to the creation of many enumerators.
  166. Although technically obtaining an enumerator for some objects
  167. like ArrayList is more efficient than using the ArrayList
  168. indexer, the pressure introduced due to the extra memory
  169. requirements and the demands on the garbage collector make it
  170. more inefficient.
  171. There is no straight-forward rule on when to use foreach, and
  172. when to use a manual loop. The best thing to do is to always
  173. use foreach, and only when profile shows a problem, replace
  174. foreach with for loops.