Skip to content

Commit

Permalink
Added support for fast formatting of TimeSpan (to log unity startup s…
Browse files Browse the repository at this point in the history
…econds)
  • Loading branch information
CptMoore committed Dec 24, 2024
1 parent 7da1676 commit 58faa50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 14 additions & 10 deletions ModTek/Features/Logging/FastBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ internal void Append(int value)
FormattingHelpers.WriteDigits(position, (uint)value, digits);
}

internal void Append(decimal value)
{
// TODO optimize
Append(value.ToString(CultureInfo.InvariantCulture));
}

internal void Append(string value)
{
var processingCount = value.Length;
Expand Down Expand Up @@ -169,15 +163,25 @@ private void SetAscii(byte* positionIterPtr, char* charsIterPtr, int offset, out
}

internal void Append(DateTime value)
{
AppendTime(value.Hour, value.Minute, value.Second, value.Ticks);
}

internal void Append(TimeSpan value)
{
AppendTime(value.Hours, value.Minutes, value.Seconds, value.Ticks);
}

private void AppendTime(int hours, int minutes, int seconds, long ticks)
{
var position = GetPointerAndIncrementLength(17);
FormattingHelpers.WriteDigits(position, value.Hour, 2);
FormattingHelpers.WriteDigits(position, hours, 2);
position[2] = (byte)':';
FormattingHelpers.WriteDigits(position + 3, value.Minute, 2);
FormattingHelpers.WriteDigits(position + 3, minutes, 2);
position[5] = (byte)':';
FormattingHelpers.WriteDigits(position + 6, value.Second, 2);
FormattingHelpers.WriteDigits(position + 6, seconds, 2);
position[8] = (byte)'.';
FormattingHelpers.WriteDigits(position + 9, value.Ticks, 7);
FormattingHelpers.WriteDigits(position + 9, ticks, 7);
position[16] = (byte)' ';
}

Expand Down
4 changes: 1 addition & 3 deletions ModTek/Features/Logging/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ internal int GetFormattedLogLine(ref MTLoggerMessageDto messageDto, out byte[] b
if (_startupTimeEnabled)
{
var ts = messageDto.StartupTime();
var secondsWithFraction = ts.Ticks * 1E-07m;
s_buffer.Append(secondsWithFraction);
s_buffer.Append((byte)' ');
s_buffer.Append(ts);
}

if (messageDto.ThreadId != s_unityMainThreadId)
Expand Down

0 comments on commit 58faa50

Please sign in to comment.