3 let inline funB myFun x = printf "B" myFun x le-6ren">
gpt4 book ai didi

f# - 有时 F# 不会内联函数,即使它被标记为 `inline` ?

转载 作者:行者123 更新时间:2023-12-04 23:11:01 24 4
gpt4 key购买 nike

let inline funA x =
printf "A"
x > 3

let inline funB myFun x =
printf "B"
myFun x

let foo () =
funB funA 7

IL 为 foo
.method public static 
bool foo () cil managed
{
// Method begins at RVA 0x2278
// Code size 31 (0x1f)
.maxstack 4
.locals init (
[0] class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit>
)

IL_0000: nop
IL_0001: ldstr "B"
IL_0006: newobj instance void class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`5<class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit>::.ctor(string)
IL_000b: stloc.0
IL_000c: call class [mscorlib]System.IO.TextWriter [mscorlib]System.Console::get_Out()
IL_0011: ldloc.0
IL_0012: call !!0 [FSharp.Core]Microsoft.FSharp.Core.PrintfModule::PrintFormatToTextWriter<class [FSharp.Core]Microsoft.FSharp.Core.Unit>(class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<!!0, class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit>)
IL_0017: pop
IL_0018: ldc.i4.7
IL_0019: call bool Foo::myFun@21(int32) // Here!
IL_001e: ret
} // end of method Foo::foo

IL 为 myFun@21
    .method assembly static 
bool myFun@21 (
int32 x
) cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x224c
// Code size 29 (0x1d)
.maxstack 4
.locals init (
[0] class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit>
)

IL_0000: ldstr "A"
IL_0005: newobj instance void class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`5<class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit>::.ctor(string)
IL_000a: stloc.0
IL_000b: call class [mscorlib]System.IO.TextWriter [mscorlib]System.Console::get_Out()
IL_0010: ldloc.0
IL_0011: call !!0 [FSharp.Core]Microsoft.FSharp.Core.PrintfModule::PrintFormatToTextWriter<class [FSharp.Core]Microsoft.FSharp.Core.Unit>(class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<!!0, class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit>)
IL_0016: pop
IL_0017: nop
IL_0018: ldarg.0
IL_0019: ldc.i4.3
IL_001a: cgt
IL_001c: ret
} // end of method Foo::myFun@21

IL 为 funA
.method public static 
bool funA (
int32 x
) cil managed
{
// Method begins at RVA 0x2218
// Code size 22 (0x16)
.maxstack 8

IL_0000: nop
IL_0001: ldstr "A"
IL_0006: newobj instance void class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`5<class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit>::.ctor(string)
IL_000b: call !!0 [FSharp.Core]Microsoft.FSharp.Core.ExtraTopLevelOperators::PrintFormat<class [FSharp.Core]Microsoft.FSharp.Core.Unit>(class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<!!0, class [mscorlib]System.IO.TextWriter, class [FSharp.Core]Microsoft.FSharp.Core.Unit, class [FSharp.Core]Microsoft.FSharp.Core.Unit>)
IL_0010: pop
IL_0011: ldarg.0
IL_0012: ldc.i4.3
IL_0013: cgt
IL_0015: ret
} // end of method Foo::funA

如您所见, myFun@21几乎与 funA 相同,我不明白它为什么存在。还有为什么函数没有内联?是因为我将一个函数作为参数传递给另一个函数吗?在这种情况下,我认为没有任何难以解决的闭包问题使其无法内联。

尽管函数被标记为 inline,但当函数无法内联时,是否有任何资源可以让我了解更多信息? ?

最佳答案

我相信该函数是内联的,只是不像您期望的那样。 funA只有在有参数时才能内联。由于您没有为 funA 提供参数在使用时,编译器将其视为 fun x -> funA x和内联 funA那里 - 所以一个 lambda 被创建,就像你写的一样:

let foo() = 
printf "B"
(fun x ->
printf "A"
x > 3) 7

关于f# - 有时 F# 不会内联函数,即使它被标记为 `inline` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com