Skip to content

Short circuit in ToUrl to remove allocations when possible. #5002

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

Merged
merged 1 commit into from
Sep 7, 2020
Merged

Short circuit in ToUrl to remove allocations when possible. #5002

merged 1 commit into from
Sep 7, 2020

Conversation

stevejgordon
Copy link
Contributor

In the process of going through the code, I spotted a fairly trivial change which can reduce allocations during URL generation for endpoints where no route values need to be applied.

UrlLookup.ToUrl can short-circuit in such cases to avoid duplicating the string and allocating a StringBuilder. For requests where no index is supplied, the allocations can be entirely removed and an 87% reduction in execution time can be achieved.

The examples below test with the cluster health endpoints.

Before:

|      Method |     Mean |    Error |   StdDev |  Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------ |---------:|---------:|---------:|-------:|------:|------:|----------:|
|      Health | 41.60 ns | 0.637 ns | 0.596 ns | 0.0381 |     - |     - |     160 B |
| HealthIndex | 85.60 ns | 0.851 ns | 0.796 ns | 0.0457 |     - |     - |     192 B |

After:

|      Method |      Mean |     Error |    StdDev |  Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------ |----------:|----------:|----------:|-------:|------:|------:|----------:|
|      Health |  5.352 ns | 0.0611 ns | 0.0510 ns |      - |     - |     - |         - |
| HealthIndex | 84.470 ns | 0.5005 ns | 0.4437 ns | 0.0457 |     - |     - |     192 B |

@elasticmachine
Copy link
Collaborator

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?

Copy link
Contributor

@russcam russcam left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @stevejgordon 👍

@russcam russcam merged commit 39f6383 into elastic:master Sep 7, 2020
russcam pushed a commit that referenced this pull request Sep 7, 2020
russcam pushed a commit that referenced this pull request Sep 7, 2020
@stevejgordon stevejgordon deleted the UrlLookupPerf branch September 7, 2020 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants