Repository navigation
Conversation
- Cache the formatted date/time strings per second and the local port string. - Snapshot the additional request header names once instead of calling ToList per request. - Reuse an empty array when there are no additional headers. - Replace whitespace with a vectorized scan and a single string allocation. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
The required frozen head/base source-and-guidance bundle and existing-feedback snapshot were missing, preventing validation of revision provenance and feedback deduplication.
No open issues 🎉
What changed in this PR
Reduces per-request CPU work and allocations in W3C logging middleware while preserving log output and field-array lifetimes.
Changes:
- Caches formatted date/time and port values, snapshots additional header names, and avoids empty-array allocations.
- Reworks whitespace replacement to avoid an intermediate character array.
- Adds output-equivalence coverage across requests with changing ports, whitespace, and optional headers.
| File | Description |
|---|---|
src/Middleware/HttpLogging/test/W3CLoggingMiddlewareTests.cs |
Adds checks for formatted values, timestamps, and additional-header ordering across requests. |
src/Middleware/HttpLogging/src/W3CLoggingMiddleware.cs |
Caches repeated formatting and reduces header-handling and whitespace-replacement allocations. |
Review effort: Balanced
|
/azp run aspnetcore-components-e2e The Blazor E2E tests on Linux (Mono client and WebAssembly) job failed on a 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
No pipelines are associated with this pull request. |
Co-authored-by: Copilot <[email protected]>
|
Pushed empty commit ce26859 to retrigger CI. The only failure was 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |
Adds a Microsoft.AspNetCore.HttpLogging.Microbenchmarks project that runs the UseW3CLogging pipeline with default fields and with additional request headers. The logger's queue is completed so each request still formats the full log line but nothing is written to disk. Co-authored-by: Copilot <[email protected]>
|
CI on head 6e98cce has one failing test: It's unrelated to this PR. The diff touches only HttpLogging files ( I'm rerunning components-e2e once. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/azp run aspnetcore-components-e2e 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
No pipelines are associated with this pull request. |
Co-authored-by: Copilot <[email protected]>
|
Retriggered CI with an empty commit (new head 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
The only failure on db4f5e0 is This failure is unrelated to the PR. The diff only changes HttpLogging's This PR's single retrigger was already used, so no further retrigger is planned. Apart from this known flake, the PR is ready for review. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Merge assessmentVerdict: Hold until the
Reasons to merge
Reasons not to merge yet
What would change this verdict: a green Mono server rerun and resolution of the field-order thread. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
| // Snapshot of the additional request headers in the same order the W3CLogger writes them. | ||
| private readonly string[] _additionalRequestHeaderNames; | ||
| private DateTimeStrings? _dateTimeStrings; | ||
| private PortString? _portString; |
There was a problem hiding this comment.
Nit: This class is weird and already had static fields in the middle of readonly fields, but lets move all these up top with the other instance fields.
Reduce per-request allocations in W3CLoggingMiddleware
Cut CPU and allocations on the
UseW3CLoggingrequest path.Description
The log output is unchanged.
W3CLoggingMiddleware.Invokeruns for every request when W3C logging is on. It did several pieces of work that repeat the same result from request to request:now.ToString("yyyy-MM-dd")andnow.ToString("HH:mm:ss")per requestLocalPort.ToString()per request_additionalRequestHeaders.ToList()per request (a List and its array)string[]in the constructor. The set was already cloned and fixed there, and enumeration order is the same.new string[0]when no additional headers are configured[](Array.Empty)ReplaceWhitespace: char-by-char scan, thenToCharArray()+new stringwhen whitespace is found (e.g. any normal User-Agent)IndexOfAnyInRange('\0', ' '), then onestring.CreateThe cached values are immutable strings, so nothing mutable is shared between requests. #69108 also touches W3C logging; this PR doesn't touch any of its files, and it doesn't pool the
elementsarrays, so their lifetime is unchanged.Benchmark
I used a local BenchmarkDotNet harness (not included). It builds the real pipeline (
AddW3CLogging+UseW3CLogging) with the logger's background queue completed, so each request still formats the full log line but nothing is written to disk. The request has a typical User-Agent, Host, Referer and query. Timings are on 2 cores, 3 runs each.Without the HttpContext cost, the middleware's own allocations drop by 27% with default fields (936 → 680 B) and by 37% with additional headers (1216 → 784 B).
Tests
LogsFormattedValuesAcrossRequestssends several requests with alternating local ports, User-Agent values containing spaces, tabs and control characters, and missing or present additional headers. It checks the exact log lines, the date/time format and that the column order follows the sorted header names. It passes against both the original and the new implementation.Benchmark (3 interleaved rounds)
The benchmark is committed in
src/Middleware/HttpLogging/perf/Microbenchmarks/W3CLoggingMiddlewareBenchmark.cs. It builds the realAddW3CLogging+UseW3CLoggingpipeline with the logger's background queue completed, so each request still formats the full log line inW3CLoggerbut nothing is written to disk. The request has a typical User-Agent (with spaces), Host, Referer and query. The second case adds 3AdditionalRequestHeaders.Result: a consistent win in every round. With default fields, time drops 11% and allocations drop 392 B per request (3,208 → 2,816 B). With 3 additional request headers, time drops 19% and allocations drop 568 B (3,528 → 2,960 B).
ContextOnlydrifts −3.6% between builds, well below these deltas.BenchmarkDotNet v0.13.0, InProcess, 5 warmup + 15 measured iterations, Server GC. Rounds ran in the order main, PR, main, PR, main, PR. Both builds use the same benchmark source; "main" is this PR's merge base (4f556f7). Allocated is exact bytes per op from the GC counters. Machine: AMD EPYC 9V74, 2 cores, Ubuntu 24.04 (container), .NET SDK 12.0.100-alpha.1.26521.103, runtime 12.0.0 (12.0.26.52911).
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)