From 16a61245db6e4baa9f93eb3321606a3f37a78b39 Mon Sep 17 00:00:00 2001 From: Eduardo Semprebon Date: Sun, 28 Mar 2021 19:49:13 +0200 Subject: [PATCH] Reduce allocation on TimerMessage by reusing Message.CreatedTimestamp --- src/StackExchange.Redis/Message.cs | 12 ++++++------ src/StackExchange.Redis/Profiling/ProfiledCommand.cs | 4 ++-- src/StackExchange.Redis/ResultProcessor.cs | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/StackExchange.Redis/Message.cs b/src/StackExchange.Redis/Message.cs index 1550c8f38..9c84a0199 100644 --- a/src/StackExchange.Redis/Message.cs +++ b/src/StackExchange.Redis/Message.cs @@ -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) { @@ -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; } @@ -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; diff --git a/src/StackExchange.Redis/Profiling/ProfiledCommand.cs b/src/StackExchange.Redis/Profiling/ProfiledCommand.cs index 57ed9b6e9..7d6a8fcfe 100644 --- a/src/StackExchange.Redis/Profiling/ProfiledCommand.cs +++ b/src/StackExchange.Redis/Profiling/ProfiledCommand.cs @@ -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); diff --git a/src/StackExchange.Redis/ResultProcessor.cs b/src/StackExchange.Redis/ResultProcessor.cs index 4050ea6a1..0594f1b8b 100644 --- a/src/StackExchange.Redis/ResultProcessor.cs +++ b/src/StackExchange.Redis/ResultProcessor.cs @@ -329,6 +329,8 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes public sealed class TimingProcessor : ResultProcessor { + 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); @@ -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 { @@ -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; }