-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid implicitly allocating params arrays in loops #33793
Comments
Estimates:
|
@terrajobst can you please provide a code example? |
ExamplesLet's use the public static class Console
{
public static void WriteLine(string format, params object?[]? arg);
} Case 1FlagBefore: int i = 0;
while (i++ < 10)
{
Console.WriteLine("{0}, {1}, {2}, {3}", 1, 2, 3, 4);
} After: int i = 0;
object[] arg = new object[] { 1, 2, 3, 4 };
while (i++ < 10)
{
Console.WriteLine("{0}, {1}, {2}, {3}", arg);
} Case 2Do not flag: An element in the passed params array has variable dataint i = 0;
while (i++ < 10)
{
Console.WriteLine("{0}, {1}, {2}, {3}", i, 2, 3, 4);
} Case 3FlagBefore: Variables are used inside the loop, but they weren't modified between their creation and their use: short a = 10, b = 20, c = 30, d = 40;
int i = 0;
while (i++ < 10)
{
Console.WriteLine("{0}, {1}, {2}, {3}", a, b, c, d);
} After: short a = 10, b = 20, c = 30, d = 40;
int i = 0;
object[] arg = new object[] { a, b, c, d };
while (i++ < 10)
{
Console.WriteLine("{0}, {1}, {2}, {3}", arg);
} Case 4Having this class: class C
{
private string _name;
public C(string name) => _name = name;
public override string ToString() => _name;
} FlagBefore: int i = 0;
while (i++ < 10)
{
Console.WriteLine("Format {0}, {1}", new C("a"), new C("b"), new C("c"), new C("d"));
} After: Notice the data type of the array can be preserved as int i = 0;
object[] arg = new object[] { new C("a"), new C("b"), new C("c"), new C("d") };
while (i++ < 10)
{
Console.WriteLine("{0}, {1}, {2}, {3}", arg);
} Case 5Do not flag: An element in the passed params array has variable dataint i = 0;
while (i++ < 10)
{
Console.WriteLine("{0}, {1}, {2}, {3}", new C($"{i}"), new C("b"), new C("c"), new C("d"));
} Case 6Do not flag: Method does not belong to
|
The proposed fixes, and situations where they won't fix things, seem a bit strange. There's value in highlighting that the array allocation is happening in a loop, but the manner in which it gets fixed (or ignored) is too variable. So we shouldn't have a fixer at this time. (Ideally we'll get params-span and that'll make this go away)
|
I would like to be assigned to this, please. |
I think this is ALREADY implemented (except the "in loop" part and the attribute requirement). An implementation of this as a "CAxxxx" can benefit from the existing implementation. |
With |
Find calls to
System.*
methods inside loops, where those methods takeparams
arrays and those params arrays are being allocated, and hoist the allocation to before the loop if possible.Category: Performance
The text was updated successfully, but these errors were encountered: