Date: 2025-11-18 Hypothesis Tested: H3 - Cache inefficiency causes performance degradation for large inputs
Used perf stat to measure cache behavior across all input sizes without generating flamegraphs.
Measurement Tool: Linux perf with hardware performance counters Events Measured:
cycles:u - CPU cycles (user-space)instructions:u - Instructions executedL1-dcache-loads - L1 data cache load operationsL1-dcache-load-misses - L1 data cache missesLLC-loads - Last Level Cache (L3) load operationsLLC-load-misses - LLC missesCommand:
perf stat -e cycles:u,instructions:u,L1-dcache-loads,L1-dcache-load-misses,LLC-loads,LLC-load-misses \
cargo bench --bench phonetic_rules --features phonetic-rules \
-- "throughput_by_input_size" --output-format bencher
Benchmark Performance: | Input Size | Time (ns/iter) | Std Dev | |------------|----------------|---------| | 5 phones | 776 | ±24 | | 10 phones | 1,718 | ±59 | | 20 phones | 5,708 | ±154 | | 50 phones | 29,045 | ±933 |
Performance Counters (across all benchmarks):
250,665,992,879 cycles:u
471,956,168,926 instructions:u # 1.88 insn per cycle
121,257,801,299 L1-dcache-loads:u
1,379,212,690 L1-dcache-load-misses:u # 1.14% of all L1-dcache accesses
31,358,391,442 L1-dcache-stores:u
217,617,168 LLC-loads:u
7,951,127 LLC-load-misses:u # 3.65% of all LL-cache accesses
Key Metrics:
Benchmark: 31,589 ns/iter (±910)
Performance Counters:
64,071,686,699 cycles:u
124,125,575,615 instructions:u # 1.94 insn per cycle
31,752,156,374 L1-dcache-loads:u
379,784,059 L1-dcache-load-misses:u # 1.20% of all L1-dcache accesses
58,996,100 LLC-loads:u
2,911,619 LLC-load-misses:u # 4.94% of all LL-cache accesses
Key Metrics:
| Metric | Target (Good) | Measured | Status |
|---|---|---|---|
| L1 D-cache miss rate | <2% | 1.14-1.20% | ✅ Excellent |
| LLC miss rate | <10% | 3.65-4.94% | ✅ Excellent |
| IPC | 1.5-2.5 | 1.88-1.94 | ✅ Excellent |
Reference Baseline (from docs/optimization/substitution-set/04-h1-profiling-results.md):
| Metric | Phonetic Rules | Substitution Set Reference | Comparison |
|---|---|---|---|
| L1 miss rate | 1.14-1.20% | 1.8% | 33-37% better |
| LLC miss rate | 3.65-4.94% | 8.00% | 38-54% better |
| IPC | 1.88-1.94 | 1.91 | ~equivalent |
Conclusion: Phonetic rules cache performance is better than the reference baseline from previous optimization work!
Evidence Against:
Cache Behavior Across Input Sizes:
1. Memory Access Pattern is Cache-Friendly
The code exhibits excellent locality:
&s[..pos], &s[(pos + pattern.len())..] access contiguous memory2. Vec Operations are Optimized
The extend_from_slice() operations benefit from:
3. CPU is Compute-Bound, Not Memory-Bound
IPC of 1.88-1.94 indicates:
Since cache is NOT the bottleneck, the remaining source of O(n^1.5) complexity is:
Algorithmic Work = iterations × rules × n
Where:
Total Complexity: O(√n) × 8 × n = O(n^1.5)
This is fundamental algorithmic complexity, not a cache issue.
Cache is Healthy ✅:
Focus on Algorithmic Improvements ⚡:
Do NOT pursue:
| Optimization | Overhead | Status |
|---|---|---|
| H1 (Allocation) | 27% of total time | ✅ Fixed in v0.8.0 |
| H3 (Cache) | < 2% (L1 misses) | ✅ Already optimal |
Conclusion: Allocation elimination (H1) was 13.5× more impactful than cache would be!
CPU: Intel Xeon E5-2699 v3 @ 2.30GHz Cache Hierarchy:
Measured Latencies (from cache miss rates):
Effective Memory Latency:
Avg latency = 0.988 × 4 + 0.012 × 0.95 × 42 + 0.012 × 0.05 × 200
= 3.95 + 0.48 + 0.12
= 4.55 cycles average
Cycles per load = 4.55 cycles
Time per load @ 2.3 GHz = 4.55 / 2.3e9 = 1.98 ns
Pattern Matching Time: 23-27 ns (from microbenchmarks) Memory Access Component: ~10-15% of pattern matching time
Conclusion: Even if cache were perfect (0% misses), improvement would be < 2%
Evidence:
Impact on Investigation:
Documentation Status: ✅ COMPLETE Optimization Needed: ❌ NOT REQUIRED (already optimal)
Measurement Data:
/tmp/h3_cache_all.txt - All input sizes combined/tmp/h3_detailed_cache.txt - Detailed cache eventsBaseline Comparison:
docs/optimization/substitution-set/04-h1-profiling-results.md - Reference cache metricsRelated Analysis:
docs/optimization/phonetic/04-iteration-analysis.md - H5 (algorithmic complexity)docs/optimization/phonetic/03-optimization-results.md - H1 (27% allocation overhead)Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |