Create benchmark for [[likely]] performance #122
+130
−0
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.
🎯 Summary
This PR implements comprehensive benchmarks to evaluate the performance impact of different optimization strategies in C# that simulate C++
[[likely]]and[[unlikely]]branch prediction attributes.📋 Issue Reference
Fixes #96
🔍 Background
Issue #96 requested benchmarks comparing performance of code with and without C++
[[likely]]/[[unlikely]]attributes. Since this is a C# codebase, I've implemented equivalent optimization strategies available in C#/.NET.💡 Implementation Details
C# Equivalents to [[likely]]/[[unlikely]]
Added six new benchmark scenarios to test different optimization approaches:
AggressiveInlining: Uses
[MethodImpl(MethodImplOptions.AggressiveInlining)]to force method inlining for hot pathsinlinebehavior combined with branch prediction hintsAggressiveOptimization: Uses
[MethodImpl(MethodImplOptions.AggressiveOptimization)]to enable aggressive optimizationsBothOptimizations: Combines both
AggressiveInliningandAggressiveOptimizationDoesNotReturn + NoInlining: Separates exception throwing into a helper method marked with:
[DoesNotReturn]- Helps optimizer understand this path never returns[MethodImpl(MethodImplOptions.NoInlining)]- Keeps exception path out of hot path[[unlikely]]by isolating cold pathInlineException: Exception handling within the same method with optimizations
UnlikelyFirst: Anti-pattern with exception check before happy path
GenericOptimized: Generic version using modern .NET generic math (
IUnsignedNumber<T>)Key Insights
The benchmarks test the Factorial function with different optimization strategies, focusing on:
Test Scenario
All benchmarks use
FactorialNumber = 19(within valid range) to ensure we're measuring the hot path performance rather than exception handling.🧪 Running the Benchmarks
cd csharp/Platform.Numbers.Benchmarks dotnet run -c Release📊 Expected Results
The benchmarks will show:
✅ Changes Made
🔗 Related Work
[[likely]]/[[unlikely]]attributes🤖 Generated with Claude Code