Skip to content

Commit

Permalink
Reduce allocation on TimerMessage by reusing Message.CreatedTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardobr committed Mar 28, 2021
1 parent 8bfa19a commit 16a6124
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/StackExchange.Redis/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ internal void SetBacklogState(int position, PhysicalConnection physical)

// All for profiling purposes
private ProfiledCommand performance;
internal DateTime createdDateTime;
internal long createdTimestamp;
internal DateTime CreatedDateTime;
internal long CreatedTimestamp;

protected Message(int db, CommandFlags flags, RedisCommand command)
{
Expand Down Expand Up @@ -128,8 +128,8 @@ protected Message(int db, CommandFlags flags, RedisCommand command)
Flags = flags & UserSelectableFlags;
if (masterOnly) SetMasterOnly();

createdDateTime = DateTime.UtcNow;
createdTimestamp = System.Diagnostics.Stopwatch.GetTimestamp();
CreatedDateTime = DateTime.UtcNow;
CreatedTimestamp = System.Diagnostics.Stopwatch.GetTimestamp();
Status = CommandStatus.WaitingToBeSent;
}

Expand Down Expand Up @@ -165,8 +165,8 @@ internal void PrepareToResend(ServerEndPoint resendTo, bool isMoved)
oldPerformance.SetCompleted();
performance = null;

createdDateTime = DateTime.UtcNow;
createdTimestamp = System.Diagnostics.Stopwatch.GetTimestamp();
CreatedDateTime = DateTime.UtcNow;
CreatedTimestamp = System.Diagnostics.Stopwatch.GetTimestamp();
performance = ProfiledCommand.NewAttachedToSameContext(oldPerformance, resendTo, isMoved);
performance.SetMessage(this);
Status = CommandStatus.WaitingToBeSent;
Expand Down
4 changes: 2 additions & 2 deletions src/StackExchange.Redis/Profiling/ProfiledCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public void SetMessage(Message msg)
if (Message != null) throw new InvalidOperationException($"{nameof(SetMessage)} called more than once");

Message = msg;
MessageCreatedDateTime = msg.createdDateTime;
MessageCreatedTimeStamp = msg.createdTimestamp;
MessageCreatedDateTime = msg.CreatedDateTime;
MessageCreatedTimeStamp = msg.CreatedTimestamp;
}

public void SetEnqueued() => SetTimestamp(ref EnqueuedTimeStamp);
Expand Down
10 changes: 5 additions & 5 deletions src/StackExchange.Redis/ResultProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes

public sealed class TimingProcessor : ResultProcessor<TimeSpan>
{
private static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;

public static TimerMessage CreateMessage(int db, CommandFlags flags, RedisCommand command, RedisValue value = default(RedisValue))
{
return new TimerMessage(db, flags, command, value);
Expand All @@ -346,9 +348,9 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
TimeSpan duration;
if (message is TimerMessage timingMessage)
{
var watch = timingMessage.Watch;
watch.Stop();
duration = watch.Elapsed;
var timestampDelta = Stopwatch.GetTimestamp() - timingMessage.CreatedTimestamp;
var ticks = (long)(TimestampToTicks * timestampDelta);
duration = new TimeSpan(ticks);
}
else
{
Expand All @@ -361,12 +363,10 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes

internal sealed class TimerMessage : Message
{
public readonly Stopwatch Watch;
private readonly RedisValue value;
public TimerMessage(int db, CommandFlags flags, RedisCommand command, RedisValue value)
: base(db, flags, command)
{
Watch = Stopwatch.StartNew();
this.value = value;
}

Expand Down

0 comments on commit 16a6124

Please sign in to comment.