Fsharp: `[||]` should compile to System.Array.Empty<object>

Created on 19 Sep 2019  Â·  18Comments  Â·  Source: dotnet/fsharp

Array clamps should compile to System.Array.Empty<T>() instead of new T[0];

This causes unnecessary object allocations.
In fact FsharpList already compiles to FSharpList.Empty

Provide the steps required to reproduce the problem:

  1. Create a new project with .net core 2.1 and fsharp.core 4.7

Create a type like this

type TestType =
    member __.EmptyArray() = [||]

Expected behavior
Should compile to

System.Array.Empty<T>()

Actual behavior
inspect the generated C# and it looks like this

public b[] EmptyArray<b>()
        {
            return new b[0];
        }

Known workarounds

None

Related information

Provide any related information (optional):

  • Win 10 x 64
  • .NET core 2.1 Fsharp Core 4.7
  • VS 2019 16.2
Area-Compiler Feature Improvement

Most helpful comment

They are not horrible. Just little creepy. It's all fine if we don't feed
them after midnight.

Phillip Carter notifications@github.com schrieb am Sa., 21. Sep. 2019,
18:32:

Yes, though I'd prefer to avoid any and all ifdefs given how many of those
horrible things we have

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/fsharp/issues/7595?email_source=notifications&email_token=AAAOANFIGGKA5PJPN3MHLY3QKZEDPA5CNFSM4IYGDVQKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7IVDEQ#issuecomment-533811602,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAOANHJYUNVWZYJBDRHJWTQKZEDPANCNFSM4IYGDVQA
.

All 18 comments

Maybe dumb question but what's the benefit?

Benney Au notifications@github.com schrieb am Do., 19. Sep. 2019, 06:02:

Array claims should compile to System.Array.Empty() instead of new T[0]
;

Provide the steps required to reproduce the problem:

  1. Create a new project with .net core 2.1 and fsharp.core 4.78
    2.

Create a type like this

type TestType =
member __.EmptyArray() = [||]

Expected behavior
Should compile to System.Array.Empty()

Actual behavior
inspect the generated C# and it looks like this

public b[] EmptyArray()
{
return new b[0];
}

Known workarounds

Provide a description of any known workarounds.

Related information

Provide any related information (optional):

  • Win 10 x 64
  • .NET core 2.1 Fsharp Core 4.7
  • VS 2019 16.2

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/fsharp/issues/7595?email_source=notifications&email_token=AAAOANB32GOUYTZYKEUVKFDQKL2VFA5CNFSM4IYGDVQKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HMJU5QQ,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAOANB53QDKBI3VDS5SLTDQKL2VFANCNFSM4IYGDVQA
.

@forki Array.Empty<T> caches the array which helps reduce allocations.
The empty array is immutable so this is perfectly safe.
https://github.com/dotnet/coreclr/blob/ef1e2ab328087c61a6878c1e84f4fc5d710aebce/src/mscorlib/src/System/Array.cs#L1062-L1069

Thanks

Robert Jeppesen notifications@github.com schrieb am Do., 19. Sep. 2019,
08:47:

@forki https://github.com/forki Array.Empty caches the array which
helps reduce allocations.
The empty array is immutable so this is perfectly safe.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/fsharp/issues/7595?email_source=notifications&email_token=AAAOANFFERZCAC7OPVDSTZ3QKMOBRA5CNFSM4IYGDVQKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7CNDDI#issuecomment-532992397,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAOANAWMFEIKHJWRJSSUA3QKMOBRANCNFSM4IYGDVQA
.

Wondering if String.Empty versus "" is exhibiting the same issue?

thanks @rojepp I guess I'll turn to you on allocation issues :)

@smoothdeveloper @rojepp @forki I checked FSharpList and it uses the optimised static member FSharpList.Empty

Yes I know we do it for lists. Just wanted to make sure it's the same pattern

It's currently done this way due to we'd had to target net45 in compiler and FCS for a broader reach and EmptyArray`1 type was only introduced in net461. Since we've switched to a newer target framework in the compiler we can now use the singleton, it's just needs to be done by someone. :)

@auduchinok would be happy to contribute if you gave some pointers on where to look to improve this?

This could be done for F# 5.0, since we're going to drop the net45 binary from FSharp.Core and move to be .NET Standard 2.0-only.

Well it could already be done now if we ifdef it, right?

Phillip Carter notifications@github.com schrieb am Sa., 21. Sep. 2019,
18:25:

This could be done for F# 5.0, since we're going to drop the net45 binary
from FSharp.Core and move to be .NET Standard 2.0-only.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/fsharp/issues/7595?email_source=notifications&email_token=AAAOANETUUHXG7C5UUOHLCTQKZDHBA5CNFSM4IYGDVQKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7IU6ZI#issuecomment-533811045,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAOANEQZ3WLXOHQULTQ23DQKZDHBANCNFSM4IYGDVQA
.

Yes, though I'd prefer to avoid any and all ifdefs given how many of those horrible things we have

They are not horrible. Just little creepy. It's all fine if we don't feed
them after midnight.

Phillip Carter notifications@github.com schrieb am Sa., 21. Sep. 2019,
18:32:

Yes, though I'd prefer to avoid any and all ifdefs given how many of those
horrible things we have

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/dotnet/fsharp/issues/7595?email_source=notifications&email_token=AAAOANFIGGKA5PJPN3MHLY3QKZEDPA5CNFSM4IYGDVQKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7IVDEQ#issuecomment-533811602,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAOANHJYUNVWZYJBDRHJWTQKZEDPANCNFSM4IYGDVQA
.

@forki bump

@chinwobble I think you can try to send a pull request.

maybe @dsyme can point you to the correct location in the source

maybe @dsyme can point you to the correct location in the source

I guess in IlxGen.fs GenNewArray and/or GenNewArraySimple (the latter being used for small arrays)

Was this page helpful?
0 / 5 - 0 ratings