Skip to content

Avoid materializing all custom attributes during OpenAPI schema generation - #70171

Merged
DeagleGross merged 1 commit into
mainfrom
perf/openapi-attribute-lookups
Nov 28, 2026
Merged

DeagleGross merged 1 commit into
mainfrom
perf/openapi-attribute-lookups

Conversation

@davidfowl

@davidfowl davidfowl commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Avoid materializing all custom attributes during OpenAPI schema generation

  • 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. (Small perf change found in a perf survey, no issue.)

Avoid materializing all custom attributes in OpenAPI schema generation

Description

OpenAPI schemas are rebuilt every time a document is generated, and TransformSchemaNode runs for every schema node, including every property of every type. For each node it:

  • called underlyingType.GetCustomAttributes(inherit: false). That instantiates every attribute on the type, including the [Serializable], [TypeForwardedFrom] and similar attributes on string, DateTime, int and so on. It then scanned the array with OfType<DescriptionAttribute>().LastOrDefault() and OfType<ObsoleteAttribute>().Any().
  • scanned the property attribute array with up to six separate OfType<T>() LINQ iterators.

This change:

  • Type attributes: uses GetCustomAttributes(typeof(DescriptionAttribute), inherit: false) (last one wins, as before) and IsDefined(typeof(ObsoleteAttribute), inherit: false). Only matching attributes are materialized. Derived attribute types still match.
  • Property attributes: still calls GetCustomAttributes(inherit: false) once, but finds the last DescriptionAttribute, the last DefaultValueAttribute, any ObsoleteAttribute and any ValidationAttribute in a single foreach pass. ApplyValidationAttributes is only called (with the same OfType<ValidationAttribute>() sequence) when a validation attribute exists. With none it was a no-op.
  • OpenApiDocumentService.IsRequired: uses ParameterInfo.IsDefined(typeof(RequiredAttribute), inherit: true) instead of materializing all parameter attributes for .Any(attr => attr is RequiredAttribute). PropertyAsParameterInfo overrides IsDefined consistently with GetCustomAttributes.

The order of operations on the schema is unchanged: validation, default value, then description/deprecated. Generated documents are byte-identical. No public API or behavior changes.

Note: #69861 touches the type-level ObsoleteAttribute check in the same block. Whichever PR merges second needs a trivial rebase.

Results

GenerationBenchmarks.GenerateDocument (existing benchmark), local BenchmarkDotNet, 4 A/B rounds against main (round 4 ran in reverse order). Each cell shows mean ± error. Rounds 2–3 were noisy because of contention on the shared VM.

EndpointCount Round Before After Allocated before → after
10 1 1.370 ± 0.190 ms 1.186 ± 0.224 ms 971 KB → 857 KB (−11.7%)
10 2 2.045 ± 1.070 ms 1.550 ± 0.690 ms 971 KB → 857 KB
10 3 1.570 ± 0.736 ms 1.765 ± 0.897 ms 971 KB → 857 KB
10 4 1.428 ± 0.232 ms 1.115 ± 0.075 ms 971 KB → 857 KB
100 1 11.869 ± 0.154 ms 10.606 ± 0.242 ms 8,831 KB → 7,788 KB (−11.8%)
100 2 12.758 ± 1.450 ms 11.367 ± 0.836 ms 8,831 KB → 7,788 KB
100 3 12.907 ± 1.589 ms 11.059 ± 0.526 ms 8,831 KB → 7,788 KB
100 4 11.991 ± 0.265 ms 10.678 ± 0.186 ms 8,831 KB → 7,788 KB
1000 1 134.916 ± 2.517 ms 123.499 ± 2.486 ms 87,431 KB → 77,094 KB (−11.8%)
1000 2 145.265 ± 9.887 ms 122.219 ± 3.558 ms 87,432 KB → 77,094 KB
1000 3 147.310 ± 11.834 ms 133.431 ± 12.058 ms 87,430 KB → 77,094 KB
1000 4 137.522 ± 2.665 ms 122.055 ± 1.478 ms 87,431 KB → 77,092 KB

In the quiet rounds (1 and 4) generation is about 10–13% faster, and allocations drop by 11.8% in every round. This is independent of, and additive with, #70168.

Tests

  • Added SchemaDescriptions_HandlesDerivedDescriptionAttributes, which checks that a subclass of DescriptionAttribute on a type and on a property is still honored.
  • Microsoft.AspNetCore.OpenApi.Tests: 1034 passed, 5 skipped. This includes the snapshot (Verify) integration tests that compare full generated documents, plus the existing Description, Obsolete, DefaultValue and validation attribute tests.

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

…ation

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-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc 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 frozen base/head evidence, existing feedback, and independent guide review were unavailable, preventing a complete review and deduplication of findings.

No open issues 🎉

What changed in this PR

Reduces reflection and allocation overhead during ASP.NET Core OpenAPI document generation while preserving attribute precedence and schema annotation order.

Changes:

  • Materializes only matching type-level description attributes and uses IsDefined for obsolete and required checks.
  • Consolidates property-attribute discovery into one pass, applying validation only when validation attributes are present.
  • Adds coverage for derived description attributes on types and properties.
File Description
src/​OpenApi/​test/​Microsoft.AspNetCore.OpenApi.Tests/​Services/​OpenApiSchemaService/​OpenApiSchemaService.Annotations.cs Tests derived description attributes on types and properties.
src/​OpenApi/​src/​Services/​Schemas/​OpenApiSchemaService.cs Narrows type-attribute reflection and consolidates property-attribute discovery.
src/​OpenApi/​src/​Services/​OpenApiDocumentService.cs Uses IsDefined to detect required parameters without materializing attributes.

Review effort: Balanced

@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

davidfowl commented Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

Merge assessment

Verdict: Merge candidate after a fresh aspnetcore-ci run against current main. halter73 approved Nov 25; OpenAPI document generation allocates 11.8% less, including for derived attribute types.

Size S: +35/−12 production lines, +26 test, 0 benchmark; 3 files, 1 shipping assembly (Microsoft.AspNetCore.OpenApi)
Impact Low-Medium: GenerateDocument 1000 endpoints 87,431→77,094 KB (−11.8%) in 4/4 rounds; ~10–13% faster in the 2 quiet rounds; affects document generation only, not app requests
Risk Low: leaf feature (OpenAPI schema generation); reflection API swaps with the same semantics
Tests 1 new test for a derived DescriptionAttribute on a type and a property; 1034 OpenApi tests pass, including Verify snapshot tests of full documents (author-reported)
Review halter73 formally approved Nov 25; no open review threads; Copilot review reported no open issues
CI aspnetcore-ci succeeded on d454930 Oct 31; aspnetcore-components-e2e neutral (not triggered); no conflicts, labeled pending-ci-rerun
Complexity Low: 4 LINQ OfType scans replaced by one switch pass; GetCustomAttributes(Type) and IsDefined for type-level lookups
Reversibility Easy; 2 files
Security sensitivity Not assessed (repo review guidance)
Encoding/escaping parity n/a; snapshot tests compare full generated documents
Behavior / public API change None

Reasons to merge

  • Allocations drop by the same 11.8% in all 4 rounds, at 10, 100 and 1000 endpoints.
  • "Last DescriptionAttribute wins" and the order validation, default, description/deprecated are preserved.
  • PropertyAsParameterInfo.IsDefined checks both the constructor parameter and the property, so [AsParameters] required detection is unchanged.

Reasons not to merge yet

What would change this verdict: A fresh CI failure or Verify snapshot diff after rebasing onto current main would make this a Hold.

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

@DeagleGross
DeagleGross merged commit b5afc47 into main Nov 28, 2026
43 checks passed
@DeagleGross
DeagleGross deleted the perf/openapi-attribute-lookups branch November 28, 2026 13:12
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc 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.

5 participants