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

[IL Emit] Improve il code emitting when working with loops #12138

Closed
En3Tho opened this issue Sep 10, 2021 · 1 comment · Fixed by #12959
Closed

[IL Emit] Improve il code emitting when working with loops #12138

En3Tho opened this issue Sep 10, 2021 · 1 comment · Fixed by #12959

Comments

@En3Tho
Copy link
Contributor

En3Tho commented Sep 10, 2021

I'm not sure whether this might be F# imporvement or JIT.. Basically I will duplicate this in runtime repo (dotnet/runtime#58941) too.

Consider these 2 methods:

[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
let fold initial folder (enumerator: #IEnumerator<'i>) =
    let folder = OptimizedClosures.FSharpFunc<_,_,_>.Adapt folder
    let mutable enumerator = enumerator
    let mutable result = initial
    while enumerator.MoveNext() do
        result <- folder.Invoke(result, enumerator.Current)
    result
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TResult Fold<TResult, TItem, TEnumerator>(TResult result, FSharpFunc<TResult, FSharpFunc<TItem, TResult>> folder, TEnumerator enumerator)
            where TEnumerator : IEnumerator<TItem>
{
    var fSharpFunc = OptimizedClosures.FSharpFunc<TResult, TItem, TResult>.Adapt(folder);
    var enumerator2 = enumerator;
    var result2 = result;
    while (enumerator2.MoveNext())
        result2 = fSharpFunc.Invoke(result2, enumerator2.Current);

    return result2;
}

They look very similar but there is an importnat il emit difference:

C# method is compiled to this basically:

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TResult FoldRoslynVersion<TResult, TItem, TEnumerator>(TResult result, FSharpFunc<TResult, FSharpFunc<TItem, TResult>> folder, TEnumerator enumerator)
            where TEnumerator : IEnumerator<TItem>
{
    var fSharpFunc = OptimizedClosures.FSharpFunc<TResult, TItem, TResult>.Adapt(folder);
    var enumerator2 = enumerator;
    var result2 = result;
    goto movenext;

    logic:
    result2 = fSharpFunc.Invoke(result2, enumerator2.Current);

    movenext:
    if (!enumerator2.MoveNext())
        return result2;

    goto logic;
}

While F# is compiled to this basically:

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TResult FoldFSharpVersion<TResult, TItem, TEnumerator>(TResult result, FSharpFunc<TResult, FSharpFunc<TItem, TResult>> folder, TEnumerator enumerator)
            where TEnumerator : IEnumerator<TItem>
{
    var fSharpFunc = OptimizedClosures.FSharpFunc<TResult, TItem, TResult>.Adapt(folder);
    var enumerator2 = enumerator;
    var result2 = result;

    movenext:
    if (!enumerator2.MoveNext())
        goto exit;

    result2 = fSharpFunc.Invoke(result2, enumerator2.Current);
    goto movenext;

    exit:
    return result2;
}

While difference might be non obvious, C# version with condition at the end of the method results in 10-15% perf imporvement while having the same assembly size.

image

Can F# compiler emit better IL here?

@dsyme
Copy link
Contributor

dsyme commented Sep 13, 2021

Yes it makes sense to emit the same code as C# with the loop check at the end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants