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

A few perf tweaks #940

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stephentoub
Copy link

@stephentoub stephentoub commented Nov 11, 2024

Happened to notice a few things as I was looking at a trace of an app using this library...

  • SimplePluralizer was using ToCharArray and then Any in both ToPluralInternal and ToSingularInternal. The ToCharArray is an expensive nop and can simply be removed. On .NET 8, Ascii.IsValid can be used instead of the Any call without loss of readability and is much faster.
  • ODataNameMatchResolver is using Split().Last. It can just use LastIndexOf and Substring to avoid the string[] and strings for any other segments other than the last.
  • HomogenizeEx can use a compiled / source generated regex.

Before

Method Mean Allocated
Pluralize 268.6 ns 80 B
Resolve 213.1 ns 320 B

After

Method Mean Allocated
Pluralize 236.4 ns 40 B
Resolve 124.1 ns 256 B
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Simple.OData.Client;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
public class Tests
{
    [Benchmark]
    public string Pluralize() => Pluralizers.Simple.Pluralize("runner");

    [Benchmark]
    public bool Resolve() => ODataNameMatchResolver.NotStrict.IsMatch("actualName", "requestedName");
}

- SimplePluralizer was using ToCharArray and then Any in both ToPluralInternal and ToSingularInternal. The ToCharArray is an expensive nop and can simply be removed. On .NET 8, Ascii.IsValid can be used instead of the Any call without loss of readability and is much faster.
- ODataNameMatchResolver is using Split().Last. It can just use LastIndexOf and Substring to avoid the string[] and strings for any other segments other than the last.
- HomogenizeEx can use a compiled / source generated regex.
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.

1 participant