Closed MrBrixican closed 11 months ago
Huh interesting! I had always assumed foreach
and for
had similar performance results for simple containers like lists where the enumerator is just a light struct, but it must not inline the function calls or something. Nice change!
for
loops are ~2x faster for smallList
s (that we typically deal with here).In a degenerate case where two alternating textures are drawn via
Batcher
, the time decreases for these methods (percent time in release mode):Material.Get
: 11.06% -> 5.47%Material.Apply
: 8.65% -> 8.09%A benchmark via
BenchmarkDotNet
shows the improvement as well. I also testedDictionary
,Array
, andSpan
(Span
withList
usesCollectionsMarshal.AsSpan
). Here are the full results with 5 uniforms looking for the last uniform in the list.As you can see, we can get slightly better results with
List
converted toSpan
usingCollectionsMarshal.AsSpan
at the cost of some transparency. I personally opted to just change tofor
loop to maintain simplicity. Dictionary may be tempting but aside from lookups, just about every other operation is slower for low counts. In fact, lookups near the beginning of aList
are faster.