Skip to content

TryAddWithoutValidation for multiple values could be more efficient #64049

Description

@MihaZupan

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

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jan 20, 2022
  2. added this to the Future milestone on Jan 27, 2022
  3. added
    enhancementProduct code improvement that does NOT require public API changes/additions
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Jan 27, 2022
  4. karelz commented on Jan 27, 2022

    @karelz
    Member

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

  5. deeprobin commented on Feb 19, 2022

    @deeprobin
    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?

  6. MihaZupan commented on Feb 19, 2022

    @MihaZupan
    MemberAuthor

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

  7. added
    help wanted[up-for-grabs] Good issue for external contributors
    on Jan 2, 2023
  8. added
    in-prThere is an active PR which will close this issue when it is merged
    on May 29, 2024
  9. modified the milestones: Future, 9.0.0 on Jun 24, 2024
  10. locked and limited conversation to collaborators on Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Net.HttpenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is mergedtenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions