Add generic ClosureBox to fix thunk stack growing#95
Conversation
- Create generic ClosureBox<R> in withTracking.swift to eliminate code duplication - Fix thunk stack growing for didChange closure in withContinuousStateGraphTracking by wrapping closures once at entry point and passing them directly in recursive calls - Remove duplicate private ClosureBox definitions from withGraphTrackingGroup.swift and withGraphTrackingMap.swift - Add callAsFunction support for cleaner invocation syntax The previous implementation wrapped/unwrapped closures via UnsafeSendable on each recursive iteration, causing thunk stack growth. Now closures are wrapped in ClosureBox once and passed through all iterations. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Claude finished @muukii's task —— View job Code ReviewI've analyzed the PR and found it to be a well-executed refactoring that addresses a specific performance issue. Here's my detailed feedback: ✅ Strengths1. Generic ClosureBox Design (
2. Thunk Stack Growing Fix (
3. Code Duplication Removal
🎯 Architecture ReviewThread Safety: ✅
Memory Management: ✅
API Compatibility: ✅
🔍 Technical Deep DiveBefore (Issue): // Each recursive call re-wrapped closures via UnsafeSendable
withContinuousStateGraphTracking(apply: apply, didChange: didChange)
-> withContinuousStateGraphTracking(apply: UnsafeSendable(apply)._value, ...) // Stack growth!After (Solution): // Wrap once, pass through recursion
let applyBox = ClosureBox(apply) // Single wrapping
let didChangeBox = ClosureBox(didChange)
_withContinuousStateGraphTracking(apply: applyBox, didChange: didChangeBox) // Direct passing🐛 Potential Issues & RecommendationsMinor: Documentation
Performance Validation
🧪 Test Coverage AssessmentCurrent State: ✅ 99 tests passing
Recommendation: Consider adding a specific test case that exercises deep recursion in continuous tracking to validate the stack growth fix (though this may be challenging to test reliably). 🔒 Security ReviewNo security concerns identified:
📊 Performance ConsiderationsPositive Impact:
No Negative Impact:
🎉 Final AssessmentThis is a high-quality refactoring that:
Recommendation: Approve and merge The implementation is solid, addresses the stated problem effectively, and improves the codebase without introducing new risks. Todo List
|
|
@JohnEstropia TIL |
Summary
ClosureBox<R>inwithTracking.swiftto eliminate code duplicationdidChangeclosure inwithContinuousStateGraphTrackingby wrapping closures once at entry point and passing them directly in recursive callsClosureBoxdefinitions fromwithGraphTrackingGroup.swiftandwithGraphTrackingMap.swiftcallAsFunctionsupport for cleaner invocation syntaxBackground
PR #88 fixed thunk stack growing issue for handler closures by wrapping them in
ClosureBox. However:ClosureBoxwas duplicated in two filesdidChangeclosure inwithContinuousStateGraphTrackingstill had the same thunk stack growing issue due to recursive wrapping/unwrapping viaUnsafeSendableChanges
withTracking.swiftClosureBox<R>withcallAsFunction, add private_withContinuousStateGraphTrackingwithGraphTrackingGroup.swiftClosureBox, use shared onewithGraphTrackingMap.swiftClosureBox, use shared oneTest plan
swift buildpassesswift testpasses (99 tests)🤖 Generated with Claude Code