feat: iterative Fibonacci implementation in Plinth (2,511x faster)#144
feat: iterative Fibonacci implementation in Plinth (2,511x faster)#144
Conversation
## Implementation Created iterative Fibonacci submission achieving O(n) time complexity using accumulator-based approach with tail recursion: - New module: lib/FibonacciIterative.hs - Uses helper function fibonacciLoop with accumulators (prev, curr) - Avoids exponential recalculation of values ## Performance Results Achieved 2,511x overall performance improvement: - fibonacci(25): 23.4M CPU (vs 194.2B naive - 8,299x faster) - Total CPU: 85M units (vs 213.5B naive) - Total Memory: 358K units (vs 882.2M naive - 2,464x better) - Script size: 73 bytes ## Submission Structure submissions/fibonacci/Plinth_1.45.0.0_Unisay/ ├── fibonacci.uplc (compiled UPLC program) ├── metadata.json (compiler metadata) ├── metrics.json (performance measurements) ├── README.md (implementation notes) └── source/FibonacciIterative.hs (source code) ## Testing - All 11 test cases pass (fibonacci 0-25, negative) - Schema validation passed (metadata.json, metrics.json) - cape test: 53/53 passed - cabal test: 222/222 passed Resolves #92
🚀 PR Preview DeployedPreview URL: https://intersectmbo.github.io/UPLC-CAPE/pr-144/ The preview site is automatically updated on every push to this PR and will be removed when the PR is closed. |
## Experiment Summary Tested 5 source-level optimization variants to reduce UPLC execution budget: - V2: Countdown + equality check - V3: Case expressions instead of guards - V4: Compare to zero - V5: Combined (countdown + case) - V6: Explicit strictness (bang patterns) ## Results | Variant | Total CPU | vs Original | Script Size | Verdict | |---------|-----------|-------------|-------------|---------| | Original | 85.09M | baseline | 73 bytes | ✅ BEST | | V2 | 85.09M | +0.004% | 117 bytes | ❌ | | V3 | 85.09M | 0% | 73 bytes |⚠️ No change | | V4 | 84.53M | -0.65% | 117 bytes | ❌ Script bloat | | V5 | 88.01M | +3.4% | 135 bytes | ❌ WORSE | | V6 | 92.00M | +8.1% | 122 bytes | ❌ MUCH WORSE | ## Key Findings - **Case expressions = Guards**: PlutusTx compiles both to identical UPLC - **Strictness hurts**: Bang patterns add 8% CPU overhead - **Countdown adds bloat**: Extra edge case guards increase script size 60% - **Minimal CPU gains**: Best improvement (V4) only 0.65% but 60% larger script - **Original is optimal**: No source-level optimization provides meaningful benefit ## Conclusion Original FibonacciIterative.hs implementation is already optimal for PlutusTx. Further improvements require compiler-level changes (Y-combinator elimination, native case support), not source code modifications. ## Cleanup Removed all experimental variant files and reverted build configuration.
Optimization Experiment ResultsTested 5 source-level optimizations to further reduce execution budget: Tested Variants
Performance Comparison
Key Findings
ConclusionThe original implementation is already optimal for PlutusTx. No source-level changes were applied. To close the performance gap with Aiken (67.8M CPU) would require compiler-level improvements:
These are outside the scope of source code changes and would require enhancements to the PlutusTx compiler itself. Bottom line: The 2,511x improvement over naive recursion is already excellent. The remaining 20% gap with Aiken reflects the trade-off between PlutusTx's general-purpose compiler and Aiken's UPLC-specialized compiler. |
Renamed submissions/fibonacci/Plinth_1.45.0.0_Unisay to submissions/fibonacci/Plinth_1.45.0.0_Unisay_tailrec to: - Match naming convention of Aiken_tailrec submission - Clearly indicate tail-recursive/iterative implementation - Distinguish from naive recursive baseline Updated plinth-submissions-app to generate to new path.
Summary
Creates an iterative Fibonacci submission in Plinth that achieves 2,511x overall performance improvement over naive recursion, using an O(n) accumulator-based approach.
Implementation Details
Algorithm
fibonacciLoopmaintains(prev, curr)accumulatorsCode Structure
lib/FibonacciIterative.hscape.cabalandplinth-submissions-app/Main.hssubmissions/fibonacci/Plinth_1.45.0.0_Unisay/Performance Results
Key Metrics
Comparison with Other Compilers
Testing
cape submission verifypassedFiles Changed
Acceptance Criteria
All criteria from issue #92 met:
cape submission verifypassescape submission reportto generate updated chartsResolves #92