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

TryAddWithoutValidation for multiple values could be more efficient #64049

Closed
MihaZupan opened this issue Jan 20, 2022 · 4 comments · Fixed by #102845
Closed

TryAddWithoutValidation for multiple values could be more efficient #64049

MihaZupan opened this issue Jan 20, 2022 · 4 comments · Fixed by #102845
Labels
area-System.Net.Http enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors in-pr There is an active PR which will close this issue when it is merged tenet-performance Performance related issue
Milestone

Comments

@MihaZupan
Copy link
Member

The method takes an IEnumerable<string>. Things we could improve:

  • Special-case IList<string> or some other type to avoid boxing the enumerator
  • Avoid double entry lookups in case of more than one value
  • Avoid unnecessary List resizes if we already know the final size

For example

TryAddWithoutValidation("foo", "1");
TryAddWithoutValidation("foo", new[] { "2", "3" });

Will currently allocate the enumerator, a List<string> of size 2, and a List<string> of size 4.

We can reduce that down to allocating a single List<string> of size 3.

An example of how this can be achieved:
https://gist.github.com/MihaZupan/800df7a324a66a8e814c3ec91f3d39b0

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jan 20, 2022
@ghost
Copy link

ghost commented Jan 20, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

The method takes an IEnumerable<string>. Things we could improve:

  • Special-case IList<string> or some other type to avoid boxing the enumerator
  • Avoid double entry lookups in case of more than one value
  • Avoid unnecessary List resizes if we already know the final size

For example

TryAddWithoutValidation("foo", "1");
TryAddWithoutValidation("foo", new[] { "2", "3" });

Will currently allocate the enumerator, a List<string> of size 2, and a List<string> of size 4.

We can reduce that down to allocating a single List<string> of size 3.

An example of how this can be achieved:
https://gist.github.com/MihaZupan/800df7a324a66a8e814c3ec91f3d39b0

Author: MihaZupan
Assignees: -
Labels:

area-System.Net.Http, tenet-performance

Milestone: -

@karelz karelz added this to the Future milestone Jan 27, 2022
@karelz karelz added enhancement Product code improvement that does NOT require public API changes/additions and removed untriaged New issue has not been triaged by the area owner labels Jan 27, 2022
@karelz
Copy link
Member

karelz commented Jan 27, 2022

Triage: Worth investment, but the method is not commonly used, so not priority.

@deeprobin
Copy link
Contributor

I am confident that this will grant a performance advantage. As @karelz mentioned, this does not seem to be a method on the hot path.

@MihaZupan
Do you feel like submitting a pull request and also creating a benchmark in dotnet/performance?

@MihaZupan
Copy link
Member Author

It's not a priority for us for now, but I'll happily review a PR if you are interested :)

@MihaZupan MihaZupan added the help wanted [up-for-grabs] Good issue for external contributors label Jan 2, 2023
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label May 29, 2024
@karelz karelz modified the milestones: Future, 9.0.0 Jun 24, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Http enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors in-pr There is an active PR which will close this issue when it is merged tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants