Laszlo

Hello, I am Laszlo

Software-Engineer, .NET developer

Contact Me

Improving DateTime Cache

In one of the previous posts, I explored how .NET 11 uses a copy-on-write locking strategy for caching function pointers and how other locking strategies could be used as an alternative. In that post I used an example, that is intentionally artificial: a DateTime GetDateTime(int year) method returns a specific DateTime value for a given year, representing January 1 of that year.

I used the Interlocked pattern with a constraint on the input: a maximum of 30 distinct entries were allowed. This let me preallocate a backing array for all possible values and use Interlocked.CompareExchange to atomically replace the default values of the array with the cached entries. The conclusion of that post said:

Interlocked.CompareExchange is known to be more efficient than Lock, but the gains are quickly offset by external factors. [...] The copy-on-write strategy remains a flexible and performant choice for similar problems.

This post looks into improving that solution. The code presented here is not safe to use in production.

Find out more »


Common ArrayPool Mistakes

In this post I review common mistakes I find (and sometimes make) when using ArrayPool<T> in .NET and C#. Examples target the .NET 10 runtime and the corresponding BCL.

ArrayPool<T> lets you rent arrays from a shared pool. It's typically used to obtain a temporary buffer for intermediate work before copying results to a final destination. When a method cannot update input data in place or requires a larger buffer for intermediate calculations, ArrayPool<T> is often beneficial: it avoids allocating a new array for every operation, reducing GC pressure and improving performance. The most common generic types used with ArrayPool are byte and char.

Typical usage involves three steps:

  • Rent an array from the pool.
  • Use the rented array for operations.
  • Return the array to the pool.

Find out more »


Deep dive into .NET 11 Performance Improvements #7

In this series, I explore some of the most interesting .NET 11 performance improvements described in Stephen Toub's blog post, Performance Improvements in .NET 11.

Each post focuses on one example from the original article while digging a little deeper into an additional aspect that was not covered there. The series covers the following topics:

  1. Allocation free numbers
  2. Virtual generic method calls
  3. Vector zero equality
  4. Guid creation on Linux
  5. JIT generating implicit-else assembly instructions
  6. Guid equality
  7. Current post: Copy-on-write locking strategy

Copy-on-write locking strategy

Find out more »


Deep dive into .NET 11 Performance Improvements #6

In this series, I explore some of the most interesting .NET 11 performance improvements described in Stephen Toub's blog post, Performance Improvements in .NET 11.

Each post focuses on one example from the original article while digging a little deeper into an additional aspect that was not covered there. The series covers the following topics:

  1. Allocation free numbers
  2. Virtual generic method calls
  3. Vector zero equality
  4. Guid creation on Linux
  5. JIT generating implicit-else assembly instructions
  6. Current post: Guid equality
  7. Copy-on-write locking strategy

Guid equality

Find out more »


Deep dive into .NET 11 Performance Improvements #5

In this series, I explore some of the most interesting .NET 11 performance improvements described in Stephen Toub's blog post, Performance Improvements in .NET 11.

Each post focuses on one example from the original article while digging a little deeper into an additional aspect that was not covered there. The series covers the following topics:

  1. Allocation free numbers
  2. Virtual generic method calls
  3. Vector zero equality
  4. Guid creation on Linux
  5. Current post: JIT generating implicit-else assembly instructions
  6. Guid equality
  7. Copy-on-write locking strategy

JIT generating implicit-else assembly instructions

Find out more »