27

I've been reading through the Contributing Code section of the .NET Compiler Platform ("Roslyn"), and I came across the guidelines for coding conventions. I understand most of the coding conventions and why they would ask for it. But I don't understand what they mean by this:

Avoid allocations in compiler hot paths:

Avoid LINQ.

Avoid using foreach over collections that do not have a struct enumerator.

What is a "compiler hot path"? And why should I avoid using LINQ and avoid doing a foreach over collections that do not have a struct enumerator?

2 Answers 2

40

Compiler hot paths are code execution paths in the compiler in which most of the execution time is spent, and which are potentially executed very often.

The reason for avoiding (heap) allocations in these code paths is that allocations may trigger a garbage collection, which may cause sudden, extreme performance deteriorations. These should obviously be avoided in very commonly executed code paths.

Linq and foreach are singled out because these will implicitly allocate memory – unless your GetEnumerator returns a struct, which will not cause heap allocations.

7
  • Garbage collection wouldn't matter, because a compiler is an application where the total execution time matters, actually average execution time over many invocations. Slow downs caused by garbage collection matter in UI application where the user can see even 1/10th of a second delay as stuttering. Allocations in hoth code paths just means many allocations, and you should avoid to do many allocations because they take time on average.
    – gnasher729
    Apr 6, 2014 at 14:36
  • 7
    @gnasher729: Roslyn is a UI application; GC activity directly affects typing speed in VS.
    – SLaks
    Apr 6, 2014 at 14:51
  • @gnasher729 A heap allocation on a GC which does not trigger a collection takes virtually no time at all. Otherwise GC languages, with their heavy reliance on heap allocation for most operations, wouldn’t work in practice. Oh, and what SLaks said. Apr 6, 2014 at 15:12
  • 1
    @Manu343726 Oh, I agree. GCs actually work phenomenally as long as there’s enough spare memory (by a sizeable factor), and you’re not on a real-time system. In memory intensive applications (or, conversely, on memory constrained systems), modern GCs simply don’t perform well. Apr 6, 2014 at 21:08
  • 3
    gnasher729 is partially right; it is only in part the sudden, extreme performance deterioration that we are concerned about in the Roslyn code base. We just as much concerned about the throughput of the compiler. Minimizing allocations reduces the number of garbage collections, thereby increasing the throughput of the compiler. Apr 7, 2014 at 1:25
14

The "hot path" is the code path that is most critical for performance. It is the snippets of code that are executed millions or billions of times per second, taking up the majority of the execution time.

As I read it, the other two are just examples of situations that may cause implicit allocations, and therefore should be avoided in performance-critical parts of the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.