You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The analyzer seems to recognize that there are cases where using the range operator would be suboptimal, and does not raise diagnostics even when enabled:
x.Slice(s, l)// Not flagged by analyzer, presumably to avoid unnecessary subtractions
Another case where using the range operator is suboptimal is when the slice goes to the end of the original sequence, and there is an overload accepting a single int:
x.Slice(n)// Analyzer suggests: x[n..]
Using a range expression in the way suggested would be fine if the compiler would recognise that this can use the simpler overload, and therefore generate identical code, but this isn't the case (and would appear to not be permitted by the spec):
// static Span<byte> Foo(Span<byte> x, int n) => x.Slice(n);
ldarga.s x
ldarg.1
call instance valuetype [System.Runtime]System.Span`1<!0> valuetype [System.Runtime]System.Span`1<uint8>::Slice(int32)
ret
This is by design. The survey of the ecosystem indicated the semantics should be identical in an these cases. So this is an acceptable idiom to move the code to.
Now, the question is this idiom is optimized as much as it should be is another issue altogether. But anyone writing x[n..] should expect that that should be reasonable and properly optimized by the stack.
Version Used: 3.9.0-6.21160.10 (Visual Studio 16.9.4)
There are a number of scenarios detected by this analyzer where preferring the range operator makes sense:
The analyzer seems to recognize that there are cases where using the range operator would be suboptimal, and does not raise diagnostics even when enabled:
Another case where using the range operator is suboptimal is when the slice goes to the end of the original sequence, and there is an overload accepting a single int:
Using a range expression in the way suggested would be fine if the compiler would recognise that this can use the simpler overload, and therefore generate identical code, but this isn't the case (and would appear to not be permitted by the spec):
The text was updated successfully, but these errors were encountered: