Span in a hot loop
A Span<T> is a view over memory that already exists: a range of an array, a slice of a string, a block of the stack. It has a pointer and a length, it is a value type, and it allocates nothing. Parsing a line with string.Split creates a string per field; parsing the same line over a span creates none, which is the difference this lesson measures.
Split allocates strings
Split returns an array of strings, and every element is a new string copied out of the source. A loop over a line of five fields therefore allocates five strings and one array per iteration, and a hot parser pays that a hundred thousand times:
var line = "12,34,56,78,90";
long sum = 0;
for (var n = 0; n < 100000; n++)
{
foreach (var part in line.Split(','))
{
sum += int.Parse(part);
}
}
Console.WriteLine($"sum {sum}");
The array and its strings become garbage as soon as the iteration ends, so the collector runs often and the work it does is part of the cost of a parser that looks innocent.
The same parse over a span
line.AsSpan() is a view over the characters of the string, and IndexOf finds the next comma. int.Parse has an overload that takes a ReadOnlySpan<char>, so no substring is needed. The whole measured program:
using System.Diagnostics;
var line = "12,34,56,78,90";
var splitRuns = new List<double>();
var spanRuns = new List<double>();
long splitSum = 0;
long spanSum = 0;
for (var run = 0; run < 5; run++)
{
var watch = Stopwatch.StartNew();
for (var n = 0; n < 100000; n++)
{
foreach (var part in line.Split(','))
{
splitSum += int.Parse(part);
}
}
watch.Stop();
splitRuns.Add(watch.Elapsed.TotalMilliseconds);
watch = Stopwatch.StartNew();
for (var n = 0; n < 100000; n++)
{
var rest = line.AsSpan();
while (!rest.IsEmpty)
{
var comma = rest.IndexOf(',');
var part = comma < 0 ? rest : rest[..comma];
spanSum += int.Parse(part);
rest = comma < 0 ? default : rest[(comma + 1)..];
}
}
watch.Stop();
spanRuns.Add(watch.Elapsed.TotalMilliseconds);
Console.WriteLine($"run {run + 1} split {splitRuns[^1]:F2} span {spanRuns[^1]:F2}");
}
var before = GC.GetAllocatedBytesForCurrentThread();
for (var n = 0; n < 1000; n++)
{
foreach (var part in line.Split(','))
{
splitSum += int.Parse(part);
}
}
var splitBytes = GC.GetAllocatedBytesForCurrentThread() - before;
before = GC.GetAllocatedBytesForCurrentThread();
for (var n = 0; n < 1000; n++)
{
var rest = line.AsSpan();
while (!rest.IsEmpty)
{
var comma = rest.IndexOf(',');
var part = comma < 0 ? rest : rest[..comma];
spanSum += int.Parse(part);
rest = comma < 0 ? default : rest[(comma + 1)..];
}
}
var spanBytes = GC.GetAllocatedBytesForCurrentThread() - before;
Console.WriteLine($"split min {splitRuns.Min():F2} median {splitRuns.Order().ElementAt(2):F2}");
Console.WriteLine($"span min {spanRuns.Min():F2} median {spanRuns.Order().ElementAt(2):F2}");
Console.WriteLine($"split bytes {splitBytes}");
Console.WriteLine($"span bytes {spanBytes}");
Console.WriteLine($"sums {splitSum == spanSum}");
dotnet run
run 1 split 101.80 span 91.67
run 2 split 107.97 span 146.06
run 3 split 101.56 span 53.56
run 4 split 68.02 span 53.22
run 5 split 68.07 span 53.14
split min 68.02 median 101.56
span min 53.14 median 53.56
split bytes 224000
span bytes 0
sums True
The measured allocations
The allocation line is the stable result: a thousand parses through Split allocate 224 000 bytes, which is 224 per line, and a thousand parses over a span allocate nothing at all. The time line is noisier, because a hundred thousand iterations of five fields is a short run and the container shares its CPUs: the span run was faster in four of the five pairs, and one run of the span loop was slower than any split run. The median, 53.56 against 101.56 milliseconds, is the number to quote, and the allocations are the number that does not move.
The span version also copies nothing: part points into the original string, so the characters are read where they already are. The split version copies every field, which is the 224 bytes per line.
The rules of a span
- Span<T> and ReadOnlySpan<T> are ref struct types: they can live on the stack, as a local or a parameter, and not as a field of a class, an element of an array, or a value captured by a lambda.
- A span cannot cross an await; a method that must hold a slice across a wait uses Memory<T>, which is a heap object that can produce a span when it is used.
- A read-only span over a string is safe; a writable one over an array lets the code change the array, which is why it exists for a buffer a program fills.
- stackalloc makes a span over stack memory, for a buffer of a known small size that never escapes the method.
Exercises
- Parse the same line with MemoryExtensions.Split and compare the code with the manual loop.
- Add a sixth field to the line and predict the allocation per iteration.
- Write the same parser over a char[] with AsSpan and change one character.
- Try to store a span in a field of a class and read the compiler error.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is a Span<T>?
Answer
A view over memory that already exists
It holds a reference and a length and allocates nothing.
-
How many bytes did the span parse allocate in the measurement?
Answer
0
The program printed span bytes 0 against 224000 for Split.
-
What does this program print?
Console.WriteLine("abc".AsSpan().Length);Answer
3
The span covers the three characters of the string.
-
Why can a span not be a field of a class?
Answer
It is a ref struct and may point at the stack
The compiler refuses it because the memory it points at may not outlive the method.
-
What does this program print?
var span = "12,34".AsSpan(); Console.WriteLine(span[..2].ToString());Answer
12
The range takes the first two characters and ToString copies them.
-
Which overload of int.Parse takes a span?
Answer
int.Parse(ReadOnlySpan<char>)
It is the overload the span parser used, and it copies nothing.
-
Why does Split allocate per field?
Answer
Every field becomes a new string
The measured 224 bytes per line are those strings and the array.
-
What does this program print? ``` Console.WriteLine("12,34,56".AsSpan().Count(
Answer
2
There are two commas in the span.
-
What is Memory<T> for?
Answer
Holding a slice across an await or in a field
A Memory is a heap object and can produce a span when it is used.
-
What did the median time show?
Answer
Span 53.56 ms against Split 101.56 ms
The span was faster in four pairs, and the medians are on the page.
The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev