[Proposal]: Allow "null" default value for "params" parameters #4382
Replies: 20 comments 1 reply
-
Do you have any benchmarks as to what this overhead actually is? I would be shocked if this actually mattered. |
Beta Was this translation helpful? Give feedback.
-
seems like this saves a call to |
Beta Was this translation helpful? Give feedback.
-
@RikkiGibson that should end up being a single access to a static read-only field. I would be surprised if there were any apps where that mattered. |
Beta Was this translation helpful? Give feedback.
-
I have tried to make the simple benchmark: [SimpleJob(launchCount: 1, warmupCount: 10, targetCount: 100, invocationCount: 20000000)]
public class BenchmarkClass
{
[Benchmark]
public string[] NullValue()
{
return Test(null);
}
[Benchmark]
public string[] EmptyArrayValue()
{
return Test();
}
public static string[] Test(params string[] value)
{
return value;
}
} I have got the following results on my machine:
|
Beta Was this translation helpful? Give feedback.
-
Three observations. Firstly, the difference in performance between those two calls is so small as to be meaningless for any real world situation. It's a trivial per-call overhead, and you'd need a tight loop executing a billion calls before it scaled up enough to be human perceivable. Secondly, the actual difference is even less than your benchmark shows, because now the implementation needs to test for, and handle, being passed a null value. You've got the overhead of the null test itself, plus any potential overhead from a branch predication failure by the CPU. This very well might tip the balance the other way. Thirdly, a compiler change to pass null instead of The LDM has a very high threshold for a breaking change to be accepted. Such a change has to be shown that the pain caused by the problem is substantially higher than the pain that would be caused by the fix. |
Beta Was this translation helpful? Give feedback.
-
You'd nabbed my curiosity @Chakrygin so I tried out running the benchmarks on my own machine. My first change was to change all the methods to return int instead of
Interesting to note that now My second change was to change public static int Test(params string[] value)
{
if (value == null)
{
return 0;
}
return value.Length;
}
Here we see |
Beta Was this translation helpful? Give feedback.
-
Don't forget the margins of error in those tests. In every benchmark that has been posted to this thread so far, nothing can be gleaned about which is faster because the times +/- the error has overlapped. |
Beta Was this translation helpful? Give feedback.
-
Also, benchmark.net has a statistical test mode for telling you whether a difference is meaningful:
|
Beta Was this translation helpful? Give feedback.
-
@theunrepentantgeek This proposal does not introduce breaking changes. |
Beta Was this translation helpful? Give feedback.
-
The test did not work because |
Beta Was this translation helpful? Give feedback.
-
@Cologler It does not – if |
Beta Was this translation helpful? Give feedback.
-
@IllidanS4 commented on 2021年1月31日 GMT+8 下午7:20:
You are right. This is interesting and the document only implicitly mentions it. (╯°□°)╯︵ ┻━┻ |
Beta Was this translation helpful? Give feedback.
-
Null test may be more annoying than passing a dummy reference. Anyway, |
Beta Was this translation helpful? Give feedback.
-
Human perceivable is not a relevant benchmark timescale for many things |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
That's |
Beta Was this translation helpful? Give feedback.
-
@HaloFour yes, but it was an example of explicitly not using There are several other examples, possibly most notably in |
Beta Was this translation helpful? Give feedback.
-
Those are all cases within another generic type, though. And IIRC that's a cost you only incur once. |
Beta Was this translation helpful? Give feedback.
-
Yes, but nonetheless it shows why OP's argument is at least somewhat valid, just not because of speed |
Beta Was this translation helpful? Give feedback.
-
I don't currently see any value demonstrated in this proposal. This feels like a case of "if it ain't broke". I'm also moving this to be a discussion as per our policies on issues being used officially for actual bugs or for taking proposals that we actually intend to take through the LDM process. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Allow "null" default value for "params" parameters
Summary
When calling such methods with an empty argument list, use null instead of an empty array.
Motivation
Now, if the developer defines a method with the "params" keyword:
And then the method is called with no arguments:
This code is equivalent to:
Although this code does not allocate additional memory, it has a small overhead of getting an empty array.
Therefore, many developers explicitly overload such methods:
This is a proposal to be able to use the null default instead of the overloads, like this:
Detailed design
Drawbacks
Alternatives
Unresolved questions
Design meetings
Beta Was this translation helpful? Give feedback.
All reactions