Bug 502233 - Extreme slowdown on selecting text in small file
Summary: Extreme slowdown on selecting text in small file
Status: RESOLVED FIXED
Alias: None
Product: kate
Classification: Applications
Component: application (other bugs)
Version First Reported In: 24.12.3
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-03-31 09:19 UTC by Mark van Rossum
Modified: 2025-06-12 19:57 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments
file in question (2.74 KB, application/vnd.wolfram.mathematica.package)
2025-03-31 09:19 UTC, Mark van Rossum
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mark van Rossum 2025-03-31 09:19:52 UTC
Created attachment 179882 [details]
file in question

SUMMARY
Kate becomes very slows and hogs CPU on attached file.

STEPS TO REPRODUCE
1.  Open attached file
2.  Select any text

Cpu goes to close to 100%.
I noticed that kate uses UTF-8 mode, and makes it readonly when trying utf-7.
But there are no UTF-8 characters AFAIK



SOFTWARE/OS VERSIONS

Operating System: Ubuntu 12 (not sure why it says that! It is on Ubunut 25.04 devel)
KDE Plasma Version: 6.3.3
KDE Frameworks Version: 6.12.0
Qt Version: 6.8.2
Kernel Version: 6.14.0-11-generic (64-bit)
Graphics Platform: X11
Processors: 12 × 13th Gen Intel® Core™ i5-1335U
Memory: 31.0 GiB of RAM
Graphics Processor: Intel® Iris® Xe Graphics
Comment 1 Waqar Ahmed 2025-04-09 07:48:06 UTC
The slowdown is in Qt. Its unable to handle a large number of format ranges that are produced by KateRenderer::decorationsForLine when the ranges are passed to QTextLayout::draw(). During text layouting this is not an issue.

One possible fix could be to render selection ourselves always:
- remove the following check in decorationsForLine so that we use the correct foreground color during layouting. This will have implications for printing and screenshot so that needs to be kept in mind:
            if (selectionsOnly) {
                assignSelectionBrushesFromAttribute(fr, *a);
            }
- Dont use Qt for selection background. Use our own function paintTextBackground, that function needs a bit more testing, I think its still a little bit broken with multiple wrapped lines

On quick testing, this removes all lag.
Comment 2 Christoph Cullmann 2025-04-12 15:55:54 UTC
(In reply to Waqar Ahmed from comment #1)
> The slowdown is in Qt. Its unable to handle a large number of format ranges
> that are produced by KateRenderer::decorationsForLine when the ranges are
> passed to QTextLayout::draw(). During text layouting this is not an issue.
> 
> One possible fix could be to render selection ourselves always:
> - remove the following check in decorationsForLine so that we use the
> correct foreground color during layouting. This will have implications for
> printing and screenshot so that needs to be kept in mind:
>             if (selectionsOnly) {
>                 assignSelectionBrushesFromAttribute(fr, *a);
>             }
> - Dont use Qt for selection background. Use our own function
> paintTextBackground, that function needs a bit more testing, I think its
> still a little bit broken with multiple wrapped lines
> 
> On quick testing, this removes all lag.

Given we do most other painting ourself, too, that would be fine with me.
Comment 3 Bug Janitor Service 2025-06-10 13:47:14 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/815
Comment 4 Christoph Cullmann 2025-06-12 19:57:00 UTC
Git commit 0cb99379131b6fb2b80637e6e86da428a630f3bf by Christoph Cullmann, on behalf of Waqar Ahmed.
Committed on 12/06/2025 at 19:45.
Pushed by cullmann into branch 'master'.

Draw selection background ourselves

We were using the "selectionFormats" parameter of QTextLayout::draw()
to draw selections and supplied background + foreground formats for
drawing selections. This leads to extremely poor performance when
there are a lot of "selection formats" e.g., a large list of numbers
on a single line.

To fix this, draw the selection background ourselves and handle the
foreground formats during text layouting phase.

M  +5    -22   src/render/katerenderer.cpp

https://invent.kde.org/frameworks/ktexteditor/-/commit/0cb99379131b6fb2b80637e6e86da428a630f3bf