Csharp

Visualizing Latency

Visualizing Latency

This is my visualizing latency post series Introduction What is Binning? Rendering Event Data Official D3 Latency Heatmap Page
Exploring the .NET CoreFX

Exploring the .NET CoreFX

This is my exploring the .NET CoreFX post series Annotate Pure Methods With PureAttribute Cache ThreadLocal Variables in Locals Making Methods Debugger-Friendly The Requires Convenience Class Keep Indexers Trivial to Allow JIT Optimization Use IEquatable for Higher-Performance Equals() Reference Versus Structural Equality NullReferenceException Performance Tricks Immutable Collections and the Builder Pattern Performance Tuning Enumeration Code Contracts Aggressive Inlining ImmutableList is an AVL Tree Inside Immutable Collections Using Non-Generic Factory Classes to Enable Type Inference Platform-Specific Builds Using Compile-Time Polymorphism Videotaped API Review
Exploring the .NET CoreFX Part 16: Platform-Specific Builds Using Compile-Time Polymorphism

Exploring the .NET CoreFX Part 16: Platform-Specific Builds Using Compile-Time Polymorphism

This is part 16 of my Exploring the .NET CoreFX Series. While .NET has historically been limited to Windows machines, Mono notwithstanding, the introduction of the cross-platform .NET Core runtime has introduced the possibility of running .NET Core applications on Unix machines. With this possibility, developers may have the need of writing platform-specific code. One way to write platform-specific code is: Define a conceptual base class which will have an identical name and methods across all platforms.
Exploring the .NET CoreFX Part 15: Using Non-Generic Factory Classes to Enable Type Inference

Exploring the .NET CoreFX Part 15: Using Non-Generic Factory Classes to Enable Type Inference

This is part 15 of my Exploring the .NET CoreFX Series. While C# supports type inference for generic methods, it does not support type inference for constructors. In other words, while this code works: public class FooFactory { public static Foo<T> Create<T>(T value) { return new Foo<T>(value); } } var myObj = FooFactory.Create(212); This code does not: public class Foo<T> { private readonly T field; public Foo(T value) { field = value; } } var obj = new Foo(212); // DOES NOT WORK For more background on why this is, see this StackOverflow post.
Exploring the .NET CoreFX Part 14: Inside Immutable Collections

Exploring the .NET CoreFX Part 14: Inside Immutable Collections

This is part 14 of my Exploring the .NET CoreFX Series. Back in 2013, Immo Landwerth and Andrew Arnott recorded a Going Deep video called Inside Immutable Collections which describes how and why System.Collections.Immutable is built the way it is. It’s great background material to understand System.Collections.Immutable.
Exploring the .NET CoreFX Part 13: ImmutableList is an AVL Tree

Exploring the .NET CoreFX Part 13: ImmutableList is an AVL Tree

This is part 13 of my Exploring the .NET CoreFX Series. Most implementations of IList, including System.Collections.Generic.List, are dynamic arrays. System.Collections.Immutable.ImmutableList is different – it is an AVL tree. This results in significantly different performance characteristics: List ImmutableList Indexing O(1) O(log n) Append O(1) average, O(n) worst-case O(log n) Insert at arbitrary index O(n) O(log n) Remove O(n) O(log n) Memory layout Contiguous for value types Non-contiguous The data structure behind ImmutableList was likely chosen so that modifications to the list are non-destructive and require minimal data copying.
Exploring the .NET CoreFX Part 12: Aggressive Inlining

Exploring the .NET CoreFX Part 12: Aggressive Inlining

This is part 12 of my Exploring the .NET CoreFX Series. In C++, the inline keyword allows a developer to provide a hint to the compiler that a particular method should be inlined. C# has the identical ability but uses an attribute instead: internal class SecurePooledObject<T> { ....[MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool IsOwned<TCaller>(ref TCaller caller) where TCaller : struct, ISecurePooledObjectUser { return caller.PoolUserId == _owner; } } In System.Collections.Immutable, this attribute is used highly selectively – only once, in fact.
Exploring the .NET CoreFX Part 11: Code Contracts

Exploring the .NET CoreFX Part 11: Code Contracts

This is part 11 of my Exploring the .NET CoreFX Series. In 2008, Microsoft Research published Code Contracts, which provide a language-agnostic way to express coding assumptions in .NET programs. The assumptions take the form of pre-conditions, post-conditions, and object invariants. Here is a simple example of code which uses Code Contracts: using System.Diagnostics.Contracts; public class StringUtils { internal static string Append(string s1, string s2) { Contract.Requires(s1 != null); Contract.