Member-only story

I Doubled My API Speed With Just 3 Lines of C#: CPU Cache Lines

Is It Vritra - SDE I
6 min readJan 29, 2025

Think about the last time you optimized your .NET application. You probably focused on algorithms, database queries, or maybe async patterns. But what if I told you that simply changing how your data is laid out in memory could double your application’s performance? This isn’t theoretical — we recently discovered this the hard way when investigating performance issues in our high-traffic API.

Modern CPUs are incredibly fast!!! but they spend most of their time waiting. Waiting for what? Memory. While your CPU can execute instructions in a fraction of a nanosecond, fetching data from main memory takes hundreds of CPU cycles (that’s what we can control). To bridge this gap, CPUs use a hierarchy of caches — small, fast memory areas that keep frequently accessed data close to the processing cores.

The Problem: A Tale of Innocent Struct Layout

see… we had a seemingly innocent struct that handled user session data:

public struct SessionData
{
public bool IsAuthenticated; // 1 byte
public string Username; // 8 bytes (reference)
public byte SecurityLevel; // 1 byte
public DateTime LastAccess; // 8 bytes
public Guid SessionId; // 16 bytes
}

--

--

Is It Vritra - SDE I
Is It Vritra - SDE I

Written by Is It Vritra - SDE I

Going on tech shits! AI is my pronouns

Responses (8)