|
|
|
|
|
by pjmlp
3065 days ago
|
|
Here is a list, not easy to track all of them down, but maybe as keywords to easy googling. - structs - unsafe code - stack allocation in unsafe code (think alloca()) - attribute annotations for packing and inline calls across assemblies - ref parameters - ref returns - readonly ref parameters - Span<> and Memory<> - Native memory allocation via MarshalInterop, SafeHandles - Buffer and ArraySegment - SIMD (with RyuJIT) - Profiled code cache for JIT code background generation between executions (System.Runtime.ProfileOptimization) |
|
Accessing data on the stack instead of the heap is the #1 saver of execution time, in my experience. But your bottlenecks might be different. Locally scoped value-type variables are generally on the stack. Object-scoped and static fields and properties are on the heap.
Writes to local variables seem to be faster than reads, IIRC. The fastest operators seem to be the bitwise instructions, IIRC. If running in 32-bit mode, try to work with 32-bit integers. If running in 64-bit mode, try to work with 64-bit integers.
Here's an example of a major, major improvement in performance
for(int x = 0; x < this.Width; x++)
{
}Much faster version (due to storing a copy of Width and Height on the stack instead of the heap):
int width = this.Width;
int height = this.Height;
for(int x = 0; x < width; x++)
{
}My comment here describes roughly the approach I used to take advantage of stack-allocated memory (before Span<T> was available). https://news.ycombinator.com/item?id=15136627