Skip to content

Commit

Permalink
Merge pull request #539 from JD557/reduce-boxing
Browse files Browse the repository at this point in the history
Reduce boxing overhead
  • Loading branch information
JD557 authored Nov 2, 2024
2 parents badc259 + 7a78d13 commit 4cfcb8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ trait Plane extends Function2[Int, Int, Color] { outer =>
new Plane {
def getPixel(x: Int, y: Int): Color = {
val c1 = outer.getPixel(x, y)
that.getPixel(x, y).fold(c1)(c2 => f(c1, c2))
if (x >= 0 && y >= 0 && x < that.width && y < that.height) {
val c2 = that.unsafeGetPixel(x, y)
f(c1, c2)
} else c1
}
},
width = that.width,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,24 @@ trait Surface {
* @return color matrix
*/
def getPixels(): Vector[Array[Color]] = {
Vector.tabulate(height) { y =>
Array.tabulate(width)(x => unsafeGetPixel(x, y))
val b = Vector.newBuilder[Array[Color]]
b.sizeHint(height)
var y = 0
while (y < height) {
if (width <= 0) {
b += Array.empty[Color]
} else {
val array = new Array[Color](width)
var x = 0
while (x < width) {
array(x) = unsafeGetPixel(x, y)
x += 1
}
b += array
}
y += 1
}
b.result()
}

/** Copies this surface into a new surface stored in RAM. */
Expand Down

0 comments on commit 4cfcb8c

Please sign in to comment.