Collapse surface arrays into a single contiguous chunk (#485)

Better locality in memory.
This commit is contained in:
Jake Wharton
2024-09-25 23:45:02 -04:00
committed by GitHub
parent e2f598859e
commit 687974c0bc

View File

@ -35,18 +35,26 @@ internal class TextSurface(
override var translationX = 0 override var translationX = 0
override var translationY = 0 override var translationY = 0
private val rows = Array(height) { Array(width) { TextPixel(' ') } } private val cells = Array(width * height) { TextPixel(' ') }
override operator fun get(row: Int, column: Int) = rows[translationY + row][translationX + column] override operator fun get(row: Int, column: Int): TextPixel {
val x = translationX + column
val y = row + translationY
check(x in 0 until width)
check(y in 0 until height)
return cells[y * width + x]
}
fun appendRowTo(appendable: Appendable, row: Int) { fun appendRowTo(appendable: Appendable, row: Int) {
// Reused heap allocation for building ANSI attributes inside the loop. // Reused heap allocation for building ANSI attributes inside the loop.
val attributes = mutableIntListOf() val attributes = mutableIntListOf()
val rowPixels = rows[row]
var lastPixel = blankPixel var lastPixel = blankPixel
for (columnIndex in 0 until width) {
val pixel = rowPixels[columnIndex] val rowStart = row * width
val rowStop = rowStart + width
for (columnIndex in rowStart until rowStop) {
val pixel = cells[columnIndex]
if (ansiLevel != AnsiLevel.NONE) { if (ansiLevel != AnsiLevel.NONE) {
if (pixel.foreground != lastPixel.foreground) { if (pixel.foreground != lastPixel.foreground) {