The F # compiler supports foreach optimization in the same way as the C # compiler

This is a detailed answer on how the C # compiler optimizes foreachwhen it IEnumerator<T>is a mutable structure.

Does the F # compiler perform the same optimization?

+4
source share
1 answer

AFAICT F # seems to handle valuetype enums in the same way as C #:

Here's a parsed snippet of IL code from a simple C # program that uses foreach through IEnumerable<T>

.locals init (
    [0] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>,
    [1] int32 v
)
IL_0025: ldloca.s 0
IL_0027: call instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>::MoveNext()

Notice what local 0the valuetype type is and what it uses ldloca.sto load the structure address.

Compare this with F #

.locals init (
    [0] class [mscorlib]System.Collections.Generic.List`1<int32> ra,
    [1] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>
)
IL_000e: ldloca.s 1
IL_0010: call instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>::MoveNext()

valuetype local 1 ldloca.s .

: F # , # . F # F #, , . , F # . #, F # .

IList, , , - IList "" , .

+5

Source: https://habr.com/ru/post/1624175/


All Articles