|
| 1 | +# Phase 3: Exception Safety Implementation |
| 2 | + |
| 3 | +**Status**: 🔄 IN PROGRESS |
| 4 | +**Priority**: HIGH |
| 5 | +**Estimated Time**: 2-3 days |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Exception Safety Guarantees |
| 10 | + |
| 11 | +### Definitions |
| 12 | + |
| 13 | +1. **No-throw guarantee** (`noexcept`): Operation never throws exceptions |
| 14 | +2. **Strong guarantee**: Operation either succeeds completely or has no effect |
| 15 | +3. **Basic guarantee**: Operation may modify state but leaves object in valid state |
| 16 | +4. **No guarantee**: Operation may leave object in invalid state |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Current State Analysis |
| 21 | + |
| 22 | +### Functions That Should Be `noexcept` |
| 23 | + |
| 24 | +1. **Destructors**: All destructors (already noexcept by default in C++11+) |
| 25 | +2. **Move operations**: Move constructors and move assignment |
| 26 | +3. **Swap operations**: `swap()` functions |
| 27 | +4. **Accessors**: `size()`, `empty()`, etc. |
| 28 | + |
| 29 | +### Functions Requiring Strong Guarantee |
| 30 | + |
| 31 | +1. **Constructors**: Should either fully construct or throw without side effects |
| 32 | +2. **Insert operations**: Should roll back on failure |
| 33 | +3. **Query operations**: Should never modify state |
| 34 | + |
| 35 | +### Current Issues |
| 36 | + |
| 37 | +1. **Constructor exception safety**: Partially addressed in Phase 1 (RAII) |
| 38 | +2. **Insert operation**: May partially modify state before throwing |
| 39 | +3. **Missing noexcept specifications**: Many functions could be marked noexcept |
| 40 | +4. **No rollback mechanism**: Operations don't roll back on failure |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Implementation Plan |
| 45 | + |
| 46 | +### Step 1: Add `noexcept` Specifications |
| 47 | + |
| 48 | +Mark appropriate functions as `noexcept`: |
| 49 | +- Move constructors |
| 50 | +- Move assignment operators |
| 51 | +- Destructors (explicit) |
| 52 | +- Size/empty queries |
| 53 | +- Comparison operations |
| 54 | + |
| 55 | +### Step 2: Improve Insert Operation |
| 56 | + |
| 57 | +Ensure `insert()` provides strong exception guarantee: |
| 58 | +- Build new tree node before modifying state |
| 59 | +- Only modify state if all operations succeed |
| 60 | +- Use scope guards for rollback |
| 61 | + |
| 62 | +### Step 3: Add Exception Safety Documentation |
| 63 | + |
| 64 | +Document exception safety guarantee for each public method. |
| 65 | + |
| 66 | +### Step 4: Add Exception Safety Tests |
| 67 | + |
| 68 | +Write tests that trigger exceptions and verify state consistency. |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## Implementation Details |
| 73 | + |
| 74 | +### Example: Adding noexcept |
| 75 | + |
| 76 | +```cpp |
| 77 | +// Query operations that don't modify state |
| 78 | +size_t size() const noexcept { |
| 79 | + std::lock_guard<std::mutex> lock(tree_mutex_); |
| 80 | + return idx2bb.size(); |
| 81 | +} |
| 82 | + |
| 83 | +bool empty() const noexcept { |
| 84 | + std::lock_guard<std::mutex> lock(tree_mutex_); |
| 85 | + return idx2bb.empty(); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Example: Strong Exception Guarantee |
| 90 | + |
| 91 | +```cpp |
| 92 | +void insert(const T &idx, const py::array_t<float> &x, |
| 93 | + const std::optional<std::string> objdumps = std::nullopt) { |
| 94 | + std::lock_guard<std::mutex> lock(tree_mutex_); |
| 95 | + |
| 96 | + // Check preconditions (doesn't modify state) |
| 97 | + auto it = idx2bb.find(idx); |
| 98 | + if (unlikely(it != idx2bb.end())) { |
| 99 | + throw std::runtime_error("Given index is already included."); |
| 100 | + } |
| 101 | + |
| 102 | + // Build BB (doesn't modify state) |
| 103 | + BB<D> bb = build_bb_from_array(x); |
| 104 | + |
| 105 | + // All operations that might throw are done |
| 106 | + // Now modify state (these are all noexcept or provide strong guarantee) |
| 107 | + idx2bb.emplace(idx, bb); |
| 108 | + set_obj(idx, objdumps); |
| 109 | + |
| 110 | + // Tree modification... |
| 111 | +} |
| 112 | +``` |
| 113 | +
|
| 114 | +--- |
| 115 | +
|
| 116 | +## Testing Strategy |
| 117 | +
|
| 118 | +1. **Exception injection tests**: Inject exceptions at various points |
| 119 | +2. **State validation**: Verify object state after exception |
| 120 | +3. **Memory leak tests**: Run under ASan with exceptions |
| 121 | +4. **Rollback tests**: Verify partial operations are rolled back |
| 122 | +
|
| 123 | +--- |
| 124 | +
|
| 125 | +## Success Criteria |
| 126 | +
|
| 127 | +- ✅ All appropriate functions marked `noexcept` |
| 128 | +- ✅ All public operations provide at least basic exception guarantee |
| 129 | +- ✅ Insert/erase provide strong exception guarantee |
| 130 | +- ✅ All exception paths tested |
| 131 | +- ✅ Documentation complete |
0 commit comments