Add performance optimization: skip line breaking when remaining content fits
Implement early-exit optimization to avoid expensive width calculations when we can determine that all remaining content will definitely fit on the current line.
This commit is contained in:
@@ -480,11 +480,31 @@ The following cases that previously forced line breaks now work perfectly:
|
||||
- Width calculations use Core Text (relatively fast)
|
||||
- No caching of calculated widths
|
||||
- Greedy algorithm is O(n) where n = number of atoms
|
||||
- ✅ **NEW**: Early exit optimization when remaining content fits (IMPLEMENTED!)
|
||||
|
||||
### Potential Optimizations
|
||||
### ✅ COMPLETED: Early Exit Optimization
|
||||
**Goal**: Skip expensive line breaking checks when we know all remaining content will fit.
|
||||
|
||||
**Implementation**: Lines 376, 549-606 in MTTypesetter.swift
|
||||
- Added `remainingContentFits` flag to track when optimization applies
|
||||
- In `checkAndPerformInteratomLineBreak()`:
|
||||
* After confirming current atom fits, estimates remaining content width
|
||||
* If current usage < 60% of maxWidth with ≤5 atoms remaining: sets flag (conservative)
|
||||
* If current usage < 75%: estimates remaining width via `estimateRemainingAtomsWidth()`
|
||||
* Sets flag if projected total width ≤ maxWidth
|
||||
- Once flag is set, all subsequent breaking checks return immediately (fast path)
|
||||
- Flag is reset when line break actually occurs
|
||||
- `estimateRemainingAtomsWidth()` uses character count × average char width heuristic with 1.5× safety margin
|
||||
|
||||
**Impact**: ⭐⭐⭐ GOOD performance improvement for short expressions! Avoids width calculations for atoms that definitely fit.
|
||||
|
||||
**Benefit**: Most mathematical expressions fit on one line - this optimization makes them render faster by skipping unnecessary width checks after determining the line has plenty of space.
|
||||
|
||||
**Progress**: COMPLETED and tested!
|
||||
|
||||
### Remaining Potential Optimizations
|
||||
1. **Width caching**: Cache calculated atom widths
|
||||
2. **Batch processing**: Calculate multiple atom widths together
|
||||
3. **Early exit**: Stop processing if remaining content definitely fits
|
||||
|
||||
## Conclusion
|
||||
|
||||
|
||||
Reference in New Issue
Block a user