Skip to content

Reduce allocations when serializing DistributedSession - #70169

Open
davidfowl wants to merge 2 commits into
mainfrom
perf/session-serialize
Open

davidfowl wants to merge 2 commits into
mainfrom
perf/session-serialize

Conversation

@davidfowl

@davidfowl davidfowl commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Reduce allocations when serializing DistributedSession

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Reduce allocations and CPU when committing and loading DistributedSession.

Description

Motivation: every request that modifies session state calls DistributedSession.CommitAsync. It serializes into a growable MemoryStream, writing one byte at a time for the length prefixes, and then copies the result with ToArray(). For a session holding a few KB, that allocates the payload about three times: the doubling buffers plus the final copy. Loading wraps the cached byte[] in a MemoryStream and reads every length prefix byte by byte through virtual Stream.ReadByte calls.

Change (internal implementation only, no public API or behavior change):

  • Serialize computes the exact payload size first, then writes into a single right-sized byte[] with span copies and BinaryPrimitives.
    • Validation is unchanged and runs before anything is written: the 3-byte entry count, then the 2-byte key length. The same ArgumentOutOfRangeExceptions are thrown.
  • Deserialize reads straight from the cached byte[] through a small private struct reader that mirrors MemoryStream semantics:
    • ReadByte returns -1 past the end.
    • ReadBytes allocates first, so negative lengths still throw OverflowException, then throws EndOfStreamException when data is short.
    • So malformed or truncated payloads fail exactly as before.
  • The wire format is byte-identical.

Benchmarks

Local BenchmarkDotNet run in-process, 3 rounds, main vs PR. The setup is DistributedSession with an in-memory IDistributedCache stub. Commit creates a session, Sets N entries and calls CommitAsync. Load creates a session, calls LoadAsync and does one TryGetValue. Means are shown as round 1 / round 2 / round 3.

Method Entries Value size main mean (ns) PR mean (ns) main alloc PR alloc
Commit 3 64 B 1,217 / 799 / 819 934 / 624 / 630 3,000 B 2,112 B (−30%)
Commit 3 1 KB 2,989 / 2,048 / 1,871 1,946 / 1,272 / 1,223 18,617 B 10,753 B (−42%)
Commit 20 64 B 5,491 / 3,936 / 3,963 4,698 / 3,269 / 3,255 15,097 B 11,089 B (−27%)
Commit 20 1 KB 16,953 / 12,086 / 11,304 11,158 / 7,124 / 7,589 136,104 B 68,692 B (−50%)
Load 3 64 B 549 / 426 / 383 382 / 338 / 361 1,192 B 1,128 B
Load 3 1 KB 601 / 579 / 568 523 / 552 / 571 4,072 B 4,008 B
Load 20 64 B 1,555 / 1,583 / 1,528 1,378 / 1,333 / 1,427 5,912 B 5,848 B
Load 20 1 KB 2,953 / 2,857 / 2,741 2,712 / 2,609 / 2,608 25,113 B 25,049 B

Round 1 used a different job configuration than rounds 2–3, so its absolute numbers run higher; compare within a round. Commit is roughly 20–37% faster with 27–50% less allocation. Load is neutral to about 10% faster, saving the MemoryStream. None of the cases regressed. Error was ≤ ~10% of the mean in all rounds.

Benchmark code (not included in the PR)
[MemoryDiagnoser]
public class SessionBenchmark
{
    private readonly FixedCache _cache = new();
    private (string Key, byte[] Value)[] _entries;
    [Params(3, 20)] public int Entries { get; set; }
    [Params(64, 1024)] public int ValueSize { get; set; }

    [GlobalSetup]
    public void Setup()
    {
        _entries = new (string, byte[])[Entries];
        for (var i = 0; i < Entries; i++) { _entries[i] = ("SessionKey_" + i, new byte[ValueSize]); }
        var s = Create(true);
        foreach (var (k, v) in _entries) { s.Set(k, v); }
        s.CommitAsync().GetAwaiter().GetResult();
    }

    private DistributedSession Create(bool isNew) =>
        new DistributedSession(_cache, "key", TimeSpan.FromMinutes(20), TimeSpan.FromMinutes(1), static () => true, NullLoggerFactory.Instance, isNew);

    [Benchmark]
    public async Task<bool> Load() { var s = Create(false); await s.LoadAsync(); return s.TryGetValue("SessionKey_0", out _); }

    [Benchmark]
    public async Task Commit()
    {
        var s = Create(true);
        foreach (var (k, v) in _entries) { s.Set(k, v); }
        await s.CommitAsync();
    }
    // FixedCache: IDistributedCache that stores the last Set value and returns it from Get/GetAsync.
}

Tests

  • New tests in SessionTests:
    • DistributedSession_CommitWritesExpectedFormat: an exact byte-for-byte golden payload, including a multi-byte UTF-8 key and an empty value.
    • DistributedSession_RoundTripsValues: 100 KB value, empty value, 1,000-char key.
    • DistributedSession_TruncatedPayload_LoadAsyncThrowsEndOfStream: LoadAsync still throws EndOfStreamException, and the sync path still marks the session unavailable.
    • DistributedSession_UnknownRevision_IsTreatedAsEmptyAndRewritten.
  • All 4 new tests also pass against the main implementation, which confirms identical format and failure behavior.
  • Microsoft.AspNetCore.Session.Tests: 32/32 passed.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

Compute the exact payload size up front and write the session into one
byte[] instead of growing a MemoryStream and copying it with ToArray().
Deserialize directly from the cached byte[] instead of wrapping it in a
MemoryStream and reading byte-by-byte through virtual Stream calls.
The wire format is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 07:24
@github-actions github-actions Bot added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares label Oct 31, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The required frozen review bundle was missing, preventing validation of head/base identities, routed guidance, and existing-feedback coverage.

No open issues 🎉

What changed in this PR

Reduces allocation and CPU overhead in DistributedSession commit and load operations while preserving the existing wire format and malformed-payload behavior.

Changes:

  • Replaces growable stream serialization with one exactly sized byte array and span-based writes.
  • Reads cached payloads directly through a private byte-array reader.
  • Adds tests for exact serialized bytes, value round trips, truncated payloads, and unknown revisions.
File Description
src/​Middleware/​Session/​test/​SessionTests.cs Adds serialization compatibility and error-handling tests.
src/​Middleware/​Session/​src/​DistributedSession.cs Replaces stream-based serialization and deserialization with direct byte-array operations.

Review effort: Balanced

Copy link
Copy Markdown
Member Author

/azp run aspnetcore-components-e2e

The Blazor E2E tests on Linux (CoreCLR) leg failed (build 1657484). I can't reach the AzDO test logs from my environment, so I don't know which test failed yet. I'm rerunning it to see whether the failure reproduces. Some Blazor E2E tests (SupplyParameterFromSession, TempDataSessionStorage) use session, but this PR's serialized payload is byte-identical to main: the new golden-bytes and round-trip tests in SessionTests pass against both main and this branch. If the rerun fails again I'll investigate it as a real failure.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

Copy link
Copy Markdown
Member Author

/azp run aspnetcore-components-e2e

Update: Build Analysis shows that the E2E failures are known flaky tests unrelated to session:

  • ServerRoutingTest.NavigationLock_CanBlockExternalNavigation: 25.22% failure rate. It's an external-navigation URL assertion on the CoreCLR and Mono server legs.
  • VirtualizationTest.VariableHeight_HtmlTable_DoesNotProduceNestedTrElements: 9.53% failure rate.

The session-based E2E tests (SupplyParameterFromSession, TempDataSessionStorage) passed. Rerunning.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

Copy link
Copy Markdown
Member Author

/azp run aspnetcore-components-e2e

Retrying now that the previous run has completed. The failures are the known flaky tests listed above.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copy link
Copy Markdown
Member Author

Retriggered CI with an empty commit, a94373e, because /azp run reports that no pipelines are associated with this PR.

Evidence that the failure is flaky. Build Analysis shows only known flaky Blazor E2E tests failed:

  • ServerRoutingTest.NavigationLock_CanBlockExternalNavigation: 25.22% failure rate.
  • VirtualizationTest.VariableHeight_HtmlTable_DoesNotProduceNestedTrElements: 9.53% failure rate.

Neither touches session. Every other check, including the session-based E2E tests and all Helix test legs, passed.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
Member

/review

@davidfowl davidfowl added the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026 — with Copilot SWE Agent
@davidfowl davidfowl removed the agent:working An AI agent is actively working on this issue/PR label Nov 6, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Nov 13, 2026

Copy link
Copy Markdown
Member Author

Merge assessment

Verdict: Lean merge. One-file change in DistributedSession with a byte-identical wire format, golden-byte tests that also pass on main, and green CI.

Size M: +75/−73 production lines, +82 test, +0 benchmark; 2 files, 1 shipping assembly (Microsoft.AspNetCore.Session); no src/Shared
Impact Medium: CommitAsync −20 to −37% time, −27 to −50% allocation (3×64 B: 3,000 → 2,112 B; 20×1 KB: 136 KB → 69 KB); LoadAsync neutral to −10%. Only requests that modify session
Risk Medium: Session middleware; reads payloads from IDistributedCache
Tests 4 new in SessionTests: golden payload (multi-byte UTF-8 key, empty value), 100 KB round trip, truncated payload → EndOfStreamException, unknown revision. All pass on main, so they guard parity
Review No approvals (REVIEW_REQUIRED); no review threads; no human review yet
CI Green on a94373ea (Oct 31). Mergeable, 469 commits behind main; no commits to src/Middleware/Session/src since
Complexity Neutral: replaces 8 Stream helpers with a size pass and a private PayloadReader struct
Reversibility Easy; single file
Security sensitivity Not assessed (repo review guidance excludes it)
Encoding/escaping parity Binary format only; key bytes still come from unchanged EncodedKey; golden-byte and truncation tests exist
Behavior / public API change None in practice; a payload over 2 GB would throw OverflowException from checked instead of MemoryStream's IOException

Reasons to merge

  • Halves allocation for larger sessions on every commit.
  • Wire format is unchanged and pinned by a golden-byte test.
  • Malformed-payload behavior (-1 past end, OverflowException on negative length, EndOfStreamException on short data) matches MemoryStream.

Reasons not to merge yet

  • The benchmark lives only in the PR body, not in a perf project.
  • Session is used by a minority of apps, so reach is limited.
  • CI is 16 days old against a branch 469 commits behind.

What would change this verdict: a Session owner objecting to the hand-written reader would move this to Hold.

🤖 Posted by ASP.NET Core Perf 2 (AI Agent)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants