The 1ms difference - stackalloc
During a recent leetcode exercise, I was struggling why my already optimal solution is still having 1ms time behind the top solvers.
When I inspect their code, itโs a shame for me that I didnโt realize this keyword exists after writing C# for more than a decade.
Documenting it here because I have learned something new. ๐
The keyword stackalloc is for creating small, shortโlived buffers without heap allocation.
Itโs alternative to new, which allocate memory on heap instead.
Typical use cases:
- Temporary scratch buffers in hot paths (parsers, encoders, algorithms) to avoid GC pressure.
- Interop scenarios where you need a fixed buffer for native calls.
- Highโperformance code where you want to avoid allocating a new array on each call.
Limits / Notes:
- Size should be small; stack is limited and tooโlarge allocations can cause stack overflow.
- Lifetime is the current method scope only.
- Works with
Span<T>/ReadOnlySpan<T>in safe code; no need for unsafe in many cases now. - Stack should be small, rule of thumb: A few K bytes maximum.
Alternatives
- Other choice of high performance data structure:
ArrayPool<T>- Have to be careful on not accessing memory outside of rent boundary
- Have to be careful on explicit requesting array clean up on return, otherwise it is not guaranteed to be cleared.
var pool = ArrayPool<byte>.Shared;
byte[] buffer = pool.Rent(4096);
try
{
// use buffer
}
finally
{
pool.Return(buffer, clearArray: true);
}
Next time when you need to frequently create a small data structure for your algorithm, try stackalloc or ArrayPool<T>. Happy coding. ๐