Skip to content

IL Generation: struct and struct records methods are slower #5136

@thinkbeforecoding

Description

@thinkbeforecoding

Doing a diff between structs and struct records generated code, I noticed that an extra copy is done in methods using copy and update.

Repro steps

  • Create a struct type
  • Create a struct record
  • implement the same method that copy and update the type
  • compare IL both
[<Struct>]
type Structure = 
    val Line: int
    val OriginalLine: int
    val StartOfLineAbsoluteOffset: int
    new(l,ol,s) = { Line =l; OriginalLine = ol; StartOfLineAbsoluteOffset = s }
    member x.NextLine =
        Structure(x.Line+1, x.OriginalLine+1,x.StartOfLineAbsoluteOffset)
[<Struct>]
type Rec = {
    Line: int
    OriginalLine: int
    StartOfLineAbsoluteOffset: int
} with
    member x.NextLine =
        { x with Line = x.Line + 1; OriginalLine = x.OriginalLine+1}

Expected behavior

IL should be the same

Actual behavior

There is a few extra instructions for struct records:

  .maxstack  5
  .locals init (valuetype Program/Rec V_0)
  IL_0000:  ldarg.0
  IL_0001:  ldobj      Program/Rec
  IL_0006:  stloc.0

the rest is exactly similar.

Known workarounds

Use struct types.

Related information

Is this needed ? The Structure.NextLine seems ok without it.

These lines seems generated by a call to mkAddrGet function (TastOps.fs) for byref value types.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions