Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Avoid allocations in compiler hot paths" Roslyn Coding Conventions

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?

like image 544
Cloud9999Strife Avatar asked Apr 06 '14 13:04

Cloud9999Strife


2 Answers

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.

like image 164
Konrad Rudolph Avatar answered Nov 10 '22 20:11

Konrad Rudolph


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.

like image 13
jalf Avatar answered Nov 10 '22 22:11

jalf