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 May 23, 2021
1 parent f2a7671 commit 1eeaf4c
Showing 1 changed file with 5 additions and 5 deletions.
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 1eeaf4c

Please sign in to comment.