<div dir="ltr">I noticed disabling safety checks made the custom Iterator as fast as the two nested for-loops.<div>Karl, how does your test change when disabling safety checks?</div><div><br><div>Anyone having an idea why disabling safety checks should make the two sum funcs equally fast in my example program?</div><div><br></div><div>(It shouldn't be the preconditions of Table2D<>'s subscripts, unless the optimizer (with safety checks) can remove them in the case of the nested for-loops but not in the case of the custom iterator.) </div></div><div><br></div><div>/Jens</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 4, 2017 at 6:59 PM, Jens Persson <span dir="ltr"><<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks, I wonder if it is currently impossible to make it as fast as the nested for loops, ie that some optimizer improvement could fix it.<div>Here's a stripped down version of my code:</div><div><br></div><div><div><br></div><div>import QuartzCore // This is just for timing using CACurrentMediaTime()</div><div><br></div><div><br></div><div>struct Point2DInt {</div><div> var x: Int</div><div> var y: Int</div><div>}</div><div><br></div><div>struct Size2DInt {</div><div> var width: Int</div><div> var height: Int</div><div> var rect: Rect2DInt { return Rect2DInt(x: 0, y: 0, width: width, height: height) }</div><div>}</div><div><br></div><div>struct Rect2DInt : Sequence {</div><div> var description: String { return "(\(origin), \(size))" }</div><div> var origin: Point2DInt</div><div> var size: Size2DInt</div><div> var minX: Int {</div><div> get { return origin.x }</div><div> set { size.width = maxX - newValue; origin.x = newValue }</div><div> }</div><div> var maxX: Int {</div><div> get { return origin.x + size.width }</div><div> set { size.width = newValue - minX }</div><div> }</div><div> var minY: Int {</div><div> get { return origin.y }</div><div> set { size.height = maxY - newValue; origin.y = newValue }</div><div> }</div><div> var maxY: Int {</div><div> get { return origin.y + size.height }</div><div> set { size.height = newValue - minY }</div><div> }</div><div> init(origin: Point2DInt, size: Size2DInt) {</div><div> self.origin = origin</div><div> self.size = size</div><div> }</div><div> init(x: Int, y: Int, width: Int, height: Int) {</div><div> self.init(origin: Point2DInt(x: x, y: y), size: Size2DInt(width: width, height: height))</div><div> }</div><div> init(minX: Int, minY: Int, maxX: Int, maxY: Int) {</div><div> self.init(origin: Point2DInt(x: minX, y: minY),</div><div> size: Size2DInt(width: maxX - minX, height: maxY - minY))</div><div> }</div><div> func makeIterator() -> Rect2DIntPointIterator {</div><div> return Rect2DIntPointIterator(rect: self)</div><div> }</div><div>}</div><div><br></div><div><br></div><div>// This is the crucial type here, this version is the fastest that</div><div>// I could find, but it's still slower than two nested for loops,</div><div>// see test below.</div><span class=""><div>struct Rect2DIntPointIterator : IteratorProtocol, Sequence {<br></div><div> let startX, startY, stopX, stopY: Int</div></span><div> var currentPoint: Point2DInt</div><div> init(rect: Rect2DInt) {</div><div> currentPoint = rect.origin</div><span class=""><div> startX = rect.origin.x</div><div> startY = rect.origin.y</div><div> stopX = rect.maxX</div><div> stopY = rect.maxY</div><div> }</div></span><span class=""><div> mutating func next() -> Point2DInt? {</div></span><div> defer { currentPoint.x = currentPoint.x &+ 1 }</div><div> if currentPoint.x == stopX {</div><div> currentPoint.x = startX</div><div> currentPoint.y = currentPoint.y &+ 1</div><div> if currentPoint.y == stopY { return nil }</div><div> }</div><div> return currentPoint</div><div> }</div><div>}</div><div><br></div><div><br></div><div>struct Table2D<Element> {</div><div> let size: Size2DInt</div><div> var storage: [Element]</div><div> init(size: Size2DInt, filledWith element: Element) {</div><div> precondition(size.width > 0 && size.height > 0)</div><div> self.size = size</div><div> self.storage = [Element](repeating: element, count: size.width * size.height)</div><div> }</div><div> subscript(x: Int, y: Int) -> Element {</div><div> get {</div><div> precondition(x >= 0 && y >= 0 && x < size.width && y < size.height)</div><div> return storage[x + y * size.width]</div><div> }</div><div> set {</div><div> precondition(x >= 0 && y >= 0 && x < size.width && y < size.height)</div><div> storage[x + y * size.width] = newValue</div><div> }</div><div> }</div><div> subscript(position: Point2DInt) -> Element {</div><div> get { return self[position.x, position.y] }</div><div> set { self[position.x, position.y] = newValue }</div><div> }</div><div>}</div><div><br></div><div>func randomDouble() -> Double {</div><div> // Returns a random Double in the range [0, 1)</div><div> let ui64 = (UInt64(arc4random()) << 32) | UInt64(arc4random())</div><div> return Double(ui64 >> UInt64(63 - Double.significandBitCount)) * .ulpOfOne/2</div><div>}</div><div>func randomInt(from: Int, to: Int) -> Int {</div><div> // Returns an Int in the range [from, to)</div><div> return Int(Double(from) + (randomDouble() * Double(to - from)).rounded(.down))</div><div>}</div><div><br></div><div>func randomSubrects(of rect: Rect2DInt, minSize: Size2DInt, count: Int) -> [Rect2DInt] {</div><div> precondition(count > 0 && minSize.width > 0 && minSize.height > 0)</div><div> var subrects = [Rect2DInt]()</div><div> subrects.reserveCapacity(<wbr>count)</div><div> for _ in 0 ..< count {</div><div> let size = Size2DInt(width: randomInt(from: minSize.width, to: rect.size.width),</div><div> height: randomInt(from: minSize.height, to: rect.size.height))</div><div> let origin = Point2DInt(x: randomInt(from: 0, to: rect.size.width - size.width),</div><div> y: randomInt(from: 0, to: rect.size.height - size.height))</div><div> subrects.append(Rect2DInt(<wbr>origin: origin, size: size))</div><div> }</div><div> return subrects</div><div>}</div><div><br></div><div><br></div><div>func randomTable(size: Size2DInt) -> Table2D<Double> {</div><div> var table = Table2D(size: size, filledWith: 0.0)</div><div> for p in table.size.rect { table[p] = randomDouble() }</div><div> return table</div><div>}</div><div><br></div><div><br></div><div>func sum1(areas: [Rect2DInt], of table: Table2D<Double>) -> Double {</div><div> var sum = 0.0</div><div> for r in areas {</div><div> // Using custom iterator:</div><div> for p in r { sum += table[p] }</div><div> }</div><div> return sum</div><div>}</div><div><br></div><div>func sum2(areas: [Rect2DInt], of table: Table2D<Double>) -> Double {</div><div> var sum = 0.0</div><div> for r in areas {</div><div> // Using two nested for loops:</div><div> for y in r.minY ..< r.maxY {</div><div> for x in r.minX ..< r.maxX {</div><div> sum = sum + table[x, y]</div><div> }</div><div> }</div><div> }</div><div> return sum</div><div>}</div><div><br></div><div>func test(</div><div> sumFn: ([Rect2DInt], Table2D<Double>) -> Double,</div><div> label: String,</div><div> table: Table2D<Double>,</div><div> areas: [Rect2DInt]</div><div> )</div><div>{</div><div> let t0 = CACurrentMediaTime()</div><div> let sum = sumFn(areas, table)</div><div> let t1 = CACurrentMediaTime()</div><div> print(label, t1 - t0, "seconds (sum \(sum))")</div><div>}</div><div><br></div><div>for _ in 0 ..< 4 {</div><div> let rndTable = randomTable(size: Size2DInt(width: 1000, height: 1000))</div><div> let rndTableAreas = randomSubrects(of: rndTable.size.rect,</div><div> minSize: Size2DInt(width: 100, height: 100),</div><div> count: 1000)</div><div> test(sumFn: sum1, label: "sum1 - using custom iterator ", table: rndTable, areas: rndTableAreas)</div><div> test(sumFn: sum2, label: "sum2 - using nested for-loops ", table: rndTable, areas: rndTableAreas)</div><div> print()</div><div>}</div><div><br></div><div>//</div><div>// Typical output on my MacBook Pro (Retina, 15-inch, Late 2013):</div><div>//</div><div>// sum1 - using custom iterator 0.480134483019356 seconds (sum 153408603.850653)</div><div>// sum2 - using nested for-loops 0.348341046017595 seconds (sum 153408603.850653)</div><div>// </div><div>// sum1 - using custom iterator 0.426998238021042 seconds (sum 149851816.622638)</div><div>// sum2 - using nested for-loops 0.34111139801098 seconds (sum 149851816.622638)</div><div>// </div><div>// sum1 - using custom iterator 0.466021075990284 seconds (sum 155267702.297466)</div><div>// sum2 - using nested for-loops 0.351970263000112 seconds (sum 155267702.297466)</div><div>// </div><div>// sum1 - using custom iterator 0.426723245007452 seconds (sum 146331850.202214)</div><div>// sum2 - using nested for-loops 0.340267747989856 seconds (sum 146331850.202214)</div><div>// </div></div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 4, 2017 at 12:42 PM, Karl <span dir="ltr"><<a href="mailto:razielim@gmail.com" target="_blank">razielim@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hmmm that’s interesting. A brief test I ran:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><font face="Courier">import CoreGraphics</font></div><div><font face="Courier">import Foundation</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">struct PointIterator {</font></div><div><font face="Courier"> let rect: CGRect</font></div><div><font face="Courier"> var nextPoint: CGPoint</font></div><div><font face="Courier"><br></font></div><div><font face="Courier"> let maxX: CGFloat</font></div><div><font face="Courier"> let maxY: CGFloat</font></div><div><font face="Courier"><br></font></div><div><font face="Courier"> init(rect: CGRect) {</font></div><div><font face="Courier"> self.rect = rect.standardized</font></div><div><font face="Courier"> self.nextPoint = self.rect.origin</font></div><div><font face="Courier"> // Cache for fast iteration</font></div><div><font face="Courier"> self.maxX = self.rect.maxX</font></div><div><font face="Courier"> self.maxY = self.rect.maxY</font></div><div><font face="Courier"> }</font></div><div><font face="Courier"><br></font></div><div><font face="Courier"> mutating func next() -> CGPoint? {</font></div><div><font face="Courier"> guard nextPoint.x <= maxX, nextPoint.y <= maxY else {</font></div><div><font face="Courier"> return .none</font></div><div><font face="Courier"> }</font></div><div><font face="Courier"> defer {</font></div><div><font face="Courier"> nextPoint.x += 1</font></div><div><font face="Courier"> if nextPoint.x > maxX {</font></div><div><font face="Courier"> nextPoint.x = rect.origin.x</font></div><div><font face="Courier"> nextPoint.y += 1</font></div><div><font face="Courier"> }</font></div><div><font face="Courier"> }</font></div><div><font face="Courier"> return nextPoint</font></div><div><font face="Courier"> }</font></div><div><font face="Courier">}</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">// Use iterator</font></div><div><font face="Courier">func iteratePoints_it(_ rect: CGRect, with: (CGPoint)->()) {</font></div><div><font face="Courier"> var it = PointIterator(rect: rect)</font></div><div><font face="Courier"> while let point = it.next() {</font></div><div><font face="Courier"> with(point)</font></div><div><font face="Courier"> }</font></div><div><font face="Courier">}</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">// Basic unwrapping of the iterator as a function (no ‘defer’)</font></div><div><font face="Courier">func iteratePoints_fe(_ rect: CGRect, with: (CGPoint)->()) {</font></div><div><font face="Courier"> let rect = rect.standardized</font></div><div><font face="Courier"> var nextPoint = rect.origin</font></div><div><font face="Courier"> let maxX = rect.maxX</font></div><div><font face="Courier"> let maxY = rect.maxY</font></div><div><font face="Courier"> </font></div><div><font face="Courier"> while true {</font></div><div><font face="Courier"> guard nextPoint.x <= maxX, nextPoint.y <= maxY else {</font></div><div><font face="Courier"> return</font></div><div><font face="Courier"> }</font></div><div><font face="Courier"> with(nextPoint) </font></div><div><font face="Courier"> nextPoint.x += 1</font></div><div><font face="Courier"> if nextPoint.x > maxX {</font></div><div><font face="Courier"> nextPoint.x = rect.origin.x</font></div><div><font face="Courier"> nextPoint.y += 1</font></div><div><font face="Courier"> }</font></div><div><font face="Courier"> } </font></div><div><font face="Courier">}</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">// for..in loop</font></div><div><font face="Courier">func iteratePoints_fe2(_ rect: CGRect, with: (CGPoint)->()) {</font></div><div><font face="Courier"> let rect = rect.standardized</font></div><div><font face="Courier"> let maxX = rect.maxX</font></div><div><font face="Courier"> let maxY = rect.maxY</font></div><div><font face="Courier"> for y in stride(from: rect.origin.y, to: maxY, by: 1) {</font></div><div><font face="Courier"> for x in stride(from: rect.origin.x, to: maxX, by: 1) {</font></div><div><font face="Courier"> with(CGPoint(x: x, y: y))</font></div><div><font face="Courier"> }</font></div><div><font face="Courier"> }</font></div><div><font face="Courier">}</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">func profile(_ iterations: Int, _ thing: ()->()) -> TimeInterval {</font></div><div><font face="Courier"> var totalTime: TimeInterval = 0</font></div><div><font face="Courier"> for _ in 0..<iterations {</font></div><div><font face="Courier"> let start = Date().timeIntervalSinceRefere<wbr>nceDate</font></div><div><font face="Courier"> thing()</font></div><div><font face="Courier"> totalTime += (Date().timeIntervalSinceRefer<wbr>enceDate - start)</font></div><div><font face="Courier"> }</font></div><div><font face="Courier"> return totalTime/TimeInterval(iterati<wbr>ons)</font></div><div><font face="Courier">}</font></div><div><font face="Courier"><br></font></div><div><font face="Courier"><br></font></div><div><font face="Courier">let bigRect = CGRect(x: 0, y: 0, width: 10_000, height: 10_000)</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">let iterator = profile(10) { iteratePoints_it(bigRect) { if $0.x > 1_000_000 { print("?") } } } // always false, won't be optimised out.</font></div><div><font face="Courier">let foreach = profile(10) { iteratePoints_fe(bigRect) { if $0.x > 1_000_000 { print("?") } } }</font></div><div><font face="Courier">let foreach2 = profile(10) { iteratePoints_fe2(bigRect) { if $0.x > 1_000_000 { print("?") } } }</font></div><div><font face="Courier">print("iterator: \(iterator) \nforeach: \(foreach) \nforeach2: \(foreach2)")</font></div></blockquote><div><br></div><div>Results:</div><div><br></div><div>iterator: 0.316907703876495 <br>foreach: 0.283202117681503 <br>foreach2: 0.568318998813629</div><div><br></div><div>That ranking is consistent, too. Using an iterator does appear marginally slower than a basic unwrapping of the iterator in to a function.</div><div><br><div><blockquote type="cite"><div><div class="m_-5733715007301674087h5"><div>On 4 Jan 2017, at 09:56, Jens Persson via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_-5733715007301674087m_308211131717086567Apple-interchange-newline"></div></div><div><div><div class="m_-5733715007301674087h5"><div dir="ltr">Hi,<div><br></div><div>I'm working on some low-level pixel processing code (stuff that is not possible to do using standard API or on eg GPU), and I had lots of eg these:</div><div><br></div><div>for y in someStartY ..< someStopY {</div><div> for x in someStartX ..< someStopX {</div><div> ... pixels[x, y] ...</div><div> }</div><div>}</div><div><br></div><div>So I implemented some (value) types like eg IntPoint2D, IntSize2D, IntRect2D and I made an IntRect2DIterator so that IntRect2D could be a Sequence over its (discrete) points. With this I could rewrite the above like so:</div><div><br></div><div>for pixelPosAsIntPoint2D in someIntRect2D {</div><div> ... pixels[pixelPosAsIntPoint2D] ...</div><div>}</div><div><br></div><div>For some reason the first version (two nested for loops for x and y) is always a bit faster than the abstracted version no matter how I write it (tried using eg &+ instead of + etc).</div><div><br></div><div><br></div><div><br></div><div>Is it possible to write as a zero cost abstraction like this, if so, how? If not, why?</div><div><br></div><div><br></div><div><br></div><div><br></div><div>PS</div><div><br></div><div>Note that eg this:</div><div><br></div><div><div>for y in someStartY ..< someStopY {</div><div> for x in someStartX ..< someStopX {</div><div> let pixelPosAsIntPoint2D = IntPoint2D(x: x, y: y)</div><div> ... pixels[pixelPosAsIntPoint2D] ...</div><div> }</div><div>}</div></div><div><br></div><div>is exactly as fast as the top example (using just ... pixels[x, y] ...). So the difference in execution time seems to be due to something in the Iterator and not eg the pixel accessing subscript taking the 2d int point type instead of separate x and y ints.</div><div><br></div><div>Here is one Iterator variant that I have tested:</div><div><br></div><div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px">struct Rect2DIntPointIterator : IteratorProtocol, Sequence {</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> let startX, startY, stopX, stopY: Int</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> var px, py: Int</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> init(rect: Rect2DInt) {</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> startX = rect.origin.x</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> startY = rect.origin.y</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> stopX = rect.maxX</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> stopY = rect.maxY</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> px = startX</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> py = startY</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> }</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> mutating func next() -> Point2DInt? {</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> defer { px = px &+ 1 }</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> if px == stopX {</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> px = startX</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> py = py &+ 1</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> if py == stopY { return nil }</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> }</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> return Point2DInt(x: px, y: py)</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px"> }</span></font></div><div style="margin:0px;line-height:normal"><font face="menlo"><span style="font-size:12px">}</span></font></div></div><div><br></div><div><br></div><div>And here are typical execution times from an example test:</div><div>2.1 seconds using my Iterator (the fastest I can get it, no matter how I try to rewrite it).</div><div>1.5 seconds using nested x, y for loops.</div><div><br></div><div>I'm pretty sure my testing is done thoroughly (measuring average of many runs, using random data, avoiding dead code elimination, whole module optimization, etc).<br></div><div><br></div><div>I have tried profiling the code and looking at the disassmbly but I'm failing to understand what's going on.</div><div><br></div><div>So the ultimate answer would be in the form of a (2d, Int) Rectangle type whose (2d, Int) Points can be iterated in a for loop, at zero cost compared to doing the same using two nested for loops. Or an explanation of why this is impossible.</div><div><br></div><div>DS</div><div><br></div><div>/Jens</div><div><br></div></div></div></div>
______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>