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 sprintf where possible #8621

Closed
charlesroddie opened this issue Feb 23, 2020 · 1 comment
Closed

Compile sprintf where possible #8621

charlesroddie opened this issue Feb 23, 2020 · 1 comment
Labels
Area-Library Issues for FSharp.Core not covered elsewhere Feature Request

Comments

@charlesroddie
Copy link
Contributor

charlesroddie commented Feb 23, 2020

String formatting functions sprintf, printf, etc. are popular in F# code, but currently slow and aot-unfriendly, implemented using reflection and dynamic codegen.

They should be implemented with consideration for performance, and for compatibility in reflection-constrained environments.

Most commonly sprintf occurs in conjunction with a known format string. Therefore one approach would be to recognize the pattern sprintf specificStringFormat and compile this combination.

Pros: performance and aot-compatbility
Cons: adds code to maintain, as removing the existing approach would be a breaking change.

type R = { Field:int }
let printR:R -> string =
    // Matches `sprintf specificStringFormat` so is compiled
    sprintf "r is %A"
let printTwice<'a>(f:Printf.StringFormat<'a->string>) (x:'a) =
    let s =
        // Not compiled and uses existing approach, since `f` is not known statically.
        sprintf f x
    s + s
@charlesroddie
Copy link
Contributor Author

Closing temporarily in favour of fsharp/fslang-suggestions#919 which is likely to be more natural to implement in the short term and a stepping stone to sprintf compilation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Library Issues for FSharp.Core not covered elsewhere Feature Request
Projects
None yet
Development

No branches or pull requests

2 participants