Skip to content

Commit d2235b9

Browse files
committed
Max ThreadStatic actually static
1 parent 9fceb96 commit d2235b9

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/Benchmarks/Startup.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public class PooledContextFactory : IHttpContextFactory
131131
private IHttpContextAccessor _httpContextAccessor;
132132

133133
[ThreadStatic]
134-
Queue<DefaultHttpContext> _contextPool;
134+
static Queue<DefaultHttpContext> _contextPool;
135135

136136
public PooledContextFactory() : this(httpContextAccessor: null)
137137
{
@@ -150,23 +150,20 @@ private Queue<DefaultHttpContext> ContextPool
150150
{
151151
_contextPool = new Queue<DefaultHttpContext>(16);
152152
}
153+
153154
return _contextPool;
154155
}
155156
}
156157

157158
public HttpContext Create(IFeatureCollection featureCollection)
158159
{
159160
var contextPool = ContextPool;
160-
// locking as ThreadStatic didn't appear to be behaving thread safe via property on coreclr x64
161-
lock (contextPool)
161+
if (contextPool.Count > 0)
162162
{
163-
if (contextPool.Count > 0)
164-
{
165-
var context = contextPool.Dequeue();
166-
// Needs https://github.com/aspnet/HttpAbstractions/pull/501
167-
context.UpdateFeatures(featureCollection);
168-
return context;
169-
}
163+
var context = contextPool.Dequeue();
164+
// Needs https://github.com/aspnet/HttpAbstractions/pull/501
165+
//context.UpdateFeatures(featureCollection);
166+
return context;
170167
}
171168

172169
return new DefaultHttpContext(featureCollection);
@@ -184,13 +181,10 @@ public void Dispose(HttpContext httpContext)
184181
if (context != null)
185182
{
186183
var contextPool = ContextPool;
187-
// locking as ThreadStatic didn't appear to be behaving thread safe via property on coreclr x64
188-
lock (contextPool)
184+
185+
if (contextPool.Count < 16)
189186
{
190-
if (contextPool.Count < 16)
191-
{
192-
contextPool.Enqueue(context);
193-
}
187+
contextPool.Enqueue(context);
194188
}
195189
}
196190
}

0 commit comments

Comments
 (0)