-
Notifications
You must be signed in to change notification settings - Fork 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
Discussion: lightweight string interpolation #369
Comments
There already is a way to get invariant string interpolation: using static System.FormattableString;
…
var a = Invariant($"{key}: {value}"); Though it's not a constant, so it can't be used in attributes. (And it also allocates even more than normal string interpolation.) And the issue with considering normal string interpolation to be constant is that it is not. It depends on the current culture and for example |
That issue applies to the |
@alrz So you didn't read the proposal I linked. :) I'm fully aware that these are two separate proposals, I just linked because both provide options to string interpolation or more control over it. |
I did, that's why I mentioned the |
Do you have a measurable perf problem caused by string interpolation? If you do, it seems like stepping down and using a |
Just like linq which is a no-go for perf critical code, string interpolation is recommended against in such use cases. Not to mention, it cannot be used as a constant, which is really inconvenient when you need it. |
Ahem... ;-) |
Well, almost all non-trivial scenarios that use string interpolation incur unexpected (by most developers) performace hit. As example, log.Write($"Some diagnostic string with {some} {args} ...") this single line gave us +2.5 Gb total allocations per hour (string formatting was unneeded as logging was disabled in config). Sadly, it's by design and it does not look like a thing to be fixed. |
What the use cases when you have string constants that require interpolation? |
@MgSam |
@ig-sinicyn however, libs like However, I think it should be implemented |
Th optimization went in via dotnet/roslyn#26612 Closing in favor of #384 for constant string interpolations. |
It would translate to simple string concatenations,
No "format specifier" may be used in this context.
If all interpolations are constants, it can be used as a constant:
Note: we use the "result of an invariant
ToString
" for literals to retain constness and culture-invariance.Alternatively, we could just allow the existingAs @HaloFour pointed out in #384 (comment) that would cause different results from a regular vs. constant string interpolation literal which can be confusing.$
interpolations to produce a constant, if the said constraint holds.EDIT: Something like
$"{value}"i
might be easier to understand, and won't look too stupid in verbatim mode:$@"{value}"i
vs.$$@"{value}"
.The text was updated successfully, but these errors were encountered: