Skip to content

Commit 75371bf

Browse files
committed
Avoid some allocations by checked IsEnabled first in Servers/Kestrel
1 parent d807bf0 commit 75371bf

File tree

5 files changed

+61
-47
lines changed

5 files changed

+61
-47
lines changed

src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public QuicConnectionContext(QuicConnection connection, QuicTransportContext con
3232
Features.Set<ITlsConnectionFeature>(new FakeTlsConnectionFeature());
3333
Features.Set<IProtocolErrorCodeFeature>(this);
3434

35-
_log.AcceptedConnection(ConnectionId);
35+
if (_log.IsEnabled(LogLevel.Debug))
36+
{
37+
_log.AcceptedConnection(ConnectionId);
38+
}
3639
}
3740

3841
public ValueTask<ConnectionContext> StartUnidirectionalStreamAsync()

src/Servers/Kestrel/Transport.Quic/src/Internal/QuicStreamContext.cs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,12 @@ public QuicStreamContext(QuicStream stream, QuicConnectionContext connection, Qu
7070
public bool CanRead { get; }
7171
public bool CanWrite { get; }
7272

73-
public long StreamId
74-
{
75-
get
76-
{
77-
return _stream.StreamId;
78-
}
79-
}
73+
public long StreamId => _stream.StreamId;
8074

8175
public override string ConnectionId
8276
{
83-
get
84-
{
85-
if (_connectionId == null)
86-
{
87-
_connectionId = $"{_connection.ConnectionId}:{StreamId}";
88-
}
89-
return _connectionId;
90-
}
91-
set
92-
{
93-
_connectionId = value;
94-
}
77+
get => _connectionId ??= $"{_connection.ConnectionId}:{StreamId}";
78+
set => _connectionId = value;
9579
}
9680

9781
public long Error { get; set; }
@@ -147,7 +131,10 @@ private async Task DoReceive()
147131
{
148132
// This is unexpected.
149133
error = ex;
150-
_log.StreamError(ConnectionId, error);
134+
if (_log.IsEnabled(LogLevel.Debug))
135+
{
136+
_log.StreamError(ConnectionId, error);
137+
}
151138
}
152139
finally
153140
{
@@ -180,14 +167,14 @@ private async Task ProcessReceives()
180167

181168
var paused = !flushTask.IsCompleted;
182169

183-
if (paused)
170+
if (paused && _log.IsEnabled(LogLevel.Debug))
184171
{
185172
_log.StreamPause(ConnectionId);
186173
}
187174

188175
var result = await flushTask;
189176

190-
if (paused)
177+
if (paused && _log.IsEnabled(LogLevel.Debug))
191178
{
192179
_log.StreamResume(ConnectionId);
193180
}
@@ -250,7 +237,10 @@ private async Task DoSend()
250237
{
251238
shutdownReason = ex;
252239
unexpectedError = ex;
253-
_log.ConnectionError(ConnectionId, unexpectedError);
240+
if (_log.IsEnabled(LogLevel.Debug))
241+
{
242+
_log.ConnectionError(ConnectionId, unexpectedError);
243+
}
254244
}
255245
finally
256246
{
@@ -307,7 +297,10 @@ public override void Abort(ConnectionAbortedException abortReason)
307297

308298
_aborted = true;
309299

310-
_log.StreamAbort(ConnectionId, abortReason.Message);
300+
if (_log.IsEnabled(LogLevel.Debug))
301+
{
302+
_log.StreamAbort(ConnectionId, abortReason.Message);
303+
}
311304

312305
lock (_shutdownLock)
313306
{
@@ -328,7 +321,10 @@ private async ValueTask ShutdownWrite(Exception? shutdownReason)
328321
// TODO: Exception is always allocated. Consider only allocating if receive hasn't completed.
329322
_shutdownReason = shutdownReason ?? new ConnectionAbortedException("The Quic transport's send loop completed gracefully.");
330323

331-
_log.StreamShutdownWrite(ConnectionId, _shutdownReason.Message);
324+
if (_log.IsEnabled(LogLevel.Debug))
325+
{
326+
_log.StreamShutdownWrite(ConnectionId, _shutdownReason.Message);
327+
}
332328
_stream.Shutdown();
333329
}
334330

src/Servers/Kestrel/Transport.Quic/src/Internal/QuicTrace.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Experimental.Quic.Intern
99
internal class QuicTrace : IQuicTrace
1010
{
1111
private static readonly Action<ILogger, string, Exception?> _acceptedConnection =
12-
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "AcceptedConnection"), @"Connection id ""{ConnectionId}"" accepted.");
12+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "AcceptedConnection"), @"Connection id ""{ConnectionId}"" accepted.", skipEnabledCheck: true);
1313
private static readonly Action<ILogger, string, Exception?> _acceptedStream =
1414
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(2, "AcceptedStream"), @"Stream id ""{ConnectionId}"" accepted.");
1515
private static readonly Action<ILogger, string, Exception?> _connectionError =
16-
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(3, "ConnectionError"), @"Connection id ""{ConnectionId}"" unexpected error.");
16+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(3, "ConnectionError"), @"Connection id ""{ConnectionId}"" unexpected error.", skipEnabledCheck: true);
1717
private static readonly Action<ILogger, string, Exception?> _streamError =
18-
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(4, "StreamError"), @"Stream id ""{ConnectionId}"" unexpected error.");
18+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(4, "StreamError"), @"Stream id ""{ConnectionId}"" unexpected error.", skipEnabledCheck: true);
1919
private static readonly Action<ILogger, string, Exception?> _streamPause =
20-
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(5, "StreamPause"), @"Stream id ""{ConnectionId}"" paused.");
20+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(5, "StreamPause"), @"Stream id ""{ConnectionId}"" paused.", skipEnabledCheck: true);
2121
private static readonly Action<ILogger, string, Exception?> _streamResume =
22-
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(6, "StreamResume"), @"Stream id ""{ConnectionId}"" resumed.");
22+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(6, "StreamResume"), @"Stream id ""{ConnectionId}"" resumed.", skipEnabledCheck: true);
2323
private static readonly Action<ILogger, string, string, Exception?> _streamShutdownWrite =
24-
LoggerMessage.Define<string, string>(LogLevel.Debug, new EventId(7, "StreamShutdownWrite"), @"Stream id ""{ConnectionId}"" shutting down writes because: ""{Reason}"".");
24+
LoggerMessage.Define<string, string>(LogLevel.Debug, new EventId(7, "StreamShutdownWrite"), @"Stream id ""{ConnectionId}"" shutting down writes because: ""{Reason}"".", skipEnabledCheck: true);
2525
private static readonly Action<ILogger, string, string, Exception?> _streamAborted =
26-
LoggerMessage.Define<string, string>(LogLevel.Debug, new EventId(8, "StreamAbort"), @"Stream id ""{ConnectionId}"" aborted by application because: ""{Reason}"".");
26+
LoggerMessage.Define<string, string>(LogLevel.Debug, new EventId(8, "StreamAbort"), @"Stream id ""{ConnectionId}"" aborted by application because: ""{Reason}"".", skipEnabledCheck: true);
2727

2828
private ILogger _logger;
2929

src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ private async Task DoReceive()
156156
if (bytesReceived == 0)
157157
{
158158
// FIN
159-
_trace.ConnectionReadFin(ConnectionId);
159+
if (_trace.IsEnabled(LogLevel.Debug))
160+
{
161+
_trace.ConnectionReadFin(ConnectionId);
162+
}
160163
break;
161164
}
162165

@@ -166,14 +169,14 @@ private async Task DoReceive()
166169

167170
var paused = !flushTask.IsCompleted;
168171

169-
if (paused)
172+
if (paused && _trace.IsEnabled(LogLevel.Debug))
170173
{
171174
_trace.ConnectionPause(ConnectionId);
172175
}
173176

174177
var result = await flushTask;
175178

176-
if (paused)
179+
if (paused && _trace.IsEnabled(LogLevel.Debug))
177180
{
178181
_trace.ConnectionResume(ConnectionId);
179182
}
@@ -204,7 +207,7 @@ private async Task DoReceive()
204207
// This exception should always be ignored because _shutdownReason should be set.
205208
error = ex;
206209

207-
if (!_socketDisposed)
210+
if (!_socketDisposed && _trace.IsEnabled(LogLevel.Debug))
208211
{
209212
// This is unexpected if the socket hasn't been disposed yet.
210213
_trace.ConnectionError(ConnectionId, error);
@@ -214,7 +217,10 @@ private async Task DoReceive()
214217
{
215218
// This is unexpected.
216219
error = ex;
217-
_trace.ConnectionError(ConnectionId, error);
220+
if (_trace.IsEnabled(LogLevel.Debug))
221+
{
222+
_trace.ConnectionError(ConnectionId, error);
223+
}
218224
}
219225
finally
220226
{
@@ -265,7 +271,10 @@ private async Task DoSend()
265271
catch (SocketException ex) when (IsConnectionResetError(ex.SocketErrorCode))
266272
{
267273
shutdownReason = new ConnectionResetException(ex.Message, ex);
268-
_trace.ConnectionReset(ConnectionId);
274+
if (_trace.IsEnabled(LogLevel.Debug))
275+
{
276+
_trace.ConnectionReset(ConnectionId);
277+
}
269278
}
270279
catch (Exception ex)
271280
when ((ex is SocketException socketEx && IsConnectionAbortError(socketEx.SocketErrorCode)) ||
@@ -278,7 +287,10 @@ private async Task DoSend()
278287
{
279288
shutdownReason = ex;
280289
unexpectedError = ex;
281-
_trace.ConnectionError(ConnectionId, unexpectedError);
290+
if (_trace.IsEnabled(LogLevel.Debug))
291+
{
292+
_trace.ConnectionError(ConnectionId, unexpectedError);
293+
}
282294
}
283295
finally
284296
{
@@ -331,7 +343,10 @@ private void Shutdown(Exception? shutdownReason)
331343
// to half close the connection which is currently unsupported.
332344
_shutdownReason = shutdownReason ?? new ConnectionAbortedException("The Socket transport's send loop completed gracefully.");
333345

334-
_trace.ConnectionWriteFin(ConnectionId, _shutdownReason.Message);
346+
if (_trace.IsEnabled(LogLevel.Debug))
347+
{
348+
_trace.ConnectionWriteFin(ConnectionId, _shutdownReason.Message);
349+
}
335350

336351
try
337352
{

src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketsTrace.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ internal class SocketsTrace : ISocketsTrace
1111
// ConnectionRead: Reserved: 3
1212

1313
private static readonly Action<ILogger, string, Exception?> _connectionPause =
14-
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(4, "ConnectionPause"), @"Connection id ""{ConnectionId}"" paused.");
14+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(4, "ConnectionPause"), @"Connection id ""{ConnectionId}"" paused.", skipEnabledCheck: true);
1515

1616
private static readonly Action<ILogger, string, Exception?> _connectionResume =
17-
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(5, "ConnectionResume"), @"Connection id ""{ConnectionId}"" resumed.");
17+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(5, "ConnectionResume"), @"Connection id ""{ConnectionId}"" resumed.", skipEnabledCheck: true);
1818

1919
private static readonly Action<ILogger, string, Exception?> _connectionReadFin =
20-
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(6, "ConnectionReadFin"), @"Connection id ""{ConnectionId}"" received FIN.");
20+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(6, "ConnectionReadFin"), @"Connection id ""{ConnectionId}"" received FIN.", skipEnabledCheck: true);
2121

2222
private static readonly Action<ILogger, string, string, Exception?> _connectionWriteFin =
23-
LoggerMessage.Define<string, string>(LogLevel.Debug, new EventId(7, "ConnectionWriteFin"), @"Connection id ""{ConnectionId}"" sending FIN because: ""{Reason}""");
23+
LoggerMessage.Define<string, string>(LogLevel.Debug, new EventId(7, "ConnectionWriteFin"), @"Connection id ""{ConnectionId}"" sending FIN because: ""{Reason}""", skipEnabledCheck: true);
2424

2525
// ConnectionWrite: Reserved: 11
2626

2727
// ConnectionWriteCallback: Reserved: 12
2828

2929
private static readonly Action<ILogger, string, Exception?> _connectionError =
30-
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(14, "ConnectionError"), @"Connection id ""{ConnectionId}"" communication error.");
30+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(14, "ConnectionError"), @"Connection id ""{ConnectionId}"" communication error.", skipEnabledCheck: true);
3131

3232
private static readonly Action<ILogger, string, Exception?> _connectionReset =
33-
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(19, "ConnectionReset"), @"Connection id ""{ConnectionId}"" reset.");
33+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(19, "ConnectionReset"), @"Connection id ""{ConnectionId}"" reset.", skipEnabledCheck: true);
3434

3535
private readonly ILogger _logger;
3636

0 commit comments

Comments
 (0)