-
Notifications
You must be signed in to change notification settings - Fork 21
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
Emit intermediate functions with direct method parameters instead of tuple parameters when possible #1084
Comments
I would love to get some thoughts on whether or not this could be done in a backward-compatible way. If this change were to be implemented directly, it would be a problem if |
Also, it's good to note that another workaround is explicit ValueTuples: module MyModule
let f1 (struct (x:float, y:float, z:float)) = x+y+z
let f2 (struct (x:float, y:float, z:float)) = x*y*z
let g flag =
if flag then f1
else f2
let h () =
let func = g true
printfn "%A" (func (4., 5., 6.)) Also just want to elaborate that this all still applies when |
Potentially related to #1083 |
Hi @jwosty It's not so easy to change this, nor would I expect allocation-free programming for cases like the above. We could only enable it in some cases at considerable complexity. It is true that iterated curried application is generally allocation-free for up to three simultaneous arguments, though that comes at the cost of a type test on the function object that can itself have runtime implications. #1083 is not really related but I see why you linked it. |
I'll close this, partly because it's a proposed compiler optimization not a language change. ANd also because of my comment above |
Interesting, can you elaborate on what type test it has to do? I didn't see one happening, but maybe I missed it. |
I just realized I never actually mentioned that wasn't just a hypothetical for me; I had actually found this as the culprit to a memory problem I was having, in a real-time audio application (GC collections pausing the callback is a no-no if you can help it). I don't remember the specifics right now, but switching it to a curried function ended up fixing the GC pausing problems I was having. Had to include a comment lest I ever forget while refactoring. I think I also misinterpreted your response as "we'll never do this;" I realize you were really saying "this doesn't belong in this repo and also it might be hard." I'll be reopening in the language repo |
In the following situation:
(Sharplab.io compilation)
F# emits intermediate lambdas inside
g
for each function returned. These lambdas are of typeFSharpFunc<Tuple<double, double, double>, double>
. I propose that the compiler instead generates lambdas type either ofFSharpFunc<double,double,double,double>
orFSharpFunc<ValueTuple<double,double,double>,double>
to avoid unnecessary heap allocations.This can be fixed, oddly enough, by currying
f1
andf2
:Alternatively:
(Sharplab.io compilation)
Pros and Cons
The advantages of making this adjustment to F# are:
The disadvantages of making this adjustment to F# are ...
Extra information
Estimated cost (XS, S, M, L, XL, XXL): M
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
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.
The text was updated successfully, but these errors were encountered: