Skip to content

feat: Enable AOT-compatibility#96

Merged
HofmeisterAn merged 25 commits intotestcontainers:mainfrom
campersau:aot
Apr 21, 2026
Merged

feat: Enable AOT-compatibility#96
HofmeisterAn merged 25 commits intotestcontainers:mainfrom
campersau:aot

Conversation

@campersau
Copy link
Copy Markdown

@campersau campersau commented Mar 30, 2026

  • Enable IsAotCompatible
  • Generate DockerModelsJsonSerializerContext with all docker API models which have JSON parameters, so query string only models are not added for JsonSerialization
  • Add DockerExtendedJsonSerializerContext which includes types directly used by *Operations (this can be removed / merged with DockerModelsJsonSerializerContext when STJ source generator fails when defining the serializer context in 2 partial classes dotnet/runtime#99669 is available)
  • Update reflection APIs with DynamicallyAccessedMembers attributes
  • Update xunit to xunit.v3.aot.mtp-v2 4.0.0-pre.33 which is still in preview but required to actually test AOT.
  • Use xunit.v3.mtp-v2 4.0.0-pre.33 for .NET 8.0 because xunit AOT only works from .NET 9 onwards.
  • Cleanup preprocessor directives
  • Change all IEnumerable responses to be IList for consistency (breaking change)
  • Use generic QueryStringParameter to reduce reflection

fixes #88

@campersau
Copy link
Copy Markdown
Author

campersau commented Apr 1, 2026

Before (main)

Method Mean Error StdDev Allocated
CreateContainerRequestResponse 190.7 ms 107.9 ms 5.91 ms 34.55 KB
StartContainerRequestResponse 717.5 ms 371.3 ms 20.35 ms 47.59 KB

Docker.DotNet.dll 464 KB (475.648 Bytes)

image

After (AoT)

Method Mean Error StdDev Allocated
CreateContainerRequestResponse 185.8 ms 109.1 ms 5.98 ms 41.77 KB
StartContainerRequestResponse 702.5 ms 478.7 ms 26.24 ms 68.9 KB

Docker.DotNet.dll 3,01 MB (3.162.112 Bytes)

image

@HofmeisterAn
Copy link
Copy Markdown
Collaborator

FYI: I will need a few days for the review. I'm pretty busy this week.

@HofmeisterAn
Copy link
Copy Markdown
Collaborator

I'll do the review this weekend.

@campersau
Copy link
Copy Markdown
Author

No worries. This currently uses a preview version of xunit aot. Stable release should be soon.

Copy link
Copy Markdown
Collaborator

@HofmeisterAn HofmeisterAn left a comment

Choose a reason for hiding this comment

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

Thanks, the PR looks good! I just have a few questions so I can understand a few things better.

Comment thread test/Docker.DotNet.Tests/TestFixture.cs Outdated
Comment thread tools/specgen/specgen.go
Comment thread src/Docker.DotNet/Endpoints/ISwarmOperations.cs
Comment thread src/Docker.DotNet/JsonSerializer.cs Outdated
Comment thread src/Docker.DotNet/QueryStringParameterAttribute.cs Outdated
Comment thread src/Docker.DotNet/JsonSerializer.cs
Comment thread src/Docker.DotNet/JsonSerializer.cs Outdated
@HofmeisterAn HofmeisterAn changed the title Enable IsAotCompatible feat: Enable AOT-compatibility Apr 19, 2026
@campersau
Copy link
Copy Markdown
Author

campersau commented Apr 20, 2026

The analyser is working now and also found some more missing types.
There was one Serialize(object) call here:

return new[] { JsonSerializer.Instance.Serialize(o) };

I tried to make it generic (which means I merged the converter into the attributes) and update the analyser but somehow it didn't work. The analyser didn't analyse generic attributes somehow (maybe because it is a new feature) or I did something wrong.

@HofmeisterAn
Copy link
Copy Markdown
Collaborator

The analyser is working now an also found some more missing types. There was one Serialize(object) call here:

return new[] { JsonSerializer.Instance.Serialize(o) };

I tried to make it generic (which means I merged the converter into the attributes) and update the analyser but somehow it didn't work. The analyser didn't analyse generic attributes somehow (maybe because it is a new feature) or I did something wrong.

If I got it right, you're referring to QueryStringMapParameterAttribute<T>, right? We could add an analyzer for that. LMKWYT about my recent changes. If everything looks good, we can merge the PR.

Comment thread tools/Docker.DotNet.SourceAnalyzers/Docker.DotNet.SourceAnalyzers.csproj Outdated
Comment thread src/Docker.DotNet/Models/ImagesCreateParameters.Generated.cs Outdated
Comment thread src/Docker.DotNet/QueryStringListParameterAttribute.cs
Comment thread tools/Docker.DotNet.SourceAnalyzers/JsonSerializerAnalyzer.cs
@campersau
Copy link
Copy Markdown
Author

Before After (AOT)
image image

LGTM!

@HofmeisterAn HofmeisterAn added the enhancement New feature or request label Apr 21, 2026
Copy link
Copy Markdown
Collaborator

@HofmeisterAn HofmeisterAn left a comment

Choose a reason for hiding this comment

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

👏

@HofmeisterAn HofmeisterAn merged commit dae9ea1 into testcontainers:main Apr 21, 2026
15 checks passed
@campersau campersau deleted the aot branch April 21, 2026 05:02
@bbartels
Copy link
Copy Markdown

@HofmeisterAn Could you cut a new release? Would love to try this out! Amazing work :)

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make the library compatible with Native AOT

3 participants