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

Compile interpolated strings properly for better performance #1346

Closed
charlesroddie opened this issue Jan 8, 2024 · 2 comments
Closed

Compile interpolated strings properly for better performance #1346

charlesroddie opened this issue Jan 8, 2024 · 2 comments

Comments

@charlesroddie
Copy link

charlesroddie commented Jan 8, 2024

The code

let world = "world"
let helloWorld = $"hello {world}" compiles to

Currently compiles to (see SharpLab)

object[] array = new object[1];
array[0] = "world";
PrintfModule.PrintFormatToStringThen(new PrintfFormat<string, Unit, string, string, string>("Hello %P()", array, null));

Instead, it should compile to one of these, and testing suggests that Impl2 is better

let helloWorldImpl1() =
    String.Format("Hello {0}", "world")
let helloWorldImpl2() =
    let sb = Text.StringBuilder()
    sb.Append("hello") |> ignore
    sb.Append(world) |> ignore
    sb.ToString()

Pros and Cons

The advantages of making this adjustment to F# are

  1. Performance.

https://github.com/charlesroddie/BenchmarksFsharp/tree/InterpolatedStrings

Method Mean Error StdDev Gen0 Allocated
InterpolatedStringCurrent 88.20 ns 1.764 ns 1.650 ns 0.0219 184 B
Impl1 36.03 ns 0.757 ns 0.632 ns 0.0057 48 B
Impl2 18.30 ns 0.416 ns 0.841 ns 0.0181 152 B
  1. Complex generic instantiations are not surfaced, helping the size of apps when AOT compiled. We previously found that this was several MB! But would need to retest with current NativeAOT.

  2. The current code requires reflection, making it harder for compilers to reason about, contributing to poor performance, contributing to 2 by bringing in more reflection data, and being incompatible with reflection-free modes.

The disadvantages of making this adjustment to F# are ...

None

Extra information

Estimated cost (XS, S, M, L, XL, XXL):

M

Related suggestions: (put links to related suggestions here)

#919
dotnet/fsharp#13398

Affidavit (please submit!)

Please tick these items by placing a cross in the box:

  • [ x ] This is not a question (e.g. like one you might ask on StackOverflow) and I have searched StackOverflow for discussions of this issue
  • [ x ] This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository
  • [ x ] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it
  • [ x ] I have searched both open and closed suggestions on this site and believe this is not a duplicate

Please tick all that apply:

  • [ x ] This is not a breaking change to the F# language design
  • [ x ] I or my company would be willing to help implement and/or test this

For Readers

If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.

@kerams
Copy link

kerams commented Jan 9, 2024

#1108 should be preferred. Implementing string builders for simple cases, while valuable, could result in redundant work otherwise.

@charlesroddie
Copy link
Author

Ah you are right. Closing as a duplicate and I'll past the info there.

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

No branches or pull requests

2 participants