-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
bypass Format on simple constant interpolated strings #6738
Conversation
When an interpolated string doesn't have alignment or formatString parameters it is faster to instead use Concat. Further optimizations can be done on calls to Concat with only one parameter. Fixes #6669
Will this also take care of #4678? You might also be interested in #4678 (comment) to remove some boxing, but that can wait for another time 😄 |
this does not take care of #4678 (comment) (boxing of value types for the purposes of calling There are many pedantic cases it doesn't take care of either, for example
Further it does not attempt to intercept and look at other calls of It does handle:
|
I can see just from your description that the implementation is not correct. Please see the specification for composite formatting. For an argument whose runtime type implements the Even for simple strings, it is not correct to simply insert the value into the result. One must first get an I believe that composite formatting is complicated enough that it is not worth attempting this category of "optimization". Generating correct code for even the simplest cases is unreasonably complex and will generate a huge amount of code. |
@gafter based on the reference source and core clr it looks like I have to process each arg as:
unless it is a TimeSpan and Is that worth doing? Edit: in this case I could handle formatString parameters as well:
Edit2: technically I could do alignment as well with a helper method emitted in source or some other way to create a reference: align not provided or 0:
align positive:
align negative:
|
According to the specification you must get an That code is not necessarily the code that will run on all platforms. Different platforms factor the lookup of the current culture into different layers of the API calls. The compiler must obey the specification to ensure it can interoperate correctly with any factoring of the platform implementation. |
👎 I'm voting for not making the compiler more complex. Who writes |
When you call I'm not saying it is worth doing, even the expressions above would add significant weight to the CIL for any nontrivial input. Edit: even though this code ( I could still do something about interpolated strings containing strings and null, but that seems so specific to the point I agree with @tmat. |
Perhaps if we really wanted to speed this up, a core library change that created those expressions as aggressively inlined functions and then those functions were used, it might be a decent path. It all sounds messy. |
Per the documentation for IFormattable, when the second parameter to ToString is null, it is up to the implementation of that interface to get the correct one for the current thread. I think a base class library (or something equivalent) set of methods:
(perhaps a few others to make inlining work right; I am not well versed on JIT details to make the suggestion) Then where this is defined, interpolated literals could reduce to a I'm going to pass on trying to get that done. |
@gafter |
@AdamSpeight2008 I'm not particularly concerned about how someone uses interpolated strings, so much as the idea that this very useful feature to make your code easier to follow is currently a bad refactoring decision to make inside a performance critical scenario (places like this one in Kestral). Interpolated strings even as simple as |
When an interpolated string doesn't have alignment or formatString parameters it is faster to instead use Concat. Further optimizations can be done on calls to Concat with only one parameter.
Fixes #6669