Skip to content

Generated code should use Array.Empty or constant field where possible #1598

Closed

Description

@TimothyMakkison

Array.Empty

Each method call with no parameters allocates an empty params and type array. To prevent unneeded allocations Array.Empty should be used instead.

public async global::System.Threading.Tasks.Task<global::System.Net.Http.HttpResponseMessage> GetIndex()
{
    var ______arguments = new object[] {  }; // unneeded allocation
    var ______func = requestBuilder.BuildRestResultFuncForMethod("GetIndex", new global::System.Type[] {  } );
    ...

With Array.Empty

public async global::System.Threading.Tasks.Task<global::System.Net.Http.HttpResponseMessage> GetIndex()
{
    var ______arguments = global::System.Array.Empty<object>();
    var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndex"", global::System.Array.Empty<global::System.Type>() );

typeParameters

The parameterTypes array remains the same between calls, this could be moved out into a private static or private readonly field. The generator will have to falback to the current method if one of the parameters is a generic type scoped to the method.

Before

public async global::System.Threading.Tasks.Task<global::Refit.Tests.UserSearchResult> FindUsers(string @q)
{
    var ______arguments = new object[] { @q };
    var ______func = requestBuilder.BuildRestResultFuncForMethod(""FindUsers"", new global::System.Type[] { typeof(string) } );
    ...

After

private static global::System.Type[] typeArray1 = new global::System.Type[] { typeof(string) };

public async global::System.Threading.Tasks.Task<global::Refit.Tests.UserSearchResult> FindUsers(string @q)
{
    var ______arguments = new object[] { @q };
    var ______func = requestBuilder.BuildRestResultFuncForMethod(""FindUsers"", typeArray1 );
    ...

Generic typeParameters

private static global::System.Type[]? typeArray1 = null;

public async global::System.Threading.Tasks.Task<global::Refit.Tests.UserSearchResult> FindUsers<T>(string @q, [Body]T body)
{
    if (typeArray1 == null)
    {
        typeArray1 = new global::System.Type[] { typeof(string), typeof(T) };
    }
    var ______arguments = new object[] { @q };
    var ______func = requestBuilder.BuildRestResultFuncForMethod(""FindUsers"", typeArray1 );
    ...

Activity

  1. changed the title [-]Generated code should use `Array.Empty` where possible[/-] [+]Generated code should use `Array.Empty` or constant field where possible[/+] on Nov 14, 2023
  2. github-actions commented on Jan 31, 2024

    @github-actions

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  3. locked as resolved and limited conversation to collaborators on Jan 31, 2024
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions