Repository navigation
Read the backing array through a compiled accessor in CollectionsMarshal.AsSpan - #707
Merged
Merged
Conversation
…onsMarshal.AsSpan AsSpan called FieldInfo.GetValue on every call. Compile the field read once per T and cache the delegate.
This was referenced Oct 12, 2026
This was referenced Oct 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
CollectionsMarshal.AsSpanpolyfill (below net5) readsList<T>'s private_itemsfield. It did that withFieldInfo.GetValueon every call, which costs far more than the rest of the method, andAsSpanis normally called to make a loop faster.The field read is now compiled once per
T(Expression.Field(...)+Compile()) into aFunc<List<T>, T[]>held in the existing static genericItemsAccessor<T>. If the expression can not be compiled on the current platform, it falls back to a delegate that callsFieldInfo.GetValue, as before. The//Note:is unchanged and still accurate: the backing array is still located via reflection.Measurements
net48, Release, x64, Stopwatch, best of 9, ns per call (includes the calling lambda and building the span):
AsSpan(List<int>)AsSpan(List<string>)The trade-off is a one-off cost the first time
AsSpanis used for eachT. Measured in fresh processes on a shared (noisy) machine: 6 to 8 ms for the firstTin the process, against about 1.3 ms for the first reflection based call before, and 2 to 4 ms for each furtherT, against about 0.7 ms. At roughly 60 ns saved per call, that is repaid after about 100,000 calls for the firstTand 30,000 to 50,000 for each further one. Worth it for the loopsAsSpanis meant for; a net loss for code that calls it a handful of times.Tests
Added to
PolyfillTests_CollectionsMarshal.cs: several element types in one process (string?with nulls, a struct,byte, arrays) with reads and writes through the span; length beingCountrather thanCapacity, and zero afterClear; a list followed through 200 growth steps, since each growth replaces the backing array and a cached accessor must read the current one; two lists of the sameTinterleaved; a class derived fromList<T>.