-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
API proposal: Array.IsNullOrEmpty #32642
Comments
You're dashing my hopes and dreams, man. :) |
😄 |
Within the link that @stephentoub mention above, there is a comment about |
(Edit: wrong link above.) |
FWIW I did verify that the JIT does elide the bounds check on the pattern |
Does it not elide it for: if (someArray != null && someArray.Length != 0) { /* use someArray[0] */ } or if (someArray?.Length != 0) { /* use someArray[0] */ } ? |
Correct, those patterns aren't elided. There's a code comment in runtime/src/libraries/System.Private.CoreLib/src/System/String.cs Lines 458 to 467 in 97b0b37
|
That is a bug to fix in the JIT, not to workaround by adding new APIs. |
Yeah, I only found around 30-ish call sites within System.Private.CoreLib that would be candidates for using this helper method. (See GrabYourPitchforks@ef639cd.) That might not be enough to justify the work. Even |
Having this method not return the length via out is [faux pas] e.g. NOT 聡. I hate to be a troll but since the JIT can't be fixed so easily why not take the easy road and then have the jit eliminate calls to this method later on... i.e. You can teach the JIT to see this method and then when the problem is fixed that code can go away because then as you state the asm will be the same. And btw string needs this also: |
Previously considered and rejected. Closing. |
Justification
A helper method along the same lines of
string.IsNullOrEmpty
. We have several dozen places within System.Private.CoreLib alone which perform array null or empty checks: see GrabYourPitchforks@ef639cd for a prototype of this change and a list of candidate call sites.Furthermore, we can perform tricks within
Array.IsNullOrEmpty
similar to what we already do instring.IsNullOrEmpty
. These take advantage of patterns the JIT already recognizes in order to optimize the codegen for code later in the method.For example:
This API obviously isn't required for apps to have correct behavior (devs have gone without it for 20+ years). But it is a convenience method that could add succinctness and clarity to call sites, and it could give a minor perf boost to some scenarios.
API Proposal
There's no need for this method to be generic on T; taking a standard
Array
would be sufficient to cover all use cases.The text was updated successfully, but these errors were encountered: