Skip to content

Parameter name with leading capital leads to less efficient code #5646

Closed

Description

@simoncowen88

When uing a parameter name that begins with a capital letter, different IL is generated for a function.
In some cases this appears to be simply slightly less efficient, in other this causes a closure to be unnecessarily created.

When code is being written by the mathematically inclined, the single-letter upper-case parameters are not wholy uncommon.

Repro steps

Compile this code:

let f a b = a + b
let g A b = A + b

With optimisations these generate the same IL, however observe the IL using ILSpy or run a profiler on these functions when compiled unoptimised:

.method public static 
	int32 f (
		int32 a,
		int32 b
	) cil managed 
{
	.custom instance void [FSharp.Core]Microsoft.FSharp.Core.CompilationArgumentCountsAttribute::.ctor(int32[]) = (
		01 00 02 00 00 00 01 00 00 00 01 00 00 00 00 00
	)
	// Method begins at RVA 0x205c
	// Code size 5 (0x5)
	.maxstack 8

	IL_0000: nop
	IL_0001: ldarg.0
	IL_0002: ldarg.1
	IL_0003: add
	IL_0004: ret
} // end of method Main::f
.method public static 
	int32 g (
		int32 A,
		int32 b
	) cil managed 
{
	.custom instance void [FSharp.Core]Microsoft.FSharp.Core.CompilationArgumentCountsAttribute::.ctor(int32[]) = (
		01 00 02 00 00 00 01 00 00 00 01 00 00 00 00 00
	)
	// Method begins at RVA 0x2064
	// Code size 7 (0x7)
	.maxstack 4
	.locals init (
		[0] int32
	)

	IL_0000: ldarg.0
	IL_0001: stloc.0
	IL_0002: nop
	IL_0003: ldloc.0
	IL_0004: ldarg.1
	IL_0005: add
	IL_0006: ret
} // end of method Main::g

Moreover, this becomes more serious in the following case, where even with optimistions enabled we generate IL that creates a closure for g'.

let f' a = fun x -> a + x
let g' A = fun x -> A + x

f' generates the exact same IL as f above, however with optimisation enabled g' generates:

.method public static 
	class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<int32, int32> 'g\'' (
		int32 A
	) cil managed 
{
	// Method begins at RVA 0x2064
	// Code size 8 (0x8)
	.maxstack 8

	IL_0000: nop
	IL_0001: ldarg.0
	IL_0002: newobj instance void Main/'g\'@7'::.ctor(int32)
	IL_0007: ret
} // end of method Main::'g\''
.class nested assembly auto ansi serializable beforefieldinit 'g\'@7'
	extends class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<int32, int32>
{
	// Fields
	.field public int32 A

	// Methods
	.method assembly specialname rtspecialname 
		instance void .ctor (
			int32 A
		) cil managed 
	{
		// Method begins at RVA 0x2070
		// Code size 14 (0xe)
		.maxstack 8

		IL_0000: ldarg.0
		IL_0001: call instance void class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<int32, int32>::.ctor()
		IL_0006: ldarg.0
		IL_0007: ldarg.1
		IL_0008: stfld int32 Main/'g\'@7'::A
		IL_000d: ret
	} // end of method 'g\'@7'::.ctor

	.method public strict virtual 
		instance int32 Invoke (
			int32 x
		) cil managed 
	{
		// Method begins at RVA 0x2080
		// Code size 10 (0xa)
		.maxstack 8

		IL_0000: nop
		IL_0001: ldarg.0
		IL_0002: ldfld int32 Main/'g\'@7'::A
		IL_0007: ldarg.1
		IL_0008: add
		IL_0009: ret
	} // end of method 'g\'@7'::Invoke

} // end of class g'@7

Expected behavior

The capitalisation of the first letter of parameter names should not affect the generate IL.

Actual behavior

Capitalising the first letter of a parameter name may lead to inefficient IL being generated.

Known workarounds

Make the first letter of all of your parameters lower-case.

Related information

  • Windows 10
  • .NET Framework 4.6.1
  • F# Compiler version 14.0.23020.0

Activity

  1. added this to the 16.0 milestone on Sep 18, 2018
  2. modified the milestones: 16.0, 16.1 on Feb 21, 2019
  3. modified the milestones: 16.1, 16.2 on Apr 23, 2019
  4. modified the milestones: 16.2, Backlog on Apr 30, 2019
  5. added
    Impact-Low
    on Aug 26, 2020
  6. dsyme commented on Apr 1, 2022

    @dsyme
    Contributor

    I've checked on main and these generate the same IL now - various optimization improvements must have fixed this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugImpact-Low

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions