Skip to content
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

Closed
iSazonov opened this issue Sep 24, 2021 · 4 comments
Closed

Is/can format strings benefit from interpolated string optimizations? #59567

iSazonov opened this issue Sep 24, 2021 · 4 comments
Labels

Comments

@iSazonov
Copy link
Contributor

After #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.)

@iSazonov iSazonov added the tenet-performance Performance related issue label Sep 24, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Sep 24, 2021
@dotnet-issue-labeler
Copy link

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.

@ghost
Copy link

ghost commented Sep 24, 2021

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

After #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.)

Author: iSazonov
Assignees: -
Labels:

area-System.Runtime, tenet-performance, untriaged

Milestone: -

@stephentoub
Copy link
Member

Duplicate of #50330

@stephentoub stephentoub marked this as a duplicate of #50330 Sep 24, 2021
@iSazonov
Copy link
Contributor Author

@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.
Traditional resource strings are de-facto constants known at compile time. So we need to do next step and convert the resource strings to a code.
(Not all resource strings are an interpolated strings. So perhaps we need to explicitly annotate them as interpolated strings or/and put in new source resource file.)

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.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
@tannergooding tannergooding removed the untriaged New issue has not been triaged by the area owner label Jun 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants