Skip to content

Commit 962b7ac

Browse files
niemyjskiMpdreamz
authored andcommitted
Prevent multiple date time allocations (#2510)
1. DateTime.SpecifyKind will return a new DateTime instance. 2. Creating new DateTime instance for Epoch, 3. Adding Milliseconds creates a new DateTime Instance Now only one instance is created (apart from the one time static hit)
1 parent 3a7b7b6 commit 962b7ac

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/Nest/Aggregations/Bucket/DateHistogram/DateHistogramBucket.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ namespace Nest
88
{
99
public class DateHistogramBucket : KeyedBucket<double>
1010
{
11+
private static readonly long _epochTicks = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero).Ticks;
12+
1113
// Get a DateTime form of the returned key
12-
public DateTime Date => DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddMilliseconds(0 + this.Key), DateTimeKind.Utc);
14+
public DateTime Date => new DateTime(_epochTicks + ((long)this.Key * TimeSpan.TicksPerMillisecond), DateTimeKind.Utc);
1315
}
1416
}

0 commit comments

Comments
 (0)