Skip to content

Commit

Permalink
Drop allocations when using ResilienceProperties (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Jun 12, 2023
1 parent 59fd86b commit 452613b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/Polly.Core.Tests/ResiliencePropertiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ public void Clear_Ok()
props.Should().HaveCount(0);
}

[Fact]
public void Replace_Ok()
[InlineData(true)]
[InlineData(false)]
[Theory]
public void Replace_Ok(bool isRawDictionary)
{
var key1 = new ResiliencePropertyKey<string>("A");
var key2 = new ResiliencePropertyKey<string>("B");
Expand All @@ -79,6 +81,11 @@ public void Replace_Ok()
props.Set(key1, "A");

var otherProps = new ResilienceProperties();
if (!isRawDictionary)
{
otherProps.Options = new ResilienceProperties();
}

otherProps.Set(key2, "B");

props.Replace(otherProps);
Expand Down
15 changes: 13 additions & 2 deletions src/Polly.Core/ResilienceProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,20 @@ internal void Replace(ResilienceProperties other)
{
Clear();

foreach (var pair in other.Options)
// try to avoid enumerator allocation
if (other.Options is Dictionary<string, object?> otherOptions)
{
Options[pair.Key] = pair.Value;
foreach (var pair in otherOptions)
{
Options[pair.Key] = pair.Value;
}
}
else
{
foreach (var pair in other.Options)
{
Options[pair.Key] = pair.Value;
}
}
}

Expand Down

0 comments on commit 452613b

Please sign in to comment.