From 9c31f70af112d7d4123eff45891e4b8a2b50050f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Costa?= Date: Sat, 2 Nov 2024 22:43:58 +0000 Subject: [PATCH 1/2] Avoid boxing on RamSurfaceView#getPixels --- .../eu/joaocosta/minart/graphics/SurfaceView.scala | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/shared/src/main/scala/eu/joaocosta/minart/graphics/SurfaceView.scala b/core/shared/src/main/scala/eu/joaocosta/minart/graphics/SurfaceView.scala index e6fae30a..540e6313 100644 --- a/core/shared/src/main/scala/eu/joaocosta/minart/graphics/SurfaceView.scala +++ b/core/shared/src/main/scala/eu/joaocosta/minart/graphics/SurfaceView.scala @@ -237,9 +237,18 @@ object SurfaceView { def unsafeGetPixel(x: Int, y: Int): Color = ramSurface.getPixelOrElse(cx + x, cy + y, SurfaceView.defaultColor) override def getPixels(): Vector[Array[Color]] = { - Vector.tabulate(height) { y => - ramSurface.dataBuffer(cy + y).slice(cx, cx + width) + val b = Vector.newBuilder[Array[Color]] + b.sizeHint(height) + var y = 0 + while (y < height) { + if (width <= 0) { + b += Array.empty[Color] + } else { + b += ramSurface.dataBuffer(cy + y).slice(cx, cx + width) + } + y += 1 } + b.result() } def map(f: Color => Color): SurfaceView = toPlaneSurfaceView().map(f) From f536cc90242015f783decf3e879115a639b88b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Costa?= Date: Sat, 2 Nov 2024 22:53:47 +0000 Subject: [PATCH 2/2] Avoid unnecessary bounds check --- .../main/scala/eu/joaocosta/minart/graphics/Plane.scala | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Plane.scala b/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Plane.scala index 60b7b921..2af4a78c 100644 --- a/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Plane.scala +++ b/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Plane.scala @@ -46,10 +46,9 @@ trait Plane extends Function2[Int, Int, Color] { outer => new Plane { def getPixel(x: Int, y: Int): Color = { val c1 = outer.getPixel(x, y) - if (x >= 0 && y >= 0 && x < that.width && y < that.height) { - val c2 = that.unsafeGetPixel(x, y) - f(c1, c2) - } else c1 + // This plane is clipped into a surface view, so x and y will always be in bounds + val c2 = that.unsafeGetPixel(x, y) + f(c1, c2) } }, width = that.width,