Fable: Q: Inlining?!

Created on 19 Oct 2017  路  14Comments  路  Source: fable-compiler/Fable

http://fable.io/repl/#?code=%0D%0Alet%20inline%20f%20g%20b%20%3D%20g%20b%0D%0A%0D%0Alet%20h%20x%20%3D%20f%20(fun%20a%20-%3E%20a%20*%202)%20x%0D%0A%0D%0Aprintfn%20%22%25d%22%20%3C%7C%20h%204

image

I would have expected somthing like:

import { printf } from "fable-core/String";
export function h(x) {
  return 2 * x | 0;
}
replLog(printf("%d"))(h(4));

Most helpful comment

as I'm not a maintainer or a core developer there, merely a minor contributor.

@ncave that's really an understatement. You really do an awesome work!

All 14 comments

even worse:

image

http://fable.io/repl/#?code=%0D%0Alet%20inline%20f%20g%20b%20%3D%20g%20b%0D%0A%0D%0Alet%20h%20%3D%20f%20(fun%20a%20-%3E%20a%20*%202)%0D%0A%0D%0Aprintfn%20%22%25d%22%20%3C%7C%20h%204

/cc @ncave @Pauan @alfonsogarciacaro

or even:

import { printf } from "fable-core/String";
replLog(printf("%d"))(8);

I wish we could find a way to use the F# compiler optimizer. /cc @dsyme @ncave

@forki I know that's probably not what you're looking for, but you can also further optimize the generated js with say prepack etc.

No I'm really interested in doing optimization like the compiler does. Like beta reduction and inlining.

@ncave I guess what I'd love to see is FCS providing OptimizeExpr expr as public API and fable using that instead of optimizing itself.

@forki Makes sense, and it can potentially lead to a speedup through optimization in the repl as well. Perhaps opening an issue to add that API in the FCS repo can help, as I'm not a maintainer or a core developer there, merely a minor contributor.

As commented other times, the truth is the AST provided by FCS is very messy. Fable tries to do some optimizations by eliminating many of the compiler generated closures and assignments but this is not always possible without compromising correctness. The optimization to flatten nested lambdas, for example, created many bugs and there're probably still edge cases waiting to be found. About inlining, it just does that, inlining the function body on the call site but Fable doesn't do any code evaluation at compile time (with a couple of exceptions). Actually I tried to do it for #653 and it was surprisingly hard.

It'd be very nice if FCS could optimize the provided AST. I'm not sure how this would affect us now in terms of speed and correctness as Fable already contains many heuristics about the F# AST. I'd have to check.

@ncave I was indeed hoping that something like Prepack could be the key instead of trying to overoptimize the F# or Fable AST. Unfortunately in my attempts with that tool so far I've never managed to get any working code out of it :/

as I'm not a maintainer or a core developer there, merely a minor contributor.

@ncave that's really an understatement. You really do an awesome work!

Thanks @enricosada, I guess what I was trying to say was that Fable is using the official FCS nuget distro, so that new optimizer API needs to be officially exposed (and supported). And while I'm in your favor, do you mind taking a look at this, it's needed so we can stop using netcore 1.0 for some of the Fable repl build tasks? Thanks, I really appreciate your help!

@alfonsogarciacaro Prepack isn't ready yet, it's still in the alpha stages. It doesn't even support things like the DOM yet.

And with Prepack, you need to use their API to declare all global variables, so it's extra work if you're using an API that isn't supported by Prepack.

Prepack is super cool, and I hope it succeeds, but don't hold your breath, it'll be a while before it's ready for use.

Also, fundamentally, the F# compiler has additional (compile-time) information that Prepack does not have, so it can sometimes optimize code better than Prepack.

@Pauan That's right, I tried to make Prepack work with large projects (fable repl) but it's not there yet. We have some rudimentary support for it in the fable splitter, in case you find a use for it in smaller projects, but yes, it's not ready for prime time yet.

Move discussion to PR #1197 and #1266

Was this page helpful?
0 / 5 - 0 ratings