Project: Generalized & Phonetic Automata Performance Optimization Started: November 17-18, 2025 Status: Phase 1 - COMPLETE ✅ System: Intel Xeon E5-2699 v3 @ 2.30GHz (36 cores), CPU pinned to core 0
Following the scientific method to identify and optimize performance bottlenecks in the generalized automaton and phonetic operations implementation through data-driven benchmarking, profiling, and targeted optimization.
Key Finding: Most existing benchmarks are broken and insufficient.
Results:
benches/Failures:
state_operations_benchmarks.rs: API outdated (missing query_length parameter)transition_benchmarks.rs: API changedbenchmarks.rs: Requires pathmap-backend featurecomprehensive_profiling.rs: Requires pathmap-backend featureHistorical conclusion: at the start of this investigation, no maintained generalized automaton benchmark covered the target surface, so a fresh benchmark target was created.
Documentation: docs/optimization/PHASE1_BASELINE.md
Created the generalized automaton benchmark target and identified the maintained phonetic follow-up targets:
benches/generalized_automaton_benchmarks.rs ✅ COMPLETEPurpose: Test core generalized automaton operations (H1, H2, H3)
Benchmark Groups:
Status: Baseline data captured; see docs/optimization/baseline_generalized_automaton.txt.
Purpose: Test phonetic operation compilation, NFA behavior, normalized lookup, and rewrite throughput.
Targets:
benches/phonetic_rules.rsbenches/phonetic_position_skip_benchmark.rsbenches/phonetic_nfa_benchmarks.rsbenches/phonetic_grep_parallel_benchmarks.rsbenches/phonetic_compilation_benchmarks.rsbenches/phonetic_normalized_benchmarks.rsStatus: Maintained targets are registered in Cargo.toml and used by the focused phonetic optimization journals.
Files Modified:
Benchmarks Run:
taskset -c 0 cargo bench --bench generalized_automaton_benchmarks
taskset -c 0 cargo flamegraph --bench generalized_automaton_benchmarks
Output Files:
docs/optimization/baseline_generalized_automaton.txt (complete benchmark results)docs/optimization/flamegraphs/baseline_standard.svg (369 KB flamegraph)docs/optimization/flamegraph_analysis.txt (perf report top 100)docs/optimization/flamegraph_hotspots.txt (filtered liblevenshtein code)docs/optimization/PHASE1_FLAMEGRAPH_ANALYSIS.md (comprehensive analysis)perf.data (39.2 GB, 639,127 samples)Key Benchmark Results:
| Benchmark | Time | Throughput | Notes |
|---|---|---|---|
| by_input_length/3 | 1.18 µs | 2.5 Melem/s | Baseline |
| by_distance/0 | 749 ns | 12.0 Melem/s | Rejection (0 errors) |
| by_distance/1 | 2.34 µs | 3.84 Melem/s | Accept (1 error) |
| by_distance/2 | 7.84 µs | 1.14 Melem/s | Accept (2 errors) |
| by_distance/3 | 10.90 µs | 826 Kelem/s | Accept (3 errors) |
| operations/delete | 1.02 µs | - | Fastest operation |
| operations/insert | 1.41 µs | - | 38% slower than delete |
| subsumption/max_state/1 | 494 ns | 20.2 Melem/s | Best case |
| subsumption/max_state/2 | 2.86 µs | 3.5 Melem/s | 5.8x slowdown |
| subsumption/max_state/3 | 9.50 µs | 1.05 Melem/s | 19x slowdown |
Critical Finding: Performance degrades 3.3x per distance level (749ns → 2.34µs → 7.84µs → 10.90µs)
Tool: perf + cargo-flamegraph
Samples: 639,127 CPU samples (2.09 trillion cycles)
Documentation: docs/optimization/PHASE1_FLAMEGRAPH_ANALYSIS.md
Top 5 Hotspots (Actual Code, Excluding Benchmark Overhead):
GeneralizedAutomaton::accepts (9.11%) - Main acceptance loop
GeneralizedState::successors (7.77%) - Successor generation
Memory Deallocation - cfree (4.39%) - Freeing temporary vectors
Iterator::collect (4.08%) - Collecting character vectors
Subsumption - subsumes_standard (0.71%) - Position subsumption
Combined Memory Management Overhead: 8.47% (Iterator::collect 4.08% + cfree 4.39%)
Benchmark Overhead: ~20% of cycles (Criterion's statistical analysis: KDE, exp, rayon)
Documented 6 hypotheses in PHASE1_BASELINE.md:
Priority for testing: H1 → H3 → H2 → H5 → H4 → H6
Validation Results (based on flamegraph analysis):
| Hypothesis | Status | Predicted | Actual | Verdict |
|---|---|---|---|---|
| H1: Successor generation dominates (60-80%) | ✅ Partial | 60-80% | ~10-15% | Largest component but not dominant |
| H2: char().collect() overhead (10-15%) | ✅ Confirmed | 10-15% | 8.47% | Within range (collect 4.08% + free 4.39%) |
| H3: Subsumption O(n²) bottleneck | ❌ Rejected | Major issue | 0.71% | Not a bottleneck for typical workloads |
| H4: can_apply() allocations (5-10%) | Carried forward | 5-10% | 0.79% | Lower than predicted; later phonetic journals cover targeted rule costs |
| H5: Phonetic 2-3x slowdown | Migrated | 2-3x | See focused journals | Tracked by docs/optimization/phonetic/ |
| H6: Phonetic split 20-30% slower | Migrated | 20-30% | See focused journals | Tracked by docs/optimization/phonetic/ |
Key Insights:
H1 Revision: Successor generation is the largest single component (7.77% + overhead) but doesn't "dominate" as predicted. Performance is distributed across many operations.
H2 Confirmed: Memory management (allocation + deallocation) is 8.47% of cycles - a significant cost that can be optimized.
H3 Rejected: Subsumption is very efficient (0.71%) for typical workloads. The O(n²) scaling only appears in pathological max-state scenarios (19x slowdown confirmed in benchmarks but rare in practice).
New Discovery: The bottleneck is distributed rather than concentrated:
Revised Optimization Priority:
Status: ✅ COMPLETE (November 18, 2025, 00:45 EST) Duration: ~3 hours Progress: 100% (6/6 sub-phases complete)
Major Accomplishments:
Key Discoveries:
Success Metrics:
Deliverables:
phonetic_* targetsPlanned Steps:
phonetic_rules, phonetic_nfa_benchmarks, phonetic_compilation_benchmarks, phonetic_normalized_benchmarks) for operation and acceptance coveragedocs/optimization/phonetic/Tools Ready:
cargo-flamegraph installedperf availableStatus: Superseded by focused phonetic optimization journals under docs/optimization/phonetic/.
Planned Optimizations (in order):
Methodology: For each optimization:
Status: Superseded by docs/optimization/H2_RESULTS.md, docs/optimization/H1_RESULTS.md, and later optimization result reports.
Planned:
Status: Superseded by the benchmark/result reports in this directory.
Planned Documents:
Status: Superseded by focused optimization reports and benchmark guides.
successors_i_type(): 320 lines (largest method)add_position(): Called for every successor (O(n²) potential)Immediate:
Short Term: 4. Complete phonetic operations benchmarks (Phase 2) 5. CPU profiling with perf (Phase 2.1) 6. Cache analysis (Phase 2.2) 7. Identify top 5 hotspots from flamegraphs (Phase 2.3)
Medium Term: 8. Implement first optimization (cache character vectors) 9. Benchmark and compare results 10. Iterate through optimization list
Minimum Goals:
Stretch Goals:
Phase 1: complete; baseline and flamegraph captured Phase 2: migrated to focused phonetic journals Phase 3: recorded in H1/H2 result reports Phase 4: recorded in result reports Phase 5: integrated into specialized reports
Total Estimated: 9-14 hours
Actual Progress: historical Phase 1 record; later phases are recorded in focused result reports
docs/optimization/PHASE1_BASELINE.md (comprehensive baseline)docs/optimization/PROGRESS.md (this file)docs/optimization/baseline_generalized_automaton.txtdocs/optimization/baseline_substitution_micro.txtbenches/generalized_automaton_benchmarks.rs (393 lines)Cargo.toml: phonetic_rules, phonetic_position_skip_benchmark, phonetic_nfa_benchmarks, phonetic_grep_parallel_benchmarks, phonetic_compilation_benchmarks, and phonetic_normalized_benchmarksbenches/generalized_automaton_benchmarks.rsCargo.toml (added 4 benchmark entries)docs/optimization/ createddocs/optimization/flamegraphs/ created (for future use)Phase 1 is complete. This investigation successfully:
Key Insight: The lack of working benchmarks explains why performance issues may have gone unnoticed. Our new benchmark suite provides comprehensive coverage of core operations.
Follow-up profiling and optimization decisions are recorded in the focused result reports under docs/optimization/.
Last Reconciled: June 19, 2026
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 |