Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
TimothyMakkison opened this issue Nov 14, 2023 · 1 comment · Fixed by #1712
Closed

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

TimothyMakkison opened this issue Nov 14, 2023 · 1 comment · Fixed by #1712

Comments

@TimothyMakkison
Copy link
Contributor

TimothyMakkison commented Nov 14, 2023

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 );
    ...
@TimothyMakkison TimothyMakkison changed the title Generated code should use Array.Empty where possible Generated code should use Array.Empty or constant field where possible Nov 14, 2023
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant