Skip to content

Reduce binary logger serialization overhead - #14929

Merged
baronfel merged 6 commits into
dotnet:mainfrom
AlesProkop:alesprokop/binlog-metadata-reference-cache
Sep 15, 2026
Merged

baronfel merged 6 commits into
dotnet:mainfrom
AlesProkop:alesprokop/binlog-metadata-reference-cache

Conversation

@AlesProkop

@AlesProkop AlesProkop commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

Related to #10490

Context

BinaryLogger profiling identified task-item metadata serialization, repeated metadata writer allocation, and repeated string hashing as major contributors to /bl overhead. This stacks four format-preserving hot-path optimizations that reduce that work without omitting log data.

Changes Made

  • Added an identity cache that reuses emitted NameValueList record IDs for shared immutable task-item metadata.
  • Implemented a single-pass TaskItemData metadata path that hashes already-materialized metadata directly into string-index records.
  • Reused the BinaryWriter associated with the reusable NameValueList stream.
  • Added a bounded writer-local string-reference cache for repeated string instances up to 4 KiB.
  • Preserved the legacy paths for custom ITaskItem implementations, transparent proxies, raw replay, empty metadata, escaping, and metadata ordering.
  • Added focused serialization, fallback, cache, and byte-equivalence coverage in BuildEventArgsSerializationTests.

Testing

  • Built the full repository — 0 warnings, 0 errors.
  • Ran the focused BuildEventArgsSerializationTests on net11.0 and net472 — passed.
  • Built and replayed a bootstrap binary log for src/Samples/Dependency/Dependency.csproj — passed.
  • Ran the isolated serializer benchmark — optimized won 7 of 7 comparisons, with approximately 7% lower wall and CPU time.
  • Ran sequential OrchardCore GOLDWIN diagnostics:
    • normal: 133.05 seconds → 123.50 seconds, saving 9.54 seconds / 7.17%;
    • /mt: 94.52 seconds → 85.50 seconds, saving 9.02 seconds / 9.54%.

Notes

  • The binary log format and serialized content are unchanged; no format-version increment is required.
  • The reliable Windows measurements removed 25.1% of the previous normal /bl overhead and 37.3% of the previous /mt overhead.

Reuse serialized metadata records for items that share immutable copy-on-write metadata, while preserving fallback and AppDomain behavior.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: f5f373de-cc23-46fa-bd6f-9ba8c1645411

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.

🟢 Approval recommended

The optimization is localized, preserves the existing on-wire encoding, and is covered by targeted tests including byte-for-byte equivalence checks.

Pull request overview

This PR optimizes binary logger serialization of task item metadata by memoizing previously-emitted NameValueList record IDs for shared copy-on-write immutable metadata dictionaries, reducing repeated enumeration, unescaping, and hashing work in BuildEventArgsWriter.WriteTaskItemList.

Changes:

  • Added a ConditionalWeakTable<ImmutableDictionary<string,string>, StrongBox<int>> cache to reuse NameValueList record IDs for items sharing the same immutable backing metadata dictionary.
  • Introduced a fast path that enumerates immutable backing metadata directly on cache misses while preserving the existing unescape behavior and metadata ordering contract.
  • Added focused unit tests covering shared-metadata reuse, empty-metadata short-circuiting, TaskItemData fallback behavior, and byte-for-byte equivalence with the legacy path.
File summaries
File Description
src/Framework/IMetadataContainer.cs Documents the escaped-value + ordering contract required for BackingMetadata to be safely consumable for serialization fast paths.
src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs Implements metadata record-id identity caching and updates WriteNameValueList to return the emitted record id for reuse.
src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs Adds regression/compat tests validating reuse, fallbacks, and byte-for-byte serialization equivalence.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

AlesProkop and others added 3 commits September 7, 2026 09:24
Co-authored-by: Copilot <[email protected]>
Copilot-Session: f5f373de-cc23-46fa-bd6f-9ba8c1645411
Co-authored-by: Copilot <[email protected]>
Copilot-Session: f5f373de-cc23-46fa-bd6f-9ba8c1645411
Co-authored-by: Copilot <[email protected]>
Copilot-Session: f5f373de-cc23-46fa-bd6f-9ba8c1645411
@AlesProkop AlesProkop changed the title Optimize binary logger metadata serialization Reduce binary logger serialization overhead Sep 11, 2026
@baronfel
baronfel merged commit 081bc5e into dotnet:main Sep 15, 2026
14 checks passed
@AlesProkop AlesProkop mentioned this pull request Nov 19, 2026
5 of 16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants