Skip to content

Instantly share code, notes, and snippets.

@ladeak
ladeak / Font
Last active June 19, 2024 21:51
Terminal
https://www.nerdfonts.com/font-downloads
CaskaydiaCode Nerd Font
winget install JanDeDobbeleer.OhMyPosh -s winget
@ladeak
ladeak / string-interpolation-stringbuilder-2.md
Last active August 27, 2021 09:54
string-interpolation-stringbuilder.md

String Interpolation and StringBuilder in .NET6

In a previous post I have looked into what are the performance characteristics of creating interpolated string in .NET 5 and early previews of .NET 6. With .NET 6 preview 7 or better a new approach is used by the C# compiler with support from the BCL to build interpolated strings.

Fortunately the new way provides a faster approach for the most convenient ways for assembling strings, however it makes my previous post completely obsolete.

For details about the new way DefaultInterpolatedStringHandler builds strings, read: https://devblogs.microsoft.com/dotnet/string-interpolation-in-c-10-and-net-6/

Re-running the benchmarks from the previous post, shows much better memory usage:

@ladeak
ladeak / ActivityLogWriter
Created March 14, 2021 19:14
ActivityLogWriter
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace ConsoleApp2
{
public class ActivityLogWriter : IActivityWriter
{
private readonly ILogger<ActivityLogWriter> _logger;
@ladeak
ladeak / ActivityLogWriter
Last active March 14, 2021 19:15
OpenTelemetryActivityLogWriter
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace ConsoleApp2
{
public class ActivityLogWriter : IActivityWriter
{
private readonly ILogger<ActivityLogWriter> _logger;

Null-coalescing Operator vs. Equals - Equals

I have been asked the following question over-and-over: which solution is the faster and better to use in the condition part of the if keyword, to branch based on a property which might be defined on a nullable reference type:

  • to use the null-coalescing operator (??)
  • or the equals operator (==)

To give some context let's assume that we branch our code based on a reference type's bool property:

public class SomeClass
@ladeak
ladeak / refreturns2.md
Last active April 20, 2019 12:09
RefReturns under windbg

Deep Dive in Ref Returns

In the previous post I have used Ref returns to return some data. I noticed that with slight changes we get totally different code generated by the JIT, which is can have a good or bad effect on our code.

In this post, I will dig deep (with WinDbg) in the JIT generated code. As forefront: I am using 64 bit machine, .net core 2.1 and RyuJIT.

I created a sample benchmark to showcase. I have a Point struct with 2 integer properties. I benchmark setting the values on the struct in 3 different ways, I show related IL and machine code impacting performance.

The benchmark

@ladeak
ladeak / refreturns.md
Created April 16, 2019 17:21
RefReturns Blog Post

Ref Returns

C# has recently introduced some new features (version <7.0) one of which it is ref returns. I will use this feature in this post to further improve the performance of the Parser created in my previous post.

To refresh the Parser class looked as follows:

public class Parser 
{
 public T Parse(ReadOnlySpan input, PropertySetter[] setters) where T : struct