19
votes

Using F# Interactive, you can verify the following sizes:

// sizeof<A> = 4 bytes
type A (i: int) = struct end

// sizeof<B<int>> = 8 bytes (use any type parameter)
type B<'T> (i: int) = struct end

The reason for the extra size seems to be the presence of an integer __dummy field in the generic case. Using F# Interactive again, you can see this using typeof:

  • typeof<A> shows DeclaredFields = [|Int32 i|]
  • typeof<B<int>> shows DeclaredFields = [|Int32 i; Int32 __dummy|]

I don't understand why this __dummy field has been added.

I think the code responsible for adding it is here:

https://github.com/fsharp/FSharp.Compiler.Service/blob/master/src/fsharp/ilxgen.fs

Line 6377 shows this:

if requiresExtraField then 
    yield mkILInstanceField("__dummy",cenv.g.ilg.typ_int32,None,ILMemberAccess.Assembly) ]

Line 6290 is where requiresExtraField is defined:

let requiresExtraField = 
    let isEmptyStruct = 
        (match ilTypeDefKind with ILTypeDefKind.ValueType -> true | _ -> false) &&
        // All structs are sequential by default 
        // Structs with no instance fields get size 1, pack 0
        tycon.AllFieldsAsList |> List.exists (fun f -> not f.IsStatic)

    isEmptyStruct && cenv.opts.workAroundReflectionEmitBugs && not tycon.TyparsNoRange.IsEmpty

I assume that isEmptyStruct is supposed to mean that the struct does not have any instance fields. But the code as written is testing whether the struct does have any instance fields, which for most structs, including mine, is going to be true. I think the last part of the final test is whether there are any generic type parameters. So requiresExtraField is false for type A (not generic) and true for type B (generic type).

Is this a compiler bug, or is the code correct? If it is correct, then what's the purpose of this __dummy field? Is there some way I can avoid having it?

As another test, I removed my one and only instance field, and not surprisingly, I got the following sizes, showing that the __dummy field was no longer added:

// sizeof<AA> = 1
type AA = struct end

// sizeof<BB<int>> = 1
type BB<'T> = struct end

The reason I want to have a value type, rather than a reference type, is that I will be storing lots of these objects in my data structures, not just passing them around.

1
"cenv.opts.workAroundReflectionEmitBugs" suggests that this is a workaround to some reflection emit bug. Have you checked if C# does the same thing? - A quick look at the code history shows that this dates back to at least November 2010, F# 2.0 - the earliest version of the code available online AFAIK. - Asik
Since sizeof<B<char>> = 4 in a program, is its size in the interactive really an issue for you? - jyoung
Type the lines let f()=1;; let g()=f();; let f()=2;; g();; You'll see that g() is still 1. So a 'new' f() was (incrementally) added to the assembly, but the 'old' f() is still there being referenced by the previous increments of the code. - jyoung
bananasareyellow, I would invite @JYoung to post the answer as an answer, so credit can be given where credit is due. If JYoung is unconcerned about reputation and declines the invitation, you could post the answer yourself and accept that. - phoog
Go ahead bananasareyellow, post the answer. You understand it better than me now. - jyoung

1 Answers

1
votes

The explanation is given by @jyoung in the comments below my original posting.

The last line of requiresExtraField also tests cenv.opts.workAroundReflectionEmitBugs. This flag appears to be set in fscopts.fs. The line of code is:

workAroundReflectionEmitBugs=tcConfig.isInteractive; // REVIEW: is this still required?

So the problem of the extra __dummy field only occurs in F# Interactive.