Skip to content

Commit e92d325

Browse files
committed
Merge pull request #385 from benaadams/could-not-resist
requestId ticks seed, reduced GenerateRequestId allocs
2 parents c0ada44 + 2da9f13 commit e92d325

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/Microsoft.AspNet.Hosting/Internal/FastHttpRequestIdentifierFeature.cs

+29-24
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ namespace Microsoft.AspNet.Hosting.Internal
99
{
1010
public class FastHttpRequestIdentifierFeature : IHttpRequestIdentifierFeature
1111
{
12-
private static readonly string _hexChars = "0123456789ABCDEF";
13-
// Seed the _requestId for this application instance with a random int
14-
private static long _requestId = new Random().Next();
12+
// Base64 encoding - but in ascii sort order for easy text based sorting
13+
private static readonly string _encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
14+
// Seed the _requestId for this application instance with
15+
// the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001
16+
// for a roughly increasing _requestId over restarts
17+
private static long _requestId = DateTime.UtcNow.Ticks;
1518

1619
private string _id = null;
1720

@@ -32,29 +35,31 @@ public string TraceIdentifier
3235
}
3336
}
3437

35-
private static string GenerateRequestId(long id)
38+
private static unsafe string GenerateRequestId(long id)
3639
{
37-
// The following routine is ~33% faster than calling long.ToString() when testing in tight loops of
38-
// 1 million iterations.
39-
var charBuffer = new char[sizeof(long) * 2];
40-
charBuffer[0] = _hexChars[(int)(id >> 60) & 0x0f];
41-
charBuffer[1] = _hexChars[(int)(id >> 56) & 0x0f];
42-
charBuffer[2] = _hexChars[(int)(id >> 52) & 0x0f];
43-
charBuffer[3] = _hexChars[(int)(id >> 48) & 0x0f];
44-
charBuffer[4] = _hexChars[(int)(id >> 44) & 0x0f];
45-
charBuffer[5] = _hexChars[(int)(id >> 40) & 0x0f];
46-
charBuffer[6] = _hexChars[(int)(id >> 36) & 0x0f];
47-
charBuffer[7] = _hexChars[(int)(id >> 32) & 0x0f];
48-
charBuffer[8] = _hexChars[(int)(id >> 28) & 0x0f];
49-
charBuffer[9] = _hexChars[(int)(id >> 24) & 0x0f];
50-
charBuffer[10] = _hexChars[(int)(id >> 20) & 0x0f];
51-
charBuffer[11] = _hexChars[(int)(id >> 16) & 0x0f];
52-
charBuffer[12] = _hexChars[(int)(id >> 12) & 0x0f];
53-
charBuffer[13] = _hexChars[(int)(id >> 8) & 0x0f];
54-
charBuffer[14] = _hexChars[(int)(id >> 4) & 0x0f];
55-
charBuffer[15] = _hexChars[(int)(id >> 0) & 0x0f];
40+
// The following routine is ~310% faster than calling long.ToString() on x64
41+
// and ~600% faster than calling long.ToString() on x86 in tight loops of 1 million+ iterations
42+
// See: https://github.com/aspnet/Hosting/pull/385
5643

57-
return new string(charBuffer);
44+
// stackalloc to allocate array on stack rather than heap
45+
char* charBuffer = stackalloc char[13];
46+
47+
charBuffer[0] = _encode32Chars[(int)(id >> 60) & 31];
48+
charBuffer[1] = _encode32Chars[(int)(id >> 55) & 31];
49+
charBuffer[2] = _encode32Chars[(int)(id >> 50) & 31];
50+
charBuffer[3] = _encode32Chars[(int)(id >> 45) & 31];
51+
charBuffer[4] = _encode32Chars[(int)(id >> 40) & 31];
52+
charBuffer[5] = _encode32Chars[(int)(id >> 35) & 31];
53+
charBuffer[6] = _encode32Chars[(int)(id >> 30) & 31];
54+
charBuffer[7] = _encode32Chars[(int)(id >> 25) & 31];
55+
charBuffer[8] = _encode32Chars[(int)(id >> 20) & 31];
56+
charBuffer[9] = _encode32Chars[(int)(id >> 15) & 31];
57+
charBuffer[10] = _encode32Chars[(int)(id >> 10) & 31];
58+
charBuffer[11] = _encode32Chars[(int)(id >> 5) & 31];
59+
charBuffer[12] = _encode32Chars[(int)id & 31];
60+
61+
// string ctor overload that takes char*
62+
return new string(charBuffer, 0, 13);
5863
}
5964
}
6065
}

src/Microsoft.AspNet.Hosting/project.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
"type": "git",
66
"url": "git://github.com/aspnet/hosting"
77
},
8-
"compilationOptions": { "warningsAsErrors": true },
8+
"compilationOptions": {
9+
"warningsAsErrors": true,
10+
"allowUnsafe": true
11+
},
912
"dependencies": {
1013
"Microsoft.AspNet.FileProviders.Physical": "1.0.0-*",
1114
"Microsoft.AspNet.Hosting.Abstractions": "1.0.0-*",

0 commit comments

Comments
 (0)