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>
showsDeclaredFields = [|Int32 i|]
typeof<B<int>>
showsDeclaredFields = [|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.