-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Is/can format strings benefit from interpolated string optimizations? #59567
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsAfter #50601, #50635, #51962 we can string message = string.Format(CultureInfo.CurrentCulture, "Current path: {0}", path); convert to: string message = string.Format(CultureInfo.CurrentCulture, $"Current path: {path}"); and my question is about follow common pattern: string message = string.Format(CultureInfo.CurrentCulture, ResourceManager.GetString("Current_Path"), path); where ResourceManager returns a localized version of "Current path: {0}". Since ResourceManager returns the same string (until we change current culture that is very rarely) we could parse the format string once and cache/compile. Also in compile time we know all about argument and their types and I guess we could do the same optimizations as for interpolated strings. In PowerShell we have a lot of such patterns with format strings from resources and only a few ones as static (which could be converted to interpolated string already today) and the optimization I ask could be very useful. (I found similar dotnet/csharplang#2347 but that discussion was in another context.)
|
Duplicate of #50330 |
@stephentoub Thanks for pointing #50330. I think #50330 (as well as dotnet/csharplang#2347) discussion goes a little in a different direction. My reasoning comes from PowerShell. PowerShell uses strong typed resources. This means that there is already a code generated at (before :-) ) compile time. But the code is still a wrapper around traditional resource strings. Thus string.Format(culture, FileSystemStrings.PathNotFound, path); will be compiled to: FileSystemStrings.PathNotFound(culture, path); with (simplified) implementation: class FileSystemStrings
{
public PathNotFound(CultureInfo culture, string path)
{
string.Format(culture, $"Path not found: {path}.");
}
} Of course we need an analog of ResourceManager class to discover culture specific assembly, load it and cache result by culture as ResourceManager does. I guess C# compiler could be so smart and does the work for us if FileSystemStrings.PathNotFound parameter would be annotated with new string interpolated attribute. At the same time to convert the resource file is probably better with a source generator. This, of course, is not the final sentence, but just the initial considerations. |
After #50601, #50635, #51962 we can
convert to:
and my question is about follow common pattern:
where ResourceManager returns a localized version of "Current path: {0}".
Since ResourceManager returns the same string (until we change current culture that is very rarely) we could parse the format string once and cache/compile. Also in compile time we know all about argument and their types and I guess we could do the same optimizations as for interpolated strings.
In PowerShell we have a lot of such patterns with format strings from resources and only a few ones as static (which could be converted to interpolated string already today) and the optimization I ask could be very useful.
(I found similar dotnet/csharplang#2347 but that discussion was in another context.)
The text was updated successfully, but these errors were encountered: