-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Implement new SmallCapacityDictionary for dictionaries with a small amount of values. #31360
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
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
c87b685
Trying routevaluedict
jkotalik 485474b
New dictionary
jkotalik 51dccd2
Minor cleanup
jkotalik 5e0cdc9
Comparers
jkotalik 65d1055
Some benchmarks
jkotalik 183287f
Fixing for large capacity
jkotalik 8458629
Tests
jkotalik a4b53f5
Feedback
jkotalik 58558d7
nit
jkotalik c82a205
nit
jkotalik 2733b31
More build issues
jkotalik b7d7944
No struct for testing
jkotalik b29bbde
ordre
jkotalik e7e72e7
Fixing two tests
jkotalik 9690007
Fix check
jkotalik 6eb4375
Feedback
jkotalik 6cfeb8c
fixing test
jkotalik 11f8c43
remove comment
jkotalik 3da0a96
Feedback
jkotalik 1c7c005
Perf tests and new limit
jkotalik a139d9a
Update AdaptiveCapacityDictionaryTests.cs
jkotalik 4a95eea
Update AdaptiveCapacityDictionary.cs
jkotalik 069c519
Feedback
jkotalik 5342d4d
Add comment
jkotalik 7348343
Removing adaptive restriction
jkotalik 4456cc0
wrong check for capacity
jkotalik 58b5f09
Remove cast and add constructor
jkotalik 8768f0e
Nit
jkotalik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
331 changes: 331 additions & 0 deletions
331
src/Http/Http/perf/Microbenchmarks/AdaptiveCapacityDictionaryBenchmark.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,331 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.AspNetCore.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Http | ||
{ | ||
public class AdaptiveCapacityDictionaryBenchmark | ||
{ | ||
private AdaptiveCapacityDictionary<string, string> _smallCapDict; | ||
private AdaptiveCapacityDictionary<string, string> _smallCapDictTen; | ||
private AdaptiveCapacityDictionary<string, string> _filledSmallDictionary; | ||
private Dictionary<string, string> _dict; | ||
private Dictionary<string, string> _dictTen; | ||
private Dictionary<string, string> _filledDictTen; | ||
private KeyValuePair<string, string> _oneValue; | ||
private List<KeyValuePair<string, string>> _tenValues; | ||
|
||
[IterationSetup] | ||
public void Setup() | ||
{ | ||
_oneValue = new KeyValuePair<string, string>("a", "b"); | ||
|
||
_tenValues = new List<KeyValuePair<string, string>>() | ||
{ | ||
new KeyValuePair<string, string>("a", "b"), | ||
new KeyValuePair<string, string>("c", "d"), | ||
new KeyValuePair<string, string>("e", "f"), | ||
new KeyValuePair<string, string>("g", "h"), | ||
new KeyValuePair<string, string>("i", "j"), | ||
new KeyValuePair<string, string>("k", "l"), | ||
new KeyValuePair<string, string>("m", "n"), | ||
new KeyValuePair<string, string>("o", "p"), | ||
new KeyValuePair<string, string>("q", "r"), | ||
new KeyValuePair<string, string>("s", "t"), | ||
}; | ||
|
||
_smallCapDict = new AdaptiveCapacityDictionary<string, string>(capacity: 1, StringComparer.OrdinalIgnoreCase); | ||
_smallCapDictTen = new AdaptiveCapacityDictionary<string, string>(capacity: 10, StringComparer.OrdinalIgnoreCase); | ||
_filledSmallDictionary = new AdaptiveCapacityDictionary<string, string>(_tenValues, capacity: 10, StringComparer.OrdinalIgnoreCase); | ||
|
||
_dict = new Dictionary<string, string>(1, StringComparer.OrdinalIgnoreCase); | ||
_dictTen = new Dictionary<string, string>(10, StringComparer.OrdinalIgnoreCase); | ||
_filledDictTen = new Dictionary<string, string>(10, StringComparer.OrdinalIgnoreCase); | ||
|
||
foreach (var a in _tenValues) | ||
{ | ||
_filledDictTen[a.Key] = a.Value; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void OneValue_SmallDict() | ||
{ | ||
_smallCapDict[_oneValue.Key] = _oneValue.Value; | ||
_ = _smallCapDict[_oneValue.Key]; | ||
} | ||
|
||
[Benchmark] | ||
public void OneValue_Dict() | ||
{ | ||
_dict[_oneValue.Key] = _oneValue.Value; | ||
_ = _dict[_oneValue.Key]; | ||
} | ||
|
||
[Benchmark] | ||
public void OneValue_SmallDict_Set() | ||
{ | ||
_smallCapDict[_oneValue.Key] = _oneValue.Value; | ||
} | ||
|
||
[Benchmark] | ||
public void OneValue_Dict_Set() | ||
{ | ||
_dict[_oneValue.Key] = _oneValue.Value; | ||
} | ||
|
||
|
||
[Benchmark] | ||
public void OneValue_SmallDict_Get() | ||
{ | ||
_smallCapDict.TryGetValue("test", out var val); | ||
} | ||
|
||
[Benchmark] | ||
public void OneValue_Dict_Get() | ||
{ | ||
_dict.TryGetValue("test", out var val); | ||
} | ||
|
||
[Benchmark] | ||
public void FourValues_SmallDict() | ||
{ | ||
for (var i = 0; i < 4; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_smallCapDictTen[val.Key] = val.Value; | ||
_ = _smallCapDictTen[val.Key]; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void FiveValues_SmallDict() | ||
{ | ||
for (var i = 0; i < 5; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_smallCapDictTen[val.Key] = val.Value; | ||
_ = _smallCapDictTen[val.Key]; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void SixValues_SmallDict() | ||
{ | ||
for (var i = 0; i < 6; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_smallCapDictTen[val.Key] = val.Value; | ||
_ = _smallCapDictTen[val.Key]; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void SevenValues_SmallDict() | ||
{ | ||
for (var i = 0; i < 7; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_smallCapDictTen[val.Key] = val.Value; | ||
_ = _smallCapDictTen[val.Key]; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void EightValues_SmallDict() | ||
{ | ||
for (var i = 0; i < 8; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_smallCapDictTen[val.Key] = val.Value; | ||
_ = _smallCapDictTen[val.Key]; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void NineValues_SmallDict() | ||
{ | ||
for (var i = 0; i < 9; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_smallCapDictTen[val.Key] = val.Value; | ||
_ = _smallCapDictTen[val.Key]; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void TenValues_SmallDict() | ||
{ | ||
for (var i = 0; i < 10; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_smallCapDictTen[val.Key] = val.Value; | ||
_ = _smallCapDictTen[val.Key]; | ||
} | ||
} | ||
|
||
|
||
[Benchmark] | ||
public void FourValues_Dict() | ||
{ | ||
for (var i = 0; i < 4; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_dictTen[val.Key] = val.Value; | ||
_ = _dictTen[val.Key]; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void FiveValues_Dict() | ||
{ | ||
for (var i = 0; i < 5; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_dictTen[val.Key] = val.Value; | ||
_ = _dictTen[val.Key]; | ||
} | ||
} | ||
[Benchmark] | ||
public void SixValues_Dict() | ||
{ | ||
for (var i = 0; i < 6; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_dictTen[val.Key] = val.Value; | ||
_ = _dictTen[val.Key]; | ||
} | ||
} | ||
[Benchmark] | ||
public void SevenValues_Dict() | ||
{ | ||
for (var i = 0; i < 7; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_dictTen[val.Key] = val.Value; | ||
_ = _dictTen[val.Key]; | ||
} | ||
} | ||
[Benchmark] | ||
public void EightValues_Dict() | ||
{ | ||
for (var i = 0; i < 8; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_dictTen[val.Key] = val.Value; | ||
_ = _dictTen[val.Key]; | ||
} | ||
} | ||
[Benchmark] | ||
public void NineValues_Dict() | ||
{ | ||
for (var i = 0; i < 9; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_dictTen[val.Key] = val.Value; | ||
_ = _dictTen[val.Key]; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void TenValues_Dict() | ||
{ | ||
for (var i = 0; i < 10; i++) | ||
{ | ||
var val = _tenValues[i]; | ||
_dictTen[val.Key] = val.Value; | ||
_ = _dictTen[val.Key]; | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void FourValues_SmallDictGet() | ||
{ | ||
_ = _filledSmallDictionary["g"]; | ||
} | ||
|
||
[Benchmark] | ||
public void FiveValues_SmallDictGet() | ||
{ | ||
_ = _filledSmallDictionary["i"]; | ||
} | ||
|
||
[Benchmark] | ||
public void SixValues_SmallDictGetGet() | ||
{ | ||
_ = _filledSmallDictionary["k"]; | ||
|
||
} | ||
|
||
[Benchmark] | ||
public void SevenValues_SmallDictGetGet() | ||
{ | ||
_ = _filledSmallDictionary["m"]; | ||
} | ||
|
||
[Benchmark] | ||
public void EightValues_SmallDictGet() | ||
{ | ||
_ = _filledSmallDictionary["o"]; | ||
} | ||
|
||
[Benchmark] | ||
public void NineValues_SmallDictGet() | ||
{ | ||
_ = _filledSmallDictionary["q"]; | ||
} | ||
|
||
[Benchmark] | ||
public void TenValues_SmallDictGet() | ||
{ | ||
_ = _filledSmallDictionary["s"]; | ||
} | ||
|
||
[Benchmark] | ||
public void TenValues_DictGet() | ||
{ | ||
_ = _filledDictTen["s"]; | ||
} | ||
|
||
[Benchmark] | ||
public void SmallDict() | ||
{ | ||
_ = new AdaptiveCapacityDictionary<string, string>(capacity: 1); | ||
} | ||
|
||
[Benchmark] | ||
public void Dict() | ||
{ | ||
_ = new Dictionary<string, string>(capacity: 1); | ||
} | ||
|
||
|
||
[Benchmark] | ||
public void SmallDictFour() | ||
{ | ||
_ = new AdaptiveCapacityDictionary<string, string>(capacity: 4); | ||
} | ||
|
||
[Benchmark] | ||
public void DictFour() | ||
{ | ||
_ = new Dictionary<string, string>(capacity: 4); | ||
} | ||
|
||
[Benchmark] | ||
public void SmallDictTen() | ||
{ | ||
_ = new AdaptiveCapacityDictionary<string, string>(capacity: 10); | ||
} | ||
|
||
[Benchmark] | ||
public void DictTen() | ||
{ | ||
_ = new Dictionary<string, string>(capacity: 10); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.