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