god-graph 0.6.0-alpha

A graph-based LLM white-box optimization toolbox: topology validation, Lie group orthogonalization, tensor ring compression
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# God-Graph 性能调优指南

本指南提供 god-graph 分布式图处理库的性能优化建议和最佳实践。

## 目录

- [内存优化]#内存优化
- [并行计算优化]#并行计算优化
- [算法特定优化]#算法特定优化
- [分布式系统优化]#分布式系统优化
- [基准测试与性能分析]#基准测试与性能分析

---

## 内存优化

### 1. 使用合适的图表示

```rust
// 对于稀疏图,使用 adjacency_list 更节省内存
let graph = Graph::new(GraphType::Directed);

// 对于密集图 (边数 > 顶点数²/2),考虑使用邻接矩阵
// 但 god-graph 主要优化稀疏图场景
```

**建议**:
- 稀疏图(边/顶点 < 4):使用默认邻接表
- 中等密度(4 ≤ 边/顶点 < 10):考虑压缩稀疏行 (CSR) 格式
- 密集图(边/顶点 ≥ 10):评估内存使用,可能需要分片处理

### 2. 批量添加边

```rust
// ❌ 低效:逐个添加边
for edge in edges {
    graph.add_edge(edge.0, edge.1, edge.2);
}

// ✅ 高效:批量添加
graph.add_edges_batch(edges);
```

**性能提升**:批量操作减少内存重新分配,提升 30-50% 性能。

### 3. 预分配顶点容量

```rust
// 如果知道顶点数量,预分配容量
let mut graph = Graph::new(GraphType::Undirected);
graph.reserve_vertices(100_000);
```

### 4. 使用轻量级权重类型

```rust
// ✅ 推荐:使用 f32 或 i32
let graph: Graph<f32> = Graph::new(GraphType::Directed);

// ⚠️ 谨慎:复杂权重增加内存占用
let graph: Graph<ComplexWeight> = Graph::new(GraphType::Directed);
```

---

## 并行计算优化

### 1. 调整 Rayon 线程池

```rust
use rayon::ThreadPoolBuilder;

// 根据工作负载调整线程数
ThreadPoolBuilder::new()
    .num_threads(num_cpus::get())
    .build_global()
    .unwrap();
```

**建议**:
- CPU 密集型任务:使用物理核心数
- I/O 密集型任务:使用 2× 物理核心数
- 混合负载:使用 `num_cpus::get() * 2`

### 2. 选择合适的并行度

```rust
use god_graph::distributed::DistributedConfig;

// 小图 (< 10K 顶点):减少并行开销
let config = DistributedConfig {
    num_partitions: 4,
    ..Default::default()
};

// 大图 (> 1M 顶点):增加并行度
let config = DistributedConfig {
    num_partitions: 64,
    ..Default::default()
};
```

### 3. PageRank 并行优化

```rust
use god_graph::distributed::algorithms::pagerank::PageRankConfig;

// 调整收敛阈值和最大迭代次数
let config = PageRankConfig {
    damping_factor: 0.85,
    max_iterations: 20,      // 减少迭代次数
    convergence_threshold: 1e-4, // 放宽收敛条件
    use_parallel: true,
};

let result = pagerank::compute(&graph, Some(config));
```

**性能提示**:
- 放宽收敛阈值可显著减少迭代次数
- 对于近似计算,`max_iterations=10` 通常足够

### 4. BFS 层级并行

```rust
use god_graph::distributed::algorithms::bfs::BfsConfig;

let config = BfsConfig {
    parallel_level_processing: true, // 启用层级并行
    max_parallelism: 8,              // 限制最大并行度
};
```

---

## 算法特定优化

### DFS 优化

```rust
use god_graph::distributed::algorithms::dfs::{DfsConfig, DfsMode};

// 对于深度优先遍历,使用迭代模式避免栈溢出
let config = DfsConfig {
    mode: DfsMode::Iterative,
    max_stack_size: 10_000,
};

// 对于 SCC 计算,使用 Tarjan 算法
let scc_result = tarjan_scc(&graph);
```

**建议**:
- 深度图:使用迭代模式
- 需要调用栈信息:使用递归模式
- SCC 计算:Tarjan 算法最优

### Connected Components 优化

```rust
use god_graph::distributed::algorithms::connected_components::{CCConfig, CCAlgorithm};

// 大图使用并查集算法
let config = CCConfig {
    algorithm: CCAlgorithm::UnionFind,
    use_path_compression: true,
    use_union_by_rank: true,
};

// 动态连通性查询
let mut cc = ConnectedComponents::new(&graph, config);
cc.compute();

// 快速查询
if cc.in_same_component(u, v) {
    println!("u and v are connected");
}
```

**性能对比**:
| 算法 | 时间复杂度 | 适用场景 |
|------|-----------|---------|
| UnionFind | O(E α(V)) | 静态图 |
| LabelPropagation | O(E log V) | 动态图 |
| BFS-based | O(V + E) | 小图 |

### Dijkstra 优化

```rust
use god_graph::distributed::algorithms::dijkstra::{DijkstraConfig, DijkstraAlgorithm};

// 单源最短路径
let config = DijkstraConfig {
    algorithm: DijkstraAlgorithm::Standard,
    use_bidirectional: false,
};

let result = dijkstra::single_source(&graph, source, Some(config));

// 单对最短路径使用双向搜索
let config = DijkstraConfig {
    algorithm: DijkstraAlgorithm::Bidirectional,
    ..Default::default()
};

let path = dijkstra::single_pair(&graph, source, target, Some(config));
```

**优化技巧**:
- 单对查询:使用双向 Dijkstra,减少 50% 搜索空间
- 多源查询:使用 `multi_source_dijkstra`
- 负权重:考虑使用 Bellman-Ford(未实现)

### PageRank 优化

```rust
// 使用稀疏矩阵优化
let config = PageRankConfig {
    use_sparse_matrix: true,
    compression_threshold: 1e-6, // 压缩小值
    ..Default::default()
};
```

---

## 分布式系统优化

### 1. 分区策略选择

```rust
use god_graph::distributed::{PartitionStrategy, DistributedConfig};

// 顶点切割:适合高顶点度图
let config = DistributedConfig {
    partition_strategy: PartitionStrategy::VertexCut,
    num_partitions: 16,
};

// 边切割:适合高边密度图
let config = DistributedConfig {
    partition_strategy: PartitionStrategy::EdgeCut,
    num_partitions: 16,
};
```

**选择指南**:
| 图特征 | 推荐策略 | 理由 |
|--------|---------|------|
| 高顶点度 | VertexCut | 减少边复制 |
| 高边密度 | EdgeCut | 平衡负载 |
| 社交网络 | VertexCut | 处理幂律分布 |
| 路网图 | EdgeCut | 度数分布均匀 |

### 2. 消息传递优化

```rust
use god_graph::distributed::MessageConfig;

// 批量消息传递
let msg_config = MessageConfig {
    batch_size: 1024,           // 批量大小
    aggregation_threshold: 100, // 聚合阈值
    use_compression: true,      // 启用压缩
};
```

### 3. 容错性能权衡

```rust
use god_graph::distributed::fault_tolerance::{RetryPolicy, CircuitBreakerConfig};

// 调整重试策略
let retry_policy = RetryPolicy::exponential_with_jitter(
    Duration::from_millis(100),  // 初始延迟
    Duration::from_secs(5),      // 最大延迟
    3,                            // 最大重试次数
);

// 配置熔断器
let circuit_breaker = CircuitBreaker::new(CircuitBreakerConfig {
    failure_threshold: 5,
    recovery_timeout: Duration::from_secs(30),
    half_open_max_calls: 3,
});
```

---

## 基准测试与性能分析

### 运行基准测试

```bash
# 运行所有分布式基准测试
cargo bench --bench distributed

# 运行特定算法基准测试
cargo bench --bench distributed -- --filter pagerank

# 生成性能报告
cargo bench --bench distributed -- --output-format bencher | tee results.txt
```

### 使用 perf 进行性能分析

```bash
# 安装 perf
sudo apt install linux-tools-common linux-tools-generic

# 生成火焰图
cargo flamegraph --bench distributed

# 或使用 perf
perf record --call-graph dwarf cargo bench --bench distributed
perf script | stackcollapse-perf.pl | flamegraph.pl > flame.svg
```

### 内存分析

```bash
# 使用 valgrind
valgrind --tool=massif cargo bench --bench distributed

# 查看内存使用
ms_print massif.out.<pid>
```

### 性能监控指标

```rust
use god_graph::distributed::metrics::PerformanceMetrics;

let metrics = PerformanceMetrics::new();

// 关键指标
println!("处理时间:{:?}", metrics.processing_time());
println!("内存使用:{} MB", metrics.memory_usage_mb());
println!("吞吐量:{} 顶点/秒", metrics.throughput());
println!("并行效率:{:.2}%", metrics.parallel_efficiency());
```

---

## 最佳实践总结

### ✅ 推荐做法

1. **预分配**:知道大小时预分配容量
2. **批量操作**:使用批量 API 减少开销
3. **选择合适算法**:根据图特征选择算法
4. **调整并行度**:匹配硬件资源
5. **监控性能**:定期运行基准测试

### ❌ 避免做法

1. **频繁重新分配**:避免动态增长大图
2. **过度并行**:小图使用过多线程
3. **忽略内存布局**:使用复杂权重类型
4. **不释放资源**:及时释放不用的图数据
5. **盲目优化**:先测量再优化

---

## 性能检查清单

在性能关键应用中,检查以下项目:

- [ ] 使用 `reserve_vertices()` 预分配
- [ ] 使用批量 API (`add_edges_batch`)
- [ ] 选择合适的分区策略
- [ ] 调整 Rayon 线程池大小
- [ ] 配置算法参数(迭代次数、收敛阈值)
- [ ] 启用并行处理
- [ ] 监控内存使用
- [ ] 运行基准测试对比
- [ ] 使用性能分析工具定位瓶颈
- [ ] 考虑使用近似算法(如需要)

---

## 故障排除

### 常见问题

**Q: 内存使用过高?**
- 检查是否使用复杂权重类型
- 考虑分片处理大图
- 使用 `shrink_to_fit()` 释放多余容量

**Q: 并行性能不佳?**
- 检查线程数是否过多
- 确认工作负载足够并行
- 查看是否有锁竞争

**Q: 算法收敛慢?**
- 调整收敛阈值
- 减少最大迭代次数
- 考虑使用近似算法

---

## 参考资料

- [Rayon 文档]https://docs.rs/rayon
- [Rust 性能优化指南]https://doc.rust-lang.org/book/ch13-00-functional-features.html
- [分布式图处理论文]https://github.com/god-graph/papers

---

*最后更新:2026-03-31*
*版本:god-graph v0.6.0-alpha*