-
Notifications
You must be signed in to change notification settings - Fork 21
perf: Add NativeAOT Support #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 19 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c6c700a
feat: enhance ValueJsonConverter for AOT compatibility with manual JS…
askpt bdb394b
feat: refactor EnumExtensions to improve AOT compatibility and remove…
askpt c41cab7
feat: add OpenFeatureJsonSerializerContext for AOT compilation support
askpt 56608c0
feat: add AOT and trimming support for net8.0 and net9.0
askpt ed39652
feat: add NativeAOT compatibility tests and project configuration
askpt 6f5390a
feat: update project structure for AOT compatibility and add MultiPro…
askpt 5c794e4
fix: remove unnecessary Type attribute project in solution file
askpt 5cd179e
feat: add unit tests for EnumExtensions.GetDescription method
askpt 1c54a7f
fix: remove trimming support properties for net8.0 and net9.0
askpt f62a88d
feat: add AOT compatibility workflow with cross-platform testing and …
askpt f8f90fc
fix: simplify AOT compatibility workflow by removing unnecessary prop…
askpt df63171
fix: update AOT compatibility workflow to include runtime in publish …
askpt ea053a3
fix: update AOT compatibility workflow to streamline ARM64 handling a…
askpt 4cc832e
fix: standardize shell usage and update publish command syntax in AOT…
askpt f462b4e
fix: update AOT size comparison report to remove AspNetCore sample co…
askpt 71c2bc4
fix: remove AOT size comparison job and artifact upload steps from wo…
askpt 2895325
fix: update AOT compatibility workflow permissions and enhance docume…
askpt 1086914
fix: streamline AOT compatibility documentation by removing redundant…
askpt f005f9e
fix: update actions/checkout and actions/cache versions in AOT compat…
askpt f36d20f
Apply suggestions from code review
askpt d93dd2d
Update .github/workflows/aot-compatibility.yml
askpt d05fa4e
fix: remove unnecessary properties from AOT project configuration
askpt 5994f99
docs: update README to clarify NativeAOT compatibility for contrib an…
askpt 14a881e
Apply suggestions from code review
askpt 92339a6
fix: add descriptions to ErrorType enum values for better clarity
askpt 67559c9
fix: remove AOT compatibility references and enhance error handling t…
askpt 2cadd2a
fix: update System.Text.Json package reference in project files
askpt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
name: AOT Compatibility | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
merge_group: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
aot-compatibility: | ||
name: AOT Test (${{ matrix.os }}, ${{ matrix.arch }}) | ||
permissions: | ||
contents: read | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
include: | ||
# Linux x64 | ||
- os: ubuntu-latest | ||
arch: x64 | ||
runtime: linux-x64 | ||
# Linux ARM64 | ||
- os: ubuntu-24.04-arm | ||
arch: arm64 | ||
runtime: linux-arm64 | ||
# Windows x64 | ||
- os: windows-latest | ||
arch: x64 | ||
runtime: win-x64 | ||
# Windows ARM64 | ||
- os: windows-11-arm | ||
arch: arm64 | ||
runtime: win-arm64 | ||
# macOS x64 | ||
- os: macos-13 | ||
arch: x64 | ||
runtime: osx-x64 | ||
# macOS ARM64 (Apple Silicon) | ||
- os: macos-latest | ||
arch: arm64 | ||
runtime: osx-arm64 | ||
|
||
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 | ||
with: | ||
fetch-depth: 0 | ||
submodules: recursive | ||
|
||
- name: Setup .NET SDK | ||
uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # v4 | ||
with: | ||
global-json-file: global.json | ||
|
||
- name: Cache NuGet packages | ||
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4 | ||
with: | ||
path: ~/.nuget/packages | ||
key: ${{ runner.os }}-${{ matrix.arch }}-nuget-${{ hashFiles('**/*.csproj') }} | ||
restore-keys: | | ||
${{ runner.os }}-${{ matrix.arch }}-nuget- | ||
${{ runner.os }}-nuget- | ||
|
||
- name: Restore dependencies | ||
shell: pwsh | ||
run: dotnet restore | ||
|
||
- name: Build solution | ||
shell: pwsh | ||
run: dotnet build -c Release --no-restore | ||
|
||
- name: Test AOT compatibility project build | ||
shell: pwsh | ||
run: dotnet build test/OpenFeature.AotCompatibility/OpenFeature.AotCompatibility.csproj -c Release --no-restore | ||
|
||
- name: Publish AOT compatibility test (cross-platform) | ||
shell: pwsh | ||
run: | | ||
dotnet publish test/OpenFeature.AotCompatibility/OpenFeature.AotCompatibility.csproj ` | ||
-c Release ` | ||
-r ${{ matrix.runtime }} ` | ||
-o ./aot-output | ||
|
||
- name: Run AOT compatibility test | ||
shell: pwsh | ||
run: | | ||
if ("${{ runner.os }}" -eq "Windows") { | ||
./aot-output/OpenFeature.AotCompatibility.exe | ||
} else { | ||
chmod +x ./aot-output/OpenFeature.AotCompatibility | ||
./aot-output/OpenFeature.AotCompatibility | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# OpenFeature .NET SDK - NativeAOT Compatibility | ||
|
||
The OpenFeature .NET SDK is compatible with .NET NativeAOT compilation, allowing you to create self-contained, native executables with faster startup times and lower memory usage. | ||
|
||
## Compatibility Status | ||
|
||
**Fully Compatible** - The SDK can be used in NativeAOT applications without any issues. | ||
|
||
### What's AOT-Compatible | ||
|
||
- Core API functionality (`Api.Instance`, `GetClient()`, flag evaluations) | ||
- All built-in providers (`NoOpProvider`, etc.) | ||
- JSON serialization of `Value`, `Structure`, and `EvaluationContext` | ||
- Error handling and enum descriptions | ||
- Hook system | ||
- Event handling | ||
- Metrics collection | ||
- Dependency injection | ||
|
||
## Using OpenFeature with NativeAOT | ||
|
||
### 1. Project Configuration | ||
|
||
To enable NativeAOT in your project, add these properties to your `.csproj` file: | ||
|
||
```xml | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> <!-- or net9.0 --> | ||
<OutputType>Exe</OutputType> | ||
|
||
<!-- Enable NativeAOT --> | ||
<PublishAot>true</PublishAot> | ||
<IsAotCompatible>true</IsAotCompatible> | ||
askpt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<InvariantGlobalization>true</InvariantGlobalization> | ||
<TrimMode>full</TrimMode> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OpenFeature" Version="2.x.x" /> | ||
</ItemGroup> | ||
</Project> | ||
``` | ||
|
||
### 2. Basic Usage | ||
|
||
```csharp | ||
using OpenFeature; | ||
using OpenFeature.Model; | ||
|
||
// Basic OpenFeature usage - fully AOT compatible | ||
var api = Api.Instance; | ||
var client = api.GetClient("my-app"); | ||
|
||
// All flag evaluation methods work | ||
var boolFlag = await client.GetBooleanValueAsync("feature-enabled", false); | ||
var stringFlag = await client.GetStringValueAsync("welcome-message", "Hello"); | ||
var intFlag = await client.GetIntegerValueAsync("max-items", 10); | ||
``` | ||
|
||
### 3. JSON Serialization (Recommended) | ||
|
||
For optimal AOT performance, use the provided `JsonSerializerContext`: | ||
|
||
```csharp | ||
using System.Text.Json; | ||
using OpenFeature.Model; | ||
using OpenFeature.Serialization; | ||
|
||
var value = new Value(Structure.Builder() | ||
.Set("name", "test") | ||
.Set("enabled", true) | ||
.Build()); | ||
|
||
// Use AOT-compatible serialization | ||
var json = JsonSerializer.Serialize(value, OpenFeatureJsonSerializerContext.Default.Value); | ||
var deserialized = JsonSerializer.Deserialize(json, OpenFeatureJsonSerializerContext.Default.Value); | ||
``` | ||
|
||
### 4. Publishing for NativeAOT | ||
|
||
Build and publish your AOT application: | ||
|
||
```bash | ||
# Build with AOT analysis | ||
dotnet build -c Release | ||
|
||
# Publish as native executable | ||
dotnet publish -c Release | ||
|
||
# Run the native executable (example path for macOS ARM64) | ||
./bin/Release/net9.0/osx-arm64/publish/MyApp | ||
``` | ||
|
||
## Performance Benefits | ||
|
||
NativeAOT compilation provides several benefits: | ||
|
||
- **Faster Startup**: Native executables start faster than JIT-compiled applications | ||
- **Lower Memory Usage**: Reduced memory footprint | ||
- **Self-Contained**: No .NET runtime dependency required | ||
- **Smaller Deployment**: Optimized for size with trimming | ||
|
||
## Testing AOT Compatibility | ||
|
||
The SDK includes an AOT compatibility test project at `test/OpenFeature.AotCompatibility/` that: | ||
|
||
- Tests all core SDK functionality | ||
- Validates JSON serialization with source generation | ||
- Verifies error handling works correctly | ||
- Can be compiled and run as a native executable | ||
|
||
Run the test: | ||
|
||
```bash | ||
cd test/OpenFeature.AotCompatibility | ||
dotnet publish -c Release | ||
./bin/Release/net9.0/[runtime]/publish/OpenFeature.AotCompatibility | ||
``` | ||
|
||
## Limitations | ||
|
||
Currently, there are no known limitations when using OpenFeature with NativeAOT. All core functionality is fully supported. | ||
|
||
## Provider Compatibility | ||
|
||
When using third-party providers, ensure they are also AOT-compatible. Check the provider's documentation for AOT support. | ||
|
||
## Troubleshooting | ||
|
||
### Trimming Warnings | ||
|
||
If you encounter trimming warnings, you can: | ||
|
||
1. Use the provided `JsonSerializerContext` for JSON operations | ||
2. Ensure your providers are AOT-compatible | ||
3. Add appropriate `[DynamicallyAccessedMembers]` attributes if needed | ||
|
||
### Build Issues | ||
|
||
- Ensure you're targeting .NET 8.0 or later | ||
- Verify all dependencies support NativeAOT | ||
- Check that `PublishAot` is set to `true` | ||
|
||
## Migration Guide | ||
|
||
If migrating from a non-AOT setup: | ||
|
||
1. **JSON Serialization**: Replace direct `JsonSerializer` calls with the provided context | ||
2. **Reflection**: The SDK no longer uses reflection, but ensure your custom code doesn't | ||
3. **Dynamic Loading**: Avoid dynamic assembly loading; register providers at compile time | ||
|
||
## Example AOT Application | ||
|
||
See the complete example in `test/OpenFeature.AotCompatibility/Program.cs` for a working AOT application that demonstrates all SDK features. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.