Skip to content

Commit ee190d0

Browse files
committed
Only update RFC 1123 once per second
Text only changes once per second; not fastest and allocates but moves it out of hot path
1 parent 634eb65 commit ee190d0

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

experimental/ManagedRIOHttpServer/Program.cs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,8 @@ static async Task ServeSocket(RIOTcpConnection socket)
273273
break;
274274
}
275275

276-
var date = DateTime.UtcNow.ToString("r");
277-
Encoding.UTF8.GetBytes(date, 0, dateBytes.Length, dateBytes, 0);
278-
276+
SetDateBytes(dateBytes);
277+
279278
for (var i = 1; i < count; i++)
280279
{
281280
socket.QueueSend(headerBuffer, false);
@@ -300,28 +299,46 @@ static async Task ServeSocket(RIOTcpConnection socket)
300299
socket.Close();
301300
}
302301
}
302+
303+
// Number of 100ns ticks per time unit
304+
private const long TicksPerMillisecond = 10000;
305+
private const long TicksPerSecond = TicksPerMillisecond * 1000;
306+
private long _lastSeconds = 0L;
307+
private void SetDateBytes(byte[] dateBytes)
308+
{
309+
var now = DateTime.UtcNow;
310+
var ticks = now.Ticks / TicksPerSecond;
311+
if (_lastSeconds == ticks) return;
312+
_lastSeconds = ticks;
303313

314+
var date = now.ToString("r");
315+
Encoding.ASCII.GetBytes(date, 0, dateBytes.Length, dateBytes, 0);
316+
}
304317

305-
public static void LowerCaseSIMD(byte[] data)
306-
{
307-
var A = new Vector<byte>(65); // A
308-
var Z = new Vector<byte>(90); // Z
318+
//public static void LowerCaseSIMD(ArraySegment<byte> data)
319+
//{
320+
// if (data.Offset + data.Count + Vector<byte>.Count < data.Array.Length)
321+
// {
322+
// throw new ArgumentOutOfRangeException("Nope");
323+
// }
324+
// var A = new Vector<byte>(65); // A
325+
// var Z = new Vector<byte>(90); // Z
309326

310-
for (var o = 0; o < data.Length - Vector<byte>.Count; o += Vector<byte>.Count)
311-
{
312-
var v = new Vector<byte>(data, o);
313-
314-
v = Vector.ConditionalSelect(
315-
Vector.BitwiseAnd(
316-
Vector.GreaterThanOrEqual(v, A),
317-
Vector.LessThanOrEqual(v, Z)
318-
),
319-
Vector.BitwiseOr(new Vector<byte>(0x20), v), // 0010 0000
320-
v
321-
);
322-
v.CopyTo(data, o);
323-
}
324-
}
327+
// for (var o = data.Offset; o < data.Count - Vector<byte>.Count; o += Vector<byte>.Count)
328+
// {
329+
// var v = new Vector<byte>(data.Array, o);
330+
331+
// v = Vector.ConditionalSelect(
332+
// Vector.BitwiseAnd(
333+
// Vector.GreaterThanOrEqual(v, A),
334+
// Vector.LessThanOrEqual(v, Z)
335+
// ),
336+
// Vector.BitwiseOr(new Vector<byte>(0x20), v), // 0010 0000
337+
// v
338+
// );
339+
// v.CopyTo(data.Array, o);
340+
// }
341+
//}
325342
}
326343

327344
}

0 commit comments

Comments
 (0)