image.domain\self_evolve\optimize_exec.mbt

83.3% · 15 / 18 points · 92 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 优化执行 self_evolve/optimize_exec
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 管道优化执行 — 应用 OptimizationSuggestion 到实际 IRPipeline
  7. 7// Optimize execution: apply suggestions to actual pipeline and verify
  8. 8//
  9. 9// Phase 5 v283: 将管道优化从"建议"推向"执行" — 实际应用优化 + 成本验证。
  10. 10
  11. 11///|
  12. 12/// 优化执行结果。
  13. 13/// 描述 Optimization execution result.
  14. 14#derive(Eq, @debug.Debug)
  15. 15pub(all) struct OptimizeExecResult {
  16. 16 original_length : Int32
  17. 17 optimized_length : Int32
  18. 18 ops_reduced : Int32
  19. 19 original_cost : Int32
  20. 20 optimized_cost : Int32
  21. 21 cost_reduced : Int32
  22. 22 is_improved : Bool
  23. 23 optimized_pipeline_desc : String
  24. 24}
  25. 25
  26. 26///|
  27. 27pub extend OptimizeExecResult with @debug.Debug::{to_repr}
  28. 28
  29. 29///|
  30. 30pub extend OptimizeExecResult with Eq::{not_equal, equal}
  31. 31
  32. 32///|
  33. 33/// 执行管道优化并返回结果(含成本变化)。
  34. 34/// 执行 Execute pipeline optimization and return result with cost delta.
  35. 35pub fn exec_optimize(pipeline : @imgir.IRPipeline) -> OptimizeExecResult {
  36. 36● let report = optimize_all(pipeline)
  37. 37● let original_baseline = estimate_pipeline_cost(pipeline)
  38. 38● let optimized_baseline = estimate_pipeline_cost(report.optimized_pipeline)
  39. 39 let ops_reduced = report.original_length - report.optimized_length
  40. 40 let cost_reduced = original_baseline.total_cost -
  41. 41 optimized_baseline.total_cost
  42. 42 let is_improved = ops_reduced > 0 || cost_reduced > 0
  43. 43 {
  44. 44 original_length: report.original_length,
  45. 45 optimized_length: report.optimized_length,
  46. 46 ops_reduced,
  47. 47 original_cost: original_baseline.total_cost,
  48. 48 optimized_cost: optimized_baseline.total_cost,
  49. 49 cost_reduced,
  50. 50 is_improved,
  51. 51● optimized_pipeline_desc: @imgir.ir_describe(report.optimized_pipeline),
  52. 52 }
  53. 53}
  54. 54
  55. 55///|
  56. 56/// 对全部 19 种算子执行优化。
  57. 57/// 执行 Execute optimization on all 19 op variants.
  58. 58pub fn exec_optimize_all() -> Array[OptimizeExecResult] {
  59. 59● let results : Array[OptimizeExecResult] = []
  60. 60● for op in all_ops() {
  61. 61● let pipe = @imgir.IRPipeline::new().add(op)
  62. 62● results.push(exec_optimize(pipe))
  63. 63 }
  64. 64 results
  65. 65}
  66. 66
  67. 67///|
  68. 68/// 验证优化后管道不劣化(算子数不增加且成本不增加)。
  69. 69/// 校验 Verify optimization does not degrade (ops and cost non-increasing).
  70. 70pub fn optimize_verified(pipeline : @imgir.IRPipeline) -> Bool {
  71. 71● let result = exec_optimize(pipeline)
  72. 72 result.optimized_length <= result.original_length &&
  73. 73 result.optimized_cost <= result.original_cost
  74. 74}
  75. 75
  76. 76///|
  77. 77/// 优化是否显著(算子减少或成本降低)。
  78. 78/// 描述 Whether optimization is significant.
  79. 79pub fn OptimizeExecResult::is_significant(self : OptimizeExecResult) -> Bool {
  80. 80● self.ops_reduced > 0 || self.cost_reduced > 0
  81. 81}
  82. 82
  83. 83///|
  84. 84/// 优化效率(成本降低百分比,0-100)。
  85. 85/// 描述 Optimization efficiency (cost reduction percentage, 0-100).
  86. 86pub fn OptimizeExecResult::efficiency_pct(self : OptimizeExecResult) -> Int32 {
  87. 87○ if self.original_cost == 0 {
  88. 88○ 0
  89. 89 } else {
  90. 90○ self.cost_reduced * 100 / self.original_cost
  91. 91 }
  92. 92}