-
Notifications
You must be signed in to change notification settings - Fork 5k
Should Vector<T>.Count be readonly? #16004
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
Comments
Just from a style perspective we should probably change this to a readonly field. As for the JIT'd code, I'm not sure there's a guarantee it will be treated as a constant even with the change, although I would think it much more likely/possible in that case. Perhaps @CarolEidt can chime in. The special-case SIMD code-path surely has very special handling for this member in particular. |
Sadly, I don't believe that the JIT does anything special with readonly fields. This is something I think would be potentially quite useful to do - i.e. if the class constructor has been called, then the JIT could get the value and use it as a constant. Caveat - even though I'm currently the "keeper" of the spec, I haven't done the due diligence to validate that it would actually be legal to do so (i.e. I think, but don't know for certain, that readonly is more than just a "hint"). All that said, I agree that making it readonly is the right thing to do. |
My understanding is that it actually does for readonly statics of primitive types, doing exactly what you cite: "if the class constructor has been called, then the JIT could get the value and use it as a constant". |
Excellent - I just found that in the importer code! All the more reason to make this readonly. |
Though various iterations aspnet/KestrelHttpServer#524 (comment) I've found the first access to the After the first access all the later jitted functions use consts. So; the trick seems to be to access the |
Would probably be better it the jitter just resolved the value/function and dropped the guard checks; as if its compiling the function it likely means its just about to be run; though I don't know if it has the ability to run the code its generating. |
It sounds like you're talking about the differences implied by beforefieldinit, which the C# compiler will emit on types that lack an explicit cctor.
Which guard checks? The JIT will use constant propagation and the like to eliminate unnecessary blocks based on (limited) compilation-time analysis. |
vs (the
Yes, though I don't fully understand the issues was a little confused on by the advice around it; which seemed to be: Might be a different scenario with |
I'm not seeing that. I grabbed the class from https://github.com/benaadams/KestrelHttpServer/blob/dc9f5c433b934bbfd75772e39013891137e77b24/src/Microsoft.AspNet.Server.Kestrel/Http/ReasonPhrases.cs and put it into a console app that just does Console.WriteLine(ReasonPhrases.ToStatusCode(1234)), and I get this:
Am I misunderstanding? |
Coreclr x64? I did see different behaviour with assigning |
Also might need to be an instance method rather than static method to exhibit the behaviour. |
Hmm, actually, I was using the full framework x64 (.NET 4.6.1). @CarolEidt, is there something that would make .NET 4.6.1 64-bit different from coreclr x64 in this regard? I just tried with the latest coreclr x64, and I'm seeing the worse result, whereas with .NET 4.6.1 I see the better result. |
@stephentoub @CarolEidt added coreclr issue https://github.com/dotnet/coreclr/issues/2526 |
If hardware accelerated is false will
s_count
still be Jitted to a const even though its notreadonly
? Or should it be readonly?The text was updated successfully, but these errors were encountered: