Daily Perf Improver - Optimize Vector.sum with hardware-accelerated horizontal reduction #83
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 optimizes the
Vector.sumoperation achieving 15-47% speedup for typical vector sizes by replacing the generic fold-based implementation with a specialized SIMD implementation that usesVector.Sumfor hardware-accelerated horizontal reduction.Performance Goal
Goal Selected: Optimize reduction operations (
sum,product,min,max) - Phase 2/3 continuationRationale: While analyzing the codebase, I noticed that the
foldfunction inSpanPrimitives.fsuses a scalar loop for horizontal reduction (lines 510-513), whereas the optimizeddotUnchecked(from previous work) andfold2functions useVector.Sumfor efficient horizontal reduction. Thesumoperation is fundamental and used frequently, so optimizing it provides cascading benefits.Changes Made
Core Optimization
Files Modified:
src/FsMath/SpanMath.fs-sumfunction (lines 252-285)benchmarks/FsMath.Benchmarks/Vector.fs- Added benchmarks forSum,Product,Min,MaxOriginal Implementation:
The generic
foldused a scalar loop for horizontal reduction:Optimized Implementation:
Approach
folduses scalar horizontal reductiondotUncheckedandfold2useVector.Sumfor efficient horizontal reductionSum,Product,Min,Maxoperationsfolddirectly, but discovered this breaksmin/maxoperationssumfunction withVector.Sumhorizontal reductionPerformance Measurements
Test Environment
Results Summary
Detailed Benchmark Results
Before (Baseline):
After (Optimized):
Key Observations
Why This Works
The optimization addresses a key bottleneck in reduction operations:
Hardware-Accelerated Horizontal Reduction:
Vector.Sumcall using hardware-specific SIMD instructions (HADDPS on AVX2)Optimized for Small/Medium Vectors:
Consistent with Existing Optimizations:
dotUncheckedalready usesVector.Sumfor horizontal reductionfold2(used by dot product) already usesVector.Sumsumin line with other optimized operationsWhy Not Modify
foldDirectly:foldto useVector.Sumunconditionallyminandmaxoperations which useVector.Min/Vector.MaxVector.Sumis only appropriate for additive reductionsReplicating the Performance Measurements
To replicate these benchmarks:
Results are saved to
BenchmarkDotNet.Artifacts/results/in multiple formats.Testing
✅ All 1486 tests pass (8 skipped)
✅ Sum benchmarks execute successfully
✅ Memory allocations unchanged (zero for all sizes)
✅ Performance improves 15-47% for small/medium vectors
✅ Correctness verified across all test cases
✅ Build completes with only pre-existing warnings
Implementation Details
Optimization Techniques Applied
Vector.Suminstead of scalar loopsumvs genericfoldCode Quality
Limitations and Future Work
While this optimization provides solid improvements, there are additional opportunities:
Vector.productsummin,maxcould potentially use similar techniques with appropriate horizontal reduction functionsNext Steps
Based on the performance plan from Discussion #4, remaining optimization work includes:
Related Issues/Discussions
Bash Commands Used
Web Searches Performed
None - this optimization was based on:
dotUnchecked,fold2)Vector.Sum)🤖 Generated with Claude Code