Skip to content

Commit 071110a

Browse files
committed
Improve the performance of the update method
1 parent 6f83eca commit 071110a

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

Sources/EasyImagy/HigherOrderFunctions.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,8 +1421,12 @@ extension Image {
14211421
@_specialize(exported: true, where Pixel == Double)
14221422
@_specialize(exported: true, where Pixel == Bool)
14231423
public mutating func update(_ body: (inout Pixel) throws -> ()) rethrows {
1424-
for i in pixels.indices {
1425-
try body(&pixels[i])
1424+
pixels.withUnsafeMutableBufferPointer {
1425+
var pointer = $0.baseAddress!
1426+
for _ in 0..<count {
1427+
try body(&pointer.pointee)
1428+
pointer += 1
1429+
}
14261430
}
14271431
}
14281432
}

Tests/EasyImagyTests/HigherOrderFunctionsTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ class HigherOrderFunctionsTests : XCTestCase {
9191
}
9292
}
9393

94+
func testUpdatePerformance() {
95+
var image = Image<Int>(width: 1024, height: 1024, pixels: 1...(1024 * 1024))
96+
measure {
97+
image.update { $0 += 1 }
98+
}
99+
}
100+
94101
static var allTests = [
95102
("testMap", testMap),
96103
("testUpdate", testUpdate),

0 commit comments

Comments
 (0)