Skip to content

Changing TimeStamp to a value type in order to reduce unnecessary heap allocations #225

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

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/NRedisStack/TimeSeries/DataTypes/TimeStamp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
/// A class represents timestamp.
/// Value can be either primitive long, DateTime or one of the strings "-", "+", "*".
/// </summary>
public class TimeStamp
public readonly record struct TimeStamp
{
private static readonly string[] constants = { "-", "+", "*" };

/// <summary>
/// TimeStamp value.
/// </summary>
public object Value { get; private set; }
public object Value { get; }

/// <summary>
/// Build a TimeStamp from primitive long.
Expand All @@ -34,7 +34,7 @@ public TimeStamp(string timestamp)
{
if (Array.IndexOf(constants, timestamp) == -1)
{
throw new NotSupportedException(string.Format("The string {0} cannot be used", timestamp));
throw new NotSupportedException($"The string {timestamp} cannot be used");
}
Value = timestamp;
}
Expand Down Expand Up @@ -78,14 +78,6 @@ public static implicit operator long(TimeStamp ts) =>
/// <param name="timeStamp">TimeStamp</param>
public static implicit operator DateTime(TimeStamp timeStamp) => DateTimeOffset.FromUnixTimeMilliseconds(timeStamp).DateTime;

/// <summary>
/// Equality of TimeSeriesTuple objects
/// </summary>
/// <param name="obj">Object to compare</param>
/// <returns>If two TimeStamp objects are equal</returns>
public override bool Equals(object? obj) =>
obj is TimeStamp stamp && EqualityComparer<object>.Default.Equals(Value, stamp.Value);

/// <summary>
/// TimeStamp object hash code.
/// </summary>
Expand Down
15 changes: 6 additions & 9 deletions src/NRedisStack/TimeSeries/TimeSeriesAux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public static void AddOnDuplicate(this IList<object> args, TsDuplicatePolicy? po
}
}

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

public static void AddTimeStamp(this IList<object> args, TimeStamp timeStamp)
{
if (timeStamp != null)
{
args.Add(TimeSeriesArgs.TIMESTAMP);
args.Add(timeStamp.Value);
}
args.Add(TimeSeriesArgs.TIMESTAMP);
args.Add(timeStamp.Value);
}

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

public static List<object> BuildTsIncrDecrByArgs(string key, double value, TimeStamp? timestamp, long? retentionTime,
public static List<object> BuildTsIncrDecrByArgs(string key, double value, TimeStamp? timestampMaybe, long? retentionTime,
IReadOnlyCollection<TimeSeriesLabel>? labels, bool? uncompressed, long? chunkSizeBytes)
{
var args = new List<object> { key, value };
if (timestamp != null) args.AddTimeStamp(timestamp);
if (timestampMaybe is {} timestamp) args.AddTimeStamp(timestamp);
args.AddRetentionTime(retentionTime);
args.AddChunkSize(chunkSizeBytes);
if (labels != null) args.AddLabels(labels);
Expand Down