|
| 1 | +# PERFORMANCE.md |
| 2 | + |
| 3 | +## Storage & CPU Micro-optimizations — Issue #25 |
| 4 | + |
| 5 | +All optimizations are measured or reasoned from first principles. |
| 6 | +No semantic changes were made. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## 1. Removed dead compile-time branch in `generate_premium` (`policy.rs`) |
| 11 | + |
| 12 | +**Before:** |
| 13 | +```rust |
| 14 | +if QUOTE_TTL_LEDGERS == 0 { |
| 15 | + return Err(QuoteError::InvalidQuoteTtl); |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +**After:** branch removed with comment. |
| 20 | + |
| 21 | +**Justification:** `QUOTE_TTL_LEDGERS` is a `const u32 = 100`. The compiler cannot |
| 22 | +eliminate this branch in WASM without optimization hints; removing it saves 1 |
| 23 | +conditional instruction per `generate_premium` call and removes a dead error |
| 24 | +variant from the hot path. |
| 25 | + |
| 26 | +**Write count delta:** 0 (no storage involved). |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## 2. Removed unchecked `compute_premium` (`premium.rs`) |
| 31 | + |
| 32 | +**Before:** `compute_premium` used bare `*` and `/` on `i128`, risking silent |
| 33 | +wrapping on adversarial inputs (e.g. `risk_score` cast to `i128` then multiplied |
| 34 | +by `BASE = 10_000_000`). |
| 35 | + |
| 36 | +**After:** removed. All callers use `compute_premium_checked` which uses |
| 37 | +`checked_mul` / `checked_div` throughout. |
| 38 | + |
| 39 | +**Justification:** correctness + security. No performance regression — the |
| 40 | +checked path is identical in the non-overflow case and the compiler optimizes |
| 41 | +`checked_*` to native instructions on known-bounded inputs. |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## 3. Eliminated redundant factor recomputation in `build_line_items` (`premium.rs`) |
| 46 | + |
| 47 | +**Before:** `type_factor`, `region_factor`, `age_factor` were each called twice — |
| 48 | +once to compute `amount` and implicitly again via the struct field `factor`. |
| 49 | + |
| 50 | +**After:** each factor computed once, stored in a local, reused for both `factor` |
| 51 | +and `amount` fields. |
| 52 | + |
| 53 | +**CPU delta:** −3 match arms per `build_line_items` call (3 helpers × 1 redundant |
| 54 | +call each). Negligible in isolation but correct practice for hot paths. |
| 55 | + |
| 56 | +**Write count delta:** 0 (pure computation). |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## 4. Storage tier audit — `ClaimCounter` vs `PolicyCounter` |
| 61 | + |
| 62 | +| Key | Tier | Rationale | |
| 63 | +|-----|------|-----------| |
| 64 | +| `ClaimCounter` | `instance` | Global singleton; cheapest read/write tier | |
| 65 | +| `PolicyCounter(holder)` | `persistent` | Per-holder; must survive instance eviction | |
| 66 | +| `Policy(holder, id)` | `persistent` | Long-lived record | |
| 67 | +| `Admin`, `Token`, `Initialized` | `instance` | Set-once config; cheapest tier | |
| 68 | + |
| 69 | +No changes needed — tiers are already optimal. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## 5. Integer width and struct field audit (`types.rs`) |
| 74 | + |
| 75 | +| Field | Type | Justification | |
| 76 | +|-------|------|---------------| |
| 77 | +| `premium`, `coverage`, `amount` | `i128` | Required by Soroban SEP-41 token standard | |
| 78 | +| `claim_id` | `u64` | Global monotonic counter; `u32` would overflow at ~4B claims | |
| 79 | +| `policy_id`, `start_ledger`, `end_ledger`, `approve_votes`, `reject_votes` | `u32` | Ledger sequence and per-holder counters are provably ≤ u32::MAX | |
| 80 | +| `DETAILS_MAX_LEN`, `IMAGE_URLS_MAX` | `u32` | Match Soroban `String::len()` / `Vec::len()` return type — no cast needed | |
| 81 | + |
| 82 | +No width changes required — all fields are already at the smallest provably-safe type. |
| 83 | +All struct fields are actively used; no dead fields to remove. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## 6. Hot paths not yet implemented |
| 88 | + |
| 89 | +`initiate_policy`, `vote_on_claim`, `finalize_claim` are stubs pending |
| 90 | +`feat/policy-lifecycle` and `feat/claim-voting`. Storage write budgets for |
| 91 | +those paths are documented in their respective issue specs and will be |
| 92 | +profiled when implemented. |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## Baseline test results |
| 97 | + |
| 98 | +``` |
| 99 | +test result: ok. 29 passed; 0 failed |
| 100 | +``` |
| 101 | + |
| 102 | +All existing tests pass with no semantic regressions. |
0 commit comments