Skip to content

TryAddWithoutValidation for multiple values could be more efficient #64049

Closed
@MihaZupan

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

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