Daily Perf Improver: Optimize mapAsync for significant performance gains #198
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 a major optimization for
AsyncSeq.mapAsync, addressing Round 2 goals from the performance improvement plan (Issue #190). The optimization replaces the computation builder approach with a directIAsyncEnumeratorimplementation, delivering substantial performance improvements.Performance Improvements
🚀 Major performance gains achieved:
📊 Benchmark Results:
Technical Implementation
Root Cause Analysis
The original
mapAsyncimplementation used theasyncSeqcomputation builder:This approach has overhead from the computation builder machinery and enumeration wrapping.
Optimization Strategy
Created
OptimizedMapAsyncEnumerator<'T, 'TResult>that:IAsyncEnumeratorimplementationCode Changes
OptimizedMapAsyncEnumeratorclass inAsyncSeq.fs:787-812mapAsyncfunction to use optimized enumeratorAsyncSeqOp<'T>fast pathValidation
✅ All existing tests pass (175/175)
✅ Performance benchmarks show massive improvements
✅ No breaking changes - API remains identical
✅ Edge cases tested - empty sequences, exceptions, disposal, single elements
✅ Memory allocation acceptable - small increase for major performance gains
✅ Linear scaling preserved - no performance regressions
Test Plan
dotnet test -c ReleaseRelated Issues
mapAsyncis one of the most commonly used operationsCommands Used
Web Searches Performed
MCP Function Calls Used
mcp__github__search_issues: Located research issue Daily Perf Improver: Research and Plan #190 and performance prioritiesmcp__github__search_pull_requests: Verified no conflicting performance workmcp__github__get_issue: Retrieved details about Issue Unexpected iteration performance drop when recursive loops are used. #57 (O(n²) recursive patterns)mcp__github__get_issue_comments: Analyzed historical performance issue discussionsThis optimization provides significant, measurable performance improvements while maintaining full backward compatibility. The 4-8x performance improvement for
mapAsyncwill benefit all applications using this fundamental operation, directly advancing the Round 2 performance goals outlined in the research plan.🤖 Generated with Claude Code