Improve the performance of ImmutableList.Contains by shortspider · Pull Request #40540 · dotnet/corefx (original) (raw)

@AArnott @stephentoub
So I redid the enumerator to use the index and I don't see a real improvement. Here is without my changes:

Type Method Size Mean Error StdDev
IterateForEach{Int32} ImmutableList 512 12.678 us 0.3188 us 0.3544 us
IterateForEach{String} ImmutableList 512 35.445 us 1.4779 us 1.5177 us

And then with the indexer implementation:

Type Method Size Mean Error StdDev
IterateForEach{Int32} ImmutableList 512 17.388 us 0.0719 us 0.0561 us
IterateForEach{String} ImmutableList 512 29.857 us 3.7395 us 4.3064 us

Switching Contains back to use IndexOf (and the Enumerator directly) here are the results (without my changes then with indexer enumerator):

Type Method Size Mean Error StdDev
ContainsFalse{Int32} ImmutableList 512 8,273.21 us 743.876 us 856.648 us
ContainsFalse{String} ImmutableList 512 23,145.14 us 3,396.777 us 3,775.509 us
ContainsTrue{Int32} ImmutableList 512 3,997.04 us 327.578 us 377.239 us
ContainsTrue{String} ImmutableList 512 10,036.07 us 29.512 us 24.644 us
Type Method Size Mean Error StdDev
ContainsFalse{Int32} ImmutableList 512 10,064.98 us 62.073 us 55.026 us
ContainsFalse{String} ImmutableList 512 13,986.43 us 38.286 us 35.813 us
ContainsTrue{Int32} ImmutableList 512 4,665.12 us 421.096 us 468.047 us
ContainsTrue{String} ImmutableList 512 7,144.50 us 8.922 us 7.909 us

I don't honestly know what to make of this data, with a pure indexer implementation the enumerator for ints seems to take a performance hit while for strings it performs better.

None of this matters for the Contains case but it's interesting.