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

Optimize MessageCracker.IsHandlerMethod for memory #914

Merged

Conversation

vasily-balansea
Copy link
Contributor

@vasily-balansea vasily-balansea commented Dec 25, 2024

MessageCracker.IsHandlerMethod is calling MethodInfo.GetParameters() method three times leading to minor memory waste. This is because GetParameters() is allocating a new array when method has parameters. If we cache parameters array then we save a bit of memory and make method a bit faster. IL code is also a bit smaller: 119 bytes vs 127.

Unit tests are passing.

Benchmark results:

| Method | Runtime            | Mean     | Error   | StdDev  | Allocated |
|------- |------------------- |---------:|--------:|--------:|----------:|
| Old    | .NET 8.0           | 194.1 ns | 1.63 ns | 1.44 ns |     480 B |
| New    | .NET 8.0           | 124.3 ns | 0.58 ns | 0.54 ns |     240 B |

| Old    | .NET Framework 4.8 | 518.1 ns | 0.91 ns | 0.76 ns |     481 B |
| New    | .NET Framework 4.8 | 350.0 ns | 1.26 ns | 1.12 ns |     241 B |

Benchmark code:

[SimpleJob(RuntimeMoniker.Net48)]
[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
[HideColumns("Job", "Gen0")]
public class IsHandlerMethodBenchmark
{
    // UnitTests.MessageCrackerTests.TheseMightBeHandlerMethods
    MethodInfo[] methods = typeof(TheseMightBeHandlerMethods).GetMethods();

    [Benchmark]
    public int Old()
    {
        int count = 0;

        foreach (MethodInfo m in methods)
        {
            if (IsHandlerMethodOld(m))
            {
                count++;
            }
        }

        return count;
    }

    [Benchmark]
    public int New()
    {
        int count = 0;

        foreach (MethodInfo m in methods)
        {
            if (IsHandlerMethodNew(m))
            {
                count++;
            }
        }

        return count;
    }

    private bool IsHandlerMethodOld(MethodInfo m)
    {
        return m.IsPublic
               && m.Name.Equals("OnMessage")
               && m.GetParameters().Length == 2
               && m.GetParameters()[0].ParameterType.IsSubclassOf(typeof(Message))
               && typeof(SessionID).IsAssignableFrom(m.GetParameters()[1].ParameterType)
               && m.ReturnType == typeof(void);
    }

    private bool IsHandlerMethodNew(MethodInfo m)
    {
        ParameterInfo[] parameters;
        return m.IsPublic
                && m.ReturnType == typeof(void)
                && m.Name.Equals("OnMessage")
                && (parameters = m.GetParameters()).Length == 2
                && parameters[0].ParameterType.IsSubclassOf(typeof(Message))
                && typeof(SessionID).IsAssignableFrom(parameters[1].ParameterType);
    }
}

This is obviously a micro optimization since gains (memory and time) are small and unlikely to have any visible effect on application performance. Still I hope you would accept PR.

@CLAassistant
Copy link

CLAassistant commented Dec 25, 2024

CLA assistant check
All committers have signed the CLA.

@gbirchmeier gbirchmeier force-pushed the improve-IsHandlerMethod branch from d86f50b to 120db20 Compare December 30, 2024 22:39
Copy link
Member

@gbirchmeier gbirchmeier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's small, but I like it! You also made it 3 times faster. 😆

FYI, I force-pushed a release-notes update into your branch. (I don't know why I did that instead of a separate commit. I shouldn't do that to other peoples' branches.)

Thanks!

@gbirchmeier gbirchmeier merged commit 1c61bf5 into connamara:master Dec 30, 2024
1 check passed
gbirchmeier added a commit that referenced this pull request Dec 30, 2024
@vasily-balansea vasily-balansea deleted the improve-IsHandlerMethod branch January 5, 2025 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants