From ebc3117c114a0268051cbb9a41c1d8fa4c99f64a Mon Sep 17 00:00:00 2001 From: Vadim1987 Date: Thu, 2 Jul 2026 13:42:38 +0200 Subject: [PATCH] perf(input): cut per-character render cost (D2) --- src/util/view.lua | 4 +-- src/view/input/userInputView.lua | 7 ----- tests/util/view_spec.lua | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 tests/util/view_spec.lua diff --git a/src/util/view.lua b/src/util/view.lua index cd4fb48f..01d1f1c6 100644 --- a/src/util/view.lua +++ b/src/util/view.lua @@ -36,7 +36,7 @@ end --- @param selected boolean local write_token = function(dy, dx, token, color, bgcolor, selected) - gfx.push('all') + local r, g, b, a = gfx.getColor() if selected then gfx.setColor(color) local back = string.rep('█', string.ulen(token)) @@ -46,7 +46,7 @@ local write_token = function(dy, dx, token, gfx.setColor(color) end gfx.print(token, dx, dy) - gfx.pop() + gfx.setColor(r, g, b, a) end --- Hide elements for debugging diff --git a/src/view/input/userInputView.lua b/src/view/input/userInputView.lua index 73cbbd83..c9c59626 100644 --- a/src/view/input/userInputView.lua +++ b/src/view/input/userInputView.lua @@ -103,7 +103,6 @@ function UserInputView:render_input(input, status, time) local apparentHeight = inHeight local wrap_forward = vc.wrap_forward - local wrap_reverse = vc.wrap_reverse local vpH = gfx.getHeight() @@ -166,7 +165,6 @@ function UserInputView:render_input(input, status, time) local char = string.usub(s, c, c) local color = colors.fg - local hl_li = wrap_reverse[ln] local tlc = vc:translate_from_visible(Cursor(l, c)) if tlc then @@ -204,11 +202,6 @@ function UserInputView:render_input(input, status, time) end end end)() - local of = calc_overflow(w, text, cursorInfo.cursor) - --- push any further lines down to display phantom line - if ofpos and hl_li > cl then - of = of - 1 - end local dy = (l) * fh local dx = (c - 1) * fw ViewUtils.write_token(dy, dx, diff --git a/tests/util/view_spec.lua b/tests/util/view_spec.lua new file mode 100644 index 00000000..af69373a --- /dev/null +++ b/tests/util/view_spec.lua @@ -0,0 +1,45 @@ +local mock = require("tests.mock") +mock.mock_love({}) +require("util.view") + +describe('view utils #view', function() + local prev_gfx + local color + + setup(function() + prev_gfx = _G.gfx + color = { 1, 1, 1, 1 } + _G.gfx = { + getColor = function() + return color[1], color[2], color[3], color[4] + end, + setColor = function(r, g, b, a) + if type(r) == 'table' then + color = { r[1], r[2], r[3], r[4] } + else + color = { r, g, b, a } + end + end, + print = function() end, + } + end) + + teardown(function() + _G.gfx = prev_gfx + end) + + describe('write_token', function() + it('leaves the active color untouched', function() + gfx.setColor(0.1, 0.2, 0.3, 1) + ViewUtils.write_token(0, 0, 'x', + { 1, 0, 0, 1 }, { 0, 0, 0, 1 }, false) + local r, g, b = gfx.getColor() + assert.same({ 0.1, 0.2, 0.3 }, { r, g, b }) + + ViewUtils.write_token(0, 0, 'x', + { 1, 0, 0, 1 }, { 0, 0, 0, 1 }, true) + r, g, b = gfx.getColor() + assert.same({ 0.1, 0.2, 0.3 }, { r, g, b }) + end) + end) +end)