-
Notifications
You must be signed in to change notification settings - Fork 1k
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
reduce allocations inside PhiAccrualFailureDetector #4913
Merged
Aaronontheweb
merged 1 commit into
akkadotnet:dev
from
Aaronontheweb:remote/phi-accrual-heartbeathistory-struct
Apr 7, 2021
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Akka.Actor; | ||
using Akka.Configuration; | ||
|
@@ -35,12 +36,12 @@ namespace Akka.Remote | |
/// </summary> | ||
public class PhiAccrualFailureDetector : FailureDetector | ||
{ | ||
private double _threshold; | ||
private int _maxSampleSize; | ||
private readonly double _threshold; | ||
private readonly int _maxSampleSize; | ||
private TimeSpan _minStdDeviation; | ||
private TimeSpan _acceptableHeartbeatPause; | ||
private TimeSpan _firstHeartbeatEstimate; | ||
private Clock _clock; | ||
private readonly Clock _clock; | ||
|
||
/// <summary> | ||
/// Procedural constructor for PhiAccrualDetector | ||
|
@@ -182,7 +183,7 @@ public override void HeartBeat() | |
{ | ||
var timestamp = _clock(); | ||
var oldState = state; | ||
HeartbeatHistory newHistory = null; | ||
HeartbeatHistory newHistory; | ||
|
||
if (!oldState.TimeStamp.HasValue) | ||
{ | ||
|
@@ -275,14 +276,14 @@ internal double Phi(long timeDiff, double mean, double stdDeviation) | |
return -Math.Log10(1.0d - 1.0d/(1.0d + e)); | ||
} | ||
|
||
private long MinStdDeviationMillis | ||
private double MinStdDeviationMillis | ||
{ | ||
get { return (long)_minStdDeviation.TotalMilliseconds; } | ||
get { return _minStdDeviation.TotalMilliseconds; } | ||
} | ||
|
||
private long AcceptableHeartbeatPauseMillis | ||
private double AcceptableHeartbeatPauseMillis | ||
{ | ||
get { return (long)_acceptableHeartbeatPause.TotalMilliseconds; } | ||
get { return _acceptableHeartbeatPause.TotalMilliseconds; } | ||
} | ||
|
||
private double EnsureValidStdDeviation(double stdDeviation) | ||
|
@@ -300,20 +301,20 @@ private double EnsureValidStdDeviation(double stdDeviation) | |
/// The stats (mean, variance, stdDeviation) are not defined for empty | ||
/// <see cref="HeartbeatHistory"/>, i.e. throws Exception | ||
/// </summary> | ||
internal class HeartbeatHistory | ||
internal readonly struct HeartbeatHistory | ||
{ | ||
private int _maxSampleSize; | ||
private List<long> _intervals; | ||
private long _intervalSum; | ||
private long _squaredIntervalSum; | ||
private readonly int _maxSampleSize; | ||
private readonly ImmutableList<long> _intervals; | ||
private readonly long _intervalSum; | ||
private readonly long _squaredIntervalSum; | ||
|
||
/// <summary> | ||
/// TBD | ||
/// Creates a new <see cref="HeartbeatHistory"/> instance. | ||
/// </summary> | ||
/// <param name="maxSampleSize">TBD</param> | ||
/// <param name="intervals">TBD</param> | ||
/// <param name="intervalSum">TBD</param> | ||
/// <param name="squaredIntervalSum">TBD</param> | ||
/// <param name="maxSampleSize">The maximum number of samples to retain. Older ones are dropped once intervals exceeds this value.</param> | ||
/// <param name="intervals">The range of recorded time intervals.</param> | ||
/// <param name="intervalSum">The sum of the recorded time intervals.</param> | ||
/// <param name="squaredIntervalSum">The squared sum of the intervals.</param> | ||
/// <exception cref="ArgumentOutOfRangeException"> | ||
/// This exception is thrown for the following reasons: | ||
/// <ul> | ||
|
@@ -322,7 +323,7 @@ internal class HeartbeatHistory | |
/// <li>The specified <paramref name="squaredIntervalSum"/> is less than zero.</li> | ||
/// </ul> | ||
/// </exception> | ||
public HeartbeatHistory(int maxSampleSize, List<long> intervals, long intervalSum, long squaredIntervalSum) | ||
public HeartbeatHistory(int maxSampleSize, ImmutableList<long> intervals, long intervalSum, long squaredIntervalSum) | ||
{ | ||
_maxSampleSize = maxSampleSize; | ||
_intervals = intervals; | ||
|
@@ -337,29 +338,11 @@ public HeartbeatHistory(int maxSampleSize, List<long> intervals, long intervalSu | |
throw new ArgumentOutOfRangeException(nameof(squaredIntervalSum), $"squaredIntervalSum must be >= 0, got {squaredIntervalSum}"); | ||
} | ||
|
||
/// <summary> | ||
/// TBD | ||
/// </summary> | ||
public double Mean | ||
{ | ||
get { return ((double)_intervalSum / _intervals.Count); } | ||
} | ||
public double Mean => ((double)_intervalSum / _intervals.Count); | ||
|
||
/// <summary> | ||
/// TBD | ||
/// </summary> | ||
public double Variance | ||
{ | ||
get { return ((double)_squaredIntervalSum / _intervals.Count) - (Mean * Mean); } | ||
} | ||
public double Variance => ((double)_squaredIntervalSum / _intervals.Count) - (Mean * Mean); | ||
|
||
/// <summary> | ||
/// TBD | ||
/// </summary> | ||
public double StdDeviation | ||
{ | ||
get { return Math.Sqrt(Variance); } | ||
} | ||
public double StdDeviation => Math.Sqrt(Variance); | ||
|
||
/// <summary> | ||
/// Increments the <see cref="HeartbeatHistory"/>. | ||
|
@@ -371,7 +354,7 @@ public double StdDeviation | |
{ | ||
if (history._intervals.Count < history._maxSampleSize) | ||
{ | ||
return new HeartbeatHistory(history._maxSampleSize, history._intervals.Concat(new[] { interval }).ToList(), | ||
return new HeartbeatHistory(history._maxSampleSize, history._intervals.Add(interval), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Systems.Collections.Immutable here instead of classic collections and LINQ (this is 7-8 year old code) |
||
history._intervalSum + interval, history._squaredIntervalSum + Pow2(interval)); | ||
} | ||
else | ||
|
@@ -382,7 +365,8 @@ public double StdDeviation | |
|
||
private static HeartbeatHistory DropOldest(HeartbeatHistory history) | ||
{ | ||
return new HeartbeatHistory(history._maxSampleSize, history._intervals.Skip(1).ToList(), history._intervalSum - history._intervals.First(), history._squaredIntervalSum - Pow2(history._intervals.First())); | ||
return new HeartbeatHistory(history._maxSampleSize, history._intervals.RemoveAt(0), history._intervalSum - history._intervals.First(), | ||
history._squaredIntervalSum - Pow2(history._intervals.First())); | ||
} | ||
|
||
private static long Pow2(long x) | ||
|
@@ -398,11 +382,11 @@ private static long Pow2(long x) | |
/// The stats (mean, variance, stdDeviation) are not defined for empty | ||
/// HeartbeatHistory and will throw DivideByZero exceptions | ||
/// </summary> | ||
/// <param name="maxSampleSize">TBD</param> | ||
/// <returns>TBD</returns> | ||
/// <param name="maxSampleSize">The maximum number of samples to include in this history.</param> | ||
/// <returns>A new <see cref="HeartbeatHistory"/> instance.</returns> | ||
public static HeartbeatHistory Apply(int maxSampleSize) | ||
{ | ||
return new HeartbeatHistory(maxSampleSize, new List<long>(), 0L, 0L); | ||
return new HeartbeatHistory(maxSampleSize, ImmutableList<long>.Empty, 0L, 0L); | ||
} | ||
|
||
#endregion | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove boxing back to
long
in order to prevent loss of precision.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(may not have been an issue anyway)