Skip to content

Commit 92e3d82

Browse files
authored
Changing TimeStamp to a value type in order to reduce unnecessary heap allocations (#225)
* Change TimeStamp to a readonly struct record in order to avoid unnecessary allocations. * Change TimeStamp to a readonly struct record in order to avoid unnecessary allocations.
1 parent f0faa18 commit 92e3d82

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

src/NRedisStack/TimeSeries/DataTypes/TimeStamp.cs

+3-11
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
/// A class represents timestamp.
55
/// Value can be either primitive long, DateTime or one of the strings "-", "+", "*".
66
/// </summary>
7-
public class TimeStamp
7+
public readonly record struct TimeStamp
88
{
99
private static readonly string[] constants = { "-", "+", "*" };
1010

1111
/// <summary>
1212
/// TimeStamp value.
1313
/// </summary>
14-
public object Value { get; private set; }
14+
public object Value { get; }
1515

1616
/// <summary>
1717
/// Build a TimeStamp from primitive long.
@@ -34,7 +34,7 @@ public TimeStamp(string timestamp)
3434
{
3535
if (Array.IndexOf(constants, timestamp) == -1)
3636
{
37-
throw new NotSupportedException(string.Format("The string {0} cannot be used", timestamp));
37+
throw new NotSupportedException($"The string {timestamp} cannot be used");
3838
}
3939
Value = timestamp;
4040
}
@@ -78,14 +78,6 @@ public static implicit operator long(TimeStamp ts) =>
7878
/// <param name="timeStamp">TimeStamp</param>
7979
public static implicit operator DateTime(TimeStamp timeStamp) => DateTimeOffset.FromUnixTimeMilliseconds(timeStamp).DateTime;
8080

81-
/// <summary>
82-
/// Equality of TimeSeriesTuple objects
83-
/// </summary>
84-
/// <param name="obj">Object to compare</param>
85-
/// <returns>If two TimeStamp objects are equal</returns>
86-
public override bool Equals(object? obj) =>
87-
obj is TimeStamp stamp && EqualityComparer<object>.Default.Equals(Value, stamp.Value);
88-
8981
/// <summary>
9082
/// TimeStamp object hash code.
9183
/// </summary>

src/NRedisStack/TimeSeries/TimeSeriesAux.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public static void AddOnDuplicate(this IList<object> args, TsDuplicatePolicy? po
7979
}
8080
}
8181

82-
public static void AddAlign(this IList<object> args, TimeStamp? align)
82+
public static void AddAlign(this IList<object> args, TimeStamp? alignMaybe)
8383
{
84-
if (align != null)
84+
if (alignMaybe is {} align)
8585
{
8686
args.Add(TimeSeriesArgs.ALIGN);
8787
args.Add(align.Value);
@@ -200,11 +200,8 @@ public static void AddGroupby(this IList<object> args, (string groupby, TsReduce
200200

201201
public static void AddTimeStamp(this IList<object> args, TimeStamp timeStamp)
202202
{
203-
if (timeStamp != null)
204-
{
205-
args.Add(TimeSeriesArgs.TIMESTAMP);
206-
args.Add(timeStamp.Value);
207-
}
203+
args.Add(TimeSeriesArgs.TIMESTAMP);
204+
args.Add(timeStamp.Value);
208205
}
209206

210207
public static void AddRule(this IList<object> args, TimeSeriesRule rule)
@@ -250,11 +247,11 @@ public static List<object> BuildTsAddArgs(string key, TimeStamp timestamp, doubl
250247
return args;
251248
}
252249

253-
public static List<object> BuildTsIncrDecrByArgs(string key, double value, TimeStamp? timestamp, long? retentionTime,
250+
public static List<object> BuildTsIncrDecrByArgs(string key, double value, TimeStamp? timestampMaybe, long? retentionTime,
254251
IReadOnlyCollection<TimeSeriesLabel>? labels, bool? uncompressed, long? chunkSizeBytes)
255252
{
256253
var args = new List<object> { key, value };
257-
if (timestamp != null) args.AddTimeStamp(timestamp);
254+
if (timestampMaybe is {} timestamp) args.AddTimeStamp(timestamp);
258255
args.AddRetentionTime(retentionTime);
259256
args.AddChunkSize(chunkSizeBytes);
260257
if (labels != null) args.AddLabels(labels);

0 commit comments

Comments
 (0)