image.domain\performance\performance_opt.mbt

94.1% · 48 / 51 points · 258 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/// # 性能优化 performance/performance_opt
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6
  7. 7///|
  8. 8/// 性能优化工具模块
  9. 9///
  10. 10/// 提供大图分块处理、循环展开、缓存友好数据访问等优化工具。
  11. 11/// v48.0.0: SIMD 友好算法重写 + 缓存优化 + 大图分块
  12. 12
  13. 13///|
  14. 14/// 分块处理参数
  15. 15pub(all) struct TileConfig {
  16. 16 tile_width : Int32
  17. 17 tile_height : Int32
  18. 18 overlap : Int32
  19. 19}
  20. 20
  21. 21///|
  22. 22/// 默认分块配置(256x256,无重叠)
  23. 23pub fn default_tile_config() -> TileConfig {
  24. 24● TileConfig::{ tile_width: 256, tile_height: 256, overlap: 0, }
  25. 25}
  26. 26
  27. 27///|
  28. 28/// 计算分块数量
  29. 29pub fn num_tiles(
  30. 30 width : Int32,
  31. 31 height : Int32,
  32. 32 config : TileConfig,
  33. 33) -> (Int32, Int32) {
  34. 34● let cols = (width + config.tile_width - 1) / config.tile_width
  35. 35 let rows = (height + config.tile_height - 1) / config.tile_height
  36. 36 (cols, rows)
  37. 37}
  38. 38
  39. 39///|
  40. 40/// 获取分块在源图像中的区域
  41. 41pub fn tile_region(
  42. 42 tile_col : Int32,
  43. 43 tile_row : Int32,
  44. 44 config : TileConfig,
  45. 45) -> (Int32, Int32, Int32, Int32) {
  46. 46● let x0 = tile_col * config.tile_width
  47. 47 let y0 = tile_row * config.tile_height
  48. 48 let x1 = x0 + config.tile_width
  49. 49 let y1 = y0 + config.tile_height
  50. 50 (x0, y0, x1, y1)
  51. 51}
  52. 52
  53. 53///|
  54. 54/// 从图像中提取分块
  55. 55pub fn extract_tile(
  56. 56 img : @types.Image,
  57. 57 x0 : Int32,
  58. 58 y0 : Int32,
  59. 59 tile_w : Int32,
  60. 60 tile_h : Int32,
  61. 61) -> @types.Image raise @types.LoadError {
  62. 62● if x0 < 0 || y0 < 0 || tile_w <= 0 || tile_h <= 0 {
  63. 63● raise @types.LoadError::DecodeFailed("Invalid tile parameters")
  64. 64 }
  65. 65 if x0 + tile_w > img.width || y0 + tile_h > img.height {
  66. 66● raise @types.LoadError::DecodeFailed("Tile out of image bounds")
  67. 67 }
  68. 68 let ch = img.channels
  69. 69● let tile_data : Array[Byte] = Array::make(tile_w * tile_h * ch, b'\x00')
  70. 70 for y in 0..<tile_h {
  71. 71● let src_row = (y0 + y) * img.width * ch + x0 * ch
  72. 72 let dst_row = y * tile_w * ch
  73. 73 for x in 0..<(tile_w * ch) {
  74. 74● tile_data[dst_row + x] = img.data[src_row + x]
  75. 75 }
  76. 76 }
  77. 77 @types.Image::{
  78. 78 width: tile_w,
  79. 79 height: tile_h,
  80. 80 channels: ch,
  81. 81● data: Bytes::from_array(tile_data),
  82. 82 }
  83. 83}
  84. 84
  85. 85///|
  86. 86/// 将分块写回图像
  87. 87pub fn paste_tile(
  88. 88 img : @types.Image,
  89. 89 tile : @types.Image,
  90. 90 x0 : Int32,
  91. 91 y0 : Int32,
  92. 92) -> @types.Image raise @types.LoadError {
  93. 93● if x0 < 0 || y0 < 0 {
  94. 94● raise @types.LoadError::DecodeFailed("Invalid paste position")
  95. 95 }
  96. 96 if x0 + tile.width > img.width || y0 + tile.height > img.height {
  97. 97● raise @types.LoadError::DecodeFailed("Tile out of image bounds")
  98. 98 }
  99. 99 if tile.channels != img.channels {
  100. 100● raise @types.LoadError::DecodeFailed("Channel mismatch")
  101. 101 }
  102. 102 let ch = img.channels
  103. 103● let result_data : Array[Byte] = Array::make(
  104. 104 img.width * img.height * ch,
  105. 105 b'\x00',
  106. 106 )
  107. 107● for i in 0..<img.data.length() {
  108. 108● result_data[i] = img.data[i]
  109. 109 }
  110. 110 for y in 0..<tile.height {
  111. 111● let dst_row = (y0 + y) * img.width * ch + x0 * ch
  112. 112 let src_row = y * tile.width * ch
  113. 113 for x in 0..<(tile.width * ch) {
  114. 114● result_data[dst_row + x] = tile.data[src_row + x]
  115. 115 }
  116. 116 }
  117. 117 @types.Image::{
  118. 118 width: img.width,
  119. 119 height: img.height,
  120. 120 channels: ch,
  121. 121● data: Bytes::from_array(result_data),
  122. 122 }
  123. 123}
  124. 124
  125. 125///|
  126. 126/// 分块处理图像
  127. 127///
  128. 128/// 将图像分成小块,对每块应用处理函数,然后合并结果。
  129. 129/// 适用于大图处理,避免一次性加载整个图像到缓存。
  130. 130pub fn process_tiled(
  131. 131 img : @types.Image,
  132. 132 config : TileConfig,
  133. 133 process_fn : (@types.Image) -> @types.Image,
  134. 134) -> @types.Image raise @types.LoadError {
  135. 135● let ch = img.channels
  136. 136● let result_data : Array[Byte] = Array::make(
  137. 137 img.width * img.height * ch,
  138. 138 b'\x00',
  139. 139 )
  140. 140● let (cols, rows) = num_tiles(img.width, img.height, config)
  141. 141 for row in 0..<rows {
  142. 142● for col in 0..<cols {
  143. 143● let (x0, y0, x1, y1) = tile_region(col, row, config)
  144. 144◐ let tw = if x1 > img.width { img.width - x0 } else { config.tile_width }
  145. 145 let th = if y1 > img.height {
  146. 146○ img.height - y0
  147. 147 } else {
  148. 148● config.tile_height
  149. 149 }
  150. 150● let tile = extract_tile(img, x0, y0, tw, th)
  151. 151● let processed = process_fn(tile)
  152. 152● let _ = paste_tile(
  153. 153 @types.Image::{
  154. 154 width: img.width,
  155. 155 height: img.height,
  156. 156 channels: ch,
  157. 157● data: Bytes::from_array(result_data),
  158. 158 },
  159. 159 processed,
  160. 160 x0,
  161. 161 y0,
  162. 162 )
  163. 163 // 直接写入 result_data
  164. 164 for y in 0..<processed.height {
  165. 165● let dst_row = (y0 + y) * img.width * ch + x0 * ch
  166. 166 let src_row = y * processed.width * ch
  167. 167 for x in 0..<(processed.width * ch) {
  168. 168● result_data[dst_row + x] = processed.data[src_row + x]
  169. 169 }
  170. 170 }
  171. 171 }
  172. 172 }
  173. 173 @types.Image::{
  174. 174 width: img.width,
  175. 175 height: img.height,
  176. 176 channels: ch,
  177. 177● data: Bytes::from_array(result_data),
  178. 178 }
  179. 179}
  180. 180
  181. 181///|
  182. 182/// 循环展开的批量复制(4x展开)
  183. 183///
  184. 184/// 将源数组的一段复制到目标数组,使用4次循环展开减少分支开销
  185. 185pub fn bulk_copy_4x(
  186. 186 src : Bytes,
  187. 187 src_offset : Int32,
  188. 188 dst : Array[Byte],
  189. 189 dst_offset : Int32,
  190. 190 len : Int32,
  191. 191) -> Unit {
  192. 192● let mut i = 0
  193. 193 let len4 = len / 4 * 4
  194. 194 while i < len4 {
  195. 195● dst[dst_offset + i] = src[src_offset + i]
  196. 196 dst[dst_offset + i + 1] = src[src_offset + i + 1]
  197. 197 dst[dst_offset + i + 2] = src[src_offset + i + 2]
  198. 198 dst[dst_offset + i + 3] = src[src_offset + i + 3]
  199. 199 i += 4
  200. 200 }
  201. 201 while i < len {
  202. 202● dst[dst_offset + i] = src[src_offset + i]
  203. 203 i += 1
  204. 204 }
  205. 205}
  206. 206
  207. 207///|
  208. 208/// 循环展开的批量填充(4x展开)
  209. 209pub fn bulk_fill_4x(
  210. 210 dst : Array[Byte],
  211. 211 offset : Int32,
  212. 212 len : Int32,
  213. 213 value : Byte,
  214. 214) -> Unit {
  215. 215● let mut i = 0
  216. 216 let len4 = len / 4 * 4
  217. 217 while i < len4 {
  218. 218● dst[offset + i] = value
  219. 219 dst[offset + i + 1] = value
  220. 220 dst[offset + i + 2] = value
  221. 221 dst[offset + i + 3] = value
  222. 222 i += 4
  223. 223 }
  224. 224 while i < len {
  225. 225○ dst[offset + i] = value
  226. 226 i += 1
  227. 227 }
  228. 228}
  229. 229
  230. 230///|
  231. 231/// 缓存友好的行优先处理
  232. 232///
  233. 233/// 按行处理图像,每行处理完后立即写入结果,提高缓存命中率
  234. 234pub fn process_row_major(
  235. 235 img : @types.Image,
  236. 236 process_row : (Int32, Array[Byte]) -> Array[Byte],
  237. 237) -> @types.Image {
  238. 238● let ch = img.channels
  239. 239 let row_len = img.width * ch
  240. 240● let result : Array[Byte] = Array::make(img.width * img.height * ch, b'\x00')
  241. 241 for y in 0..<img.height {
  242. 242● let row_data : Array[Byte] = Array::make(row_len, b'\x00')
  243. 243 let row_offset = y * row_len
  244. 244 for x in 0..<row_len {
  245. 245● row_data[x] = img.data[row_offset + x]
  246. 246 }
  247. 247● let processed = process_row(y, row_data)
  248. 248 for x in 0..<row_len {
  249. 249● result[row_offset + x] = processed[x]
  250. 250 }
  251. 251 }
  252. 252 @types.Image::{
  253. 253 width: img.width,
  254. 254 height: img.height,
  255. 255 channels: ch,
  256. 256● data: Bytes::from_array(result),
  257. 257 }
  258. 258}