image.domain\self_evolve\optimize.mbt
100% · 53 / 53 points · 110 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 优化 self_evolve/optimize - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 管道优化 — 消除冗余 + 合并相邻 - 7
// Pipeline optimization: remove redundant + merge adjacent - 8
- 9
///| - 10
/// 优化管道:消除恒等算子 + 互逆算子对 + 合并相邻同类算子。 - 11
/// 描述 Optimize pipeline: remove identity + inverse pairs + merge adjacent. - 12
pub fn optimize_pipeline(pipeline : @imgir.IRPipeline) -> @imgir.IRPipeline { - 13●
let ops = pipeline.ops - 14
let result : Array[@imgir.IROp] = [] - 15
for op in ops { - 16●
if is_identity_op(op) { - 17●
() - 18●
} else if result.length() > 0 && - 19●
check_inverse_pair(result[result.length() - 1], op) { - 20●
ignore(result.pop()) - 21●
} else if result.length() > 0 && can_merge(result[result.length() - 1], op) { - 22●
let last = result[result.length() - 1] - 23●
ignore(result.pop()) - 24●
result.push(merge_ops(last, op)) - 25
} else { - 26●
result.push(op) - 27
} - 28
} - 29●
let mut pipe = @imgir.IRPipeline::new() - 30
for op in result { - 31●
pipe = pipe.add(op) - 32
} - 33
pipe - 34
} - 35
- 36
///| - 37
/// 生成优化建议(不实际优化,仅报告可优化点)。 - 38
/// 生成 Generate optimization suggestions (report only, no actual optimization). - 39
pub fn suggest_optimizations( - 40
pipeline : @imgir.IRPipeline, - 41
) -> Array[OptimizationSuggestion] { - 42●
let ops = pipeline.ops - 43
let suggestions : Array[OptimizationSuggestion] = [] - 44
// 检查恒等算子 - 45●
for i in 0..<ops.length() { - 46●
if is_identity_op(ops[i]) { - 47●
suggestions.push({ - 48
kind: RemoveIdentity, - 49
op_indices: [i], - 50●
description: "位置 " + i.to_string() + " 是恒等算子,可移除", - 51
before_count: 1, - 52
after_count: 0, - 53
}) - 54
} - 55
} - 56
// 检查互逆算子对 - 57●
for i in 0..<(ops.length() - 1) { - 58●
if check_inverse_pair(ops[i], ops[i + 1]) { - 59●
suggestions.push({ - 60
kind: RemoveRedundant, - 61
op_indices: [i, i + 1], - 62
description: "位置 " + - 63●
i.to_string() + - 64
" 和 " + - 65●
(i + 1).to_string() + - 66
" 互逆,可移除", - 67
before_count: 2, - 68
after_count: 0, - 69
}) - 70
} - 71
} - 72
// 检查可合并的相邻算子 - 73●
for i in 0..<(ops.length() - 1) { - 74●
if can_merge(ops[i], ops[i + 1]) { - 75●
suggestions.push({ - 76
kind: MergeAdjacent, - 77
op_indices: [i, i + 1], - 78
description: "位置 " + - 79●
i.to_string() + - 80
" 和 " + - 81●
(i + 1).to_string() + - 82
" 可合并", - 83
before_count: 2, - 84
after_count: 1, - 85
}) - 86
} - 87
} - 88
suggestions - 89
} - 90
- 91
///| - 92
/// 完整优化:返回优化报告。 - 93
/// 描述 Full optimization: return optimization report. - 94
pub fn optimize_all(pipeline : @imgir.IRPipeline) -> OptimizationReport { - 95●
let optimized = optimize_pipeline(pipeline) - 96●
let suggestions = suggest_optimizations(pipeline) - 97
{ - 98●
original_length: pipeline.length(), - 99●
optimized_length: optimized.length(), - 100
suggestions, - 101
optimized_pipeline: optimized, - 102
} - 103
} - 104
- 105
///| - 106
/// 计算优化后的算子数(不实际构建管道)。 - 107
/// 计数 Count ops after optimization (without building pipeline). - 108
pub fn optimized_op_count(pipeline : @imgir.IRPipeline) -> Int32 { - 109●
optimize_pipeline(pipeline).length() - 110
}