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

Optimize interpolated string with no holes #11632

Merged
merged 4 commits into from
Jun 29, 2021
Merged

Conversation

kerams
Copy link
Contributor

@kerams kerams commented Jun 4, 2021

let stringOnly () = $"no hole" 
let printed () = printf $"printed no hole"

becomes

public static string stringOnly()
{
    return "no hole";
}

public static void printed()
{
    PrintfFormat<Unit, TextWriter, Unit, Unit> format = new PrintfFormat<Unit, TextWriter, Unit, Unit, Unit>("printed no hole");
    PrintfModule.PrintFormatToTextWriter(Console.Out, format);
}

instead of

public static string stringOnly()
{
    return PrintfModule.PrintFormatToStringThen(new PrintfFormat<string, Unit, string, string, Unit>("no hole", new object[0], null));
}

public static void printed()
{
    PrintfFormat<Unit, TextWriter, Unit, Unit> format = new PrintfFormat<Unit, TextWriter, Unit, Unit, Unit>("printed no hole", new object[0], null);
    PrintfModule.PrintFormatToTextWriter(Console.Out, format);
}

@kerams
Copy link
Contributor Author

kerams commented Jun 4, 2021

If I were to create a reduction of sprintf "xxx" into "xxx", where would be the place to do it? I don't see how in CheckExpressions, so then I considered OptimizeApplication, checking for sprintf and 'constant' format, but it seems incredibly specific compared to the what the function does.

@charlesroddie
Copy link
Contributor

charlesroddie commented Jun 4, 2021

$"no hole" is terrible code. Instead of making it perform better, adding a 10s delay would be an appropriate solution, a fair punishment for writing this.

An analyzer is the correct solution isn't it?

@kerams
Copy link
Contributor Author

kerams commented Jun 5, 2021

Yes, it's pointless and no one sets out to write that initially, but I can imagine a scenario where there was actual interpolation, then someone removed it, but forgot the $. The same with sprintf "xxx", perhaps less likely.

@charlesroddie
Copy link
Contributor

Yes, and the right solution is to help fix the code (using analyzers) rather than work around it. Isn't it?

@kerams
Copy link
Contributor Author

kerams commented Jun 5, 2021

I'd say a compiler/analyzer warning would be complementary, and concerned with code style rather than performance. There's no reason we should emit suboptimal code if the user does not notice or heed the warning (when it's this easy to fix, to be precise). If you consider one extra character a crime against humanity, by all means go ahead and also implement an analyzer. I'm not fussed myself.

@cartermp
Copy link
Contributor

cartermp commented Jun 5, 2021

I think this is a fine addition. We're not in the business of punishing people for writing what someone might personally find to be bad code.

@vzarytovskii vzarytovskii requested a review from dsyme June 28, 2021 13:59
@dsyme
Copy link
Contributor

dsyme commented Jun 29, 2021

We're not in the business of punishing people for writing what someone might personally find to be bad code.

:) let mutable x = ... 🤣

let flexes = argTys |> List.map (fun _ -> false)
let fillExprs, tpenv = TcExprs cenv env m tpenv flexes argTys synFillExprs
if List.isEmpty synFillExprs then
let str = mkString g m printfFormatString
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory this changes the quotation form for an interpolated string. I don't mind that change, however could you add a test in tests/fsharp/core/quotes/test.fsx or elsewhere to test for that and pin down the new form please?

e.g. check results of <@ $"abc" @> and you may as well add a test for <@ $"abc {1} def" @> while you're at it.

Copy link
Contributor

@dsyme dsyme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, just add a test for quotation forms please

@kerams
Copy link
Contributor Author

kerams commented Jun 29, 2021

Done. Will the test run automatically or do I need to wire it up somewhere?

@dsyme
Copy link
Contributor

dsyme commented Jun 29, 2021

Done. Will the test run automatically or do I need to wire it up somewhere?

All of the initialization code of quotes/test.fsx runs automatically as one test

Copy link
Contributor

@dsyme dsyme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, subject to green ticks

@vzarytovskii vzarytovskii merged commit 4378143 into dotnet:main Jun 29, 2021
@kerams kerams deleted the interp branch June 29, 2021 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants