1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-26 07:03:22 +00:00

Some tiny optimisations to the window API

- Use integer indexes instead of strings (i.e. text, textColour). This
   is a tiny bit faster.
 - Avoid re-creating tables when clearing.

We're still mostly limited by the VM (slow) and string concatenation
(slow!). Short of having some low-level mutable buffer type, I don't
think we can improve this much :(.
This commit is contained in:
Jonathan Coates 2023-06-25 20:56:27 +01:00
parent 54ab98473f
commit 4accda6b8e
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06

View File

@ -131,11 +131,7 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible)
local sEmptyTextColor = tEmptyColorLines[nTextColor] local sEmptyTextColor = tEmptyColorLines[nTextColor]
local sEmptyBackgroundColor = tEmptyColorLines[nBackgroundColor] local sEmptyBackgroundColor = tEmptyColorLines[nBackgroundColor]
for y = 1, nHeight do for y = 1, nHeight do
tLines[y] = { tLines[y] = { sEmptyText, sEmptyTextColor, sEmptyBackgroundColor }
text = sEmptyText,
textColor = sEmptyTextColor,
backgroundColor = sEmptyBackgroundColor,
}
end end
for i = 0, 15 do for i = 0, 15 do
@ -165,7 +161,7 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible)
local function redrawLine(n) local function redrawLine(n)
local tLine = tLines[n] local tLine = tLines[n]
parent.setCursorPos(nX, nY + n - 1) parent.setCursorPos(nX, nY + n - 1)
parent.blit(tLine.text, tLine.textColor, tLine.backgroundColor) parent.blit(tLine[1], tLine[2], tLine[3])
end end
local function redraw() local function redraw()
@ -188,9 +184,9 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible)
-- Modify line -- Modify line
local tLine = tLines[nCursorY] local tLine = tLines[nCursorY]
if nStart == 1 and nEnd == nWidth then if nStart == 1 and nEnd == nWidth then
tLine.text = sText tLine[1] = sText
tLine.textColor = sTextColor tLine[2] = sTextColor
tLine.backgroundColor = sBackgroundColor tLine[3] = sBackgroundColor
else else
local sClippedText, sClippedTextColor, sClippedBackgroundColor local sClippedText, sClippedTextColor, sClippedBackgroundColor
if nStart < 1 then if nStart < 1 then
@ -210,9 +206,9 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible)
sClippedBackgroundColor = sBackgroundColor sClippedBackgroundColor = sBackgroundColor
end end
local sOldText = tLine.text local sOldText = tLine[1]
local sOldTextColor = tLine.textColor local sOldTextColor = tLine[2]
local sOldBackgroundColor = tLine.backgroundColor local sOldBackgroundColor = tLine[3]
local sNewText, sNewTextColor, sNewBackgroundColor local sNewText, sNewTextColor, sNewBackgroundColor
if nStart > 1 then if nStart > 1 then
local nOldEnd = nStart - 1 local nOldEnd = nStart - 1
@ -231,9 +227,9 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible)
sNewBackgroundColor = sNewBackgroundColor .. string_sub(sOldBackgroundColor, nOldStart, nWidth) sNewBackgroundColor = sNewBackgroundColor .. string_sub(sOldBackgroundColor, nOldStart, nWidth)
end end
tLine.text = sNewText tLine[1] = sNewText
tLine.textColor = sNewTextColor tLine[2] = sNewTextColor
tLine.backgroundColor = sNewBackgroundColor tLine[3] = sNewBackgroundColor
end end
-- Redraw line -- Redraw line
@ -280,11 +276,10 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible)
local sEmptyTextColor = tEmptyColorLines[nTextColor] local sEmptyTextColor = tEmptyColorLines[nTextColor]
local sEmptyBackgroundColor = tEmptyColorLines[nBackgroundColor] local sEmptyBackgroundColor = tEmptyColorLines[nBackgroundColor]
for y = 1, nHeight do for y = 1, nHeight do
tLines[y] = { local line = tLines[y]
text = sEmptyText, line[1] = sEmptyText
textColor = sEmptyTextColor, line[2] = sEmptyTextColor
backgroundColor = sEmptyBackgroundColor, line[3] = sEmptyBackgroundColor
}
end end
if bVisible then if bVisible then
redraw() redraw()
@ -295,14 +290,10 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible)
function window.clearLine() function window.clearLine()
if nCursorY >= 1 and nCursorY <= nHeight then if nCursorY >= 1 and nCursorY <= nHeight then
local sEmptyText = sEmptySpaceLine local line = tLines[nCursorY]
local sEmptyTextColor = tEmptyColorLines[nTextColor] line[1] = sEmptySpaceLine
local sEmptyBackgroundColor = tEmptyColorLines[nBackgroundColor] line[2] = tEmptyColorLines[nTextColor]
tLines[nCursorY] = { line[3] = tEmptyColorLines[nBackgroundColor]
text = sEmptyText,
textColor = sEmptyTextColor,
backgroundColor = sEmptyBackgroundColor,
}
if bVisible then if bVisible then
redrawLine(nCursorY) redrawLine(nCursorY)
updateCursorColor() updateCursorColor()
@ -431,11 +422,7 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible)
if y >= 1 and y <= nHeight then if y >= 1 and y <= nHeight then
tNewLines[newY] = tLines[y] tNewLines[newY] = tLines[y]
else else
tNewLines[newY] = { tNewLines[newY] = { sEmptyText, sEmptyTextColor, sEmptyBackgroundColor }
text = sEmptyText,
textColor = sEmptyTextColor,
backgroundColor = sEmptyBackgroundColor,
}
end end
end end
tLines = tNewLines tLines = tNewLines
@ -478,7 +465,8 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible)
error("Line is out of range.", 2) error("Line is out of range.", 2)
end end
return tLines[y].text, tLines[y].textColor, tLines[y].backgroundColor local line = tLines[y]
return line[1], line[2], line[3]
end end
-- Other functions -- Other functions
@ -574,26 +562,22 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible)
local sEmptyBackgroundColor = tEmptyColorLines[nBackgroundColor] local sEmptyBackgroundColor = tEmptyColorLines[nBackgroundColor]
for y = 1, new_height do for y = 1, new_height do
if y > nHeight then if y > nHeight then
tNewLines[y] = { tNewLines[y] = { sEmptyText, sEmptyTextColor, sEmptyBackgroundColor }
text = sEmptyText,
textColor = sEmptyTextColor,
backgroundColor = sEmptyBackgroundColor,
}
else else
local tOldLine = tLines[y] local tOldLine = tLines[y]
if new_width == nWidth then if new_width == nWidth then
tNewLines[y] = tOldLine tNewLines[y] = tOldLine
elseif new_width < nWidth then elseif new_width < nWidth then
tNewLines[y] = { tNewLines[y] = {
text = string_sub(tOldLine.text, 1, new_width), string_sub(tOldLine[1], 1, new_width),
textColor = string_sub(tOldLine.textColor, 1, new_width), string_sub(tOldLine[2], 1, new_width),
backgroundColor = string_sub(tOldLine.backgroundColor, 1, new_width), string_sub(tOldLine[3], 1, new_width),
} }
else else
tNewLines[y] = { tNewLines[y] = {
text = tOldLine.text .. string_sub(sEmptyText, nWidth + 1, new_width), tOldLine[1] .. string_sub(sEmptyText, nWidth + 1, new_width),
textColor = tOldLine.textColor .. string_sub(sEmptyTextColor, nWidth + 1, new_width), tOldLine[2] .. string_sub(sEmptyTextColor, nWidth + 1, new_width),
backgroundColor = tOldLine.backgroundColor .. string_sub(sEmptyBackgroundColor, nWidth + 1, new_width), tOldLine[3] .. string_sub(sEmptyBackgroundColor, nWidth + 1, new_width),
} }
end end
end end