Bug 504287 - KFileWidget is laggy on systems with many groups
Summary: KFileWidget is laggy on systems with many groups
Status: CONFIRMED
Alias: None
Product: frameworks-kio
Classification: Frameworks and Libraries
Component: Open/save dialogs (other bugs)
Version First Reported In: 6.14.0
Platform: Debian testing Linux
: NOR normal
Target Milestone: ---
Assignee: KIO Bugs
URL:
Keywords: regression
Depends on:
Blocks:
 
Reported: 2025-05-15 18:28 UTC by Ilya Bizyaev
Modified: 2025-07-04 16:30 UTC (History)
6 users (show)

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


Attachments
Flame graph (problematic calls have a blue outline) (502.83 KB, image/png)
2025-05-15 18:30 UTC, Ilya Bizyaev
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ilya Bizyaev 2025-05-15 18:28:47 UTC
SUMMARY
When browsing directories with restricted (unreadable) subdirectories, such as /tmp, the KDE file dialog has major UI lags if the system has many groups. This affects file pickers in KDE and Qt apps, as well as the XDG portal.

This is caused by repeated calls to KUser::groups from the paint code, which calls getgrouplist, which has to read and parse /etc/group and /etc/group.cache over and over again. See the attached flame graph.

$ cat /etc/group.cache | wc -l
27634
$ du -h /etc/group.cache
752K    /etc/group.cache

There should be no need for this: getgrouplist's result can be cached and reused for the duration of the dialog's execution.

STEPS TO REPRODUCE
1. Build KIO with tests
2. Run https://invent.kde.org/frameworks/kio/-/blob/master/tests/kfilewidgettest_gui.cpp
3. Navigate to /tmp or /

OBSERVED RESULT
Moving the mouse over restricted subdirectories (with the lock emblem) causes UI freezes.

EXPECTED RESULT
No UI slowdowns.

SOFTWARE/OS VERSIONS
KDE Frameworks Version: 6.11, 6.14

ADDITIONAL INFORMATION
Relevant code:

* KFileItem::isReadable: https://invent.kde.org/frameworks/kio/-/blob/85ac29eda6/src/core/kfileitem.cpp?page=2#L1292
* KUser::groups: https://invent.kde.org/frameworks/kcoreaddons/-/blob/f67da202b88231/src/lib/util/kuser_unix.cpp#L328
Comment 1 Ilya Bizyaev 2025-05-15 18:30:17 UTC
Created attachment 181350 [details]
Flame graph (problematic calls have a blue outline)
Comment 2 Fabian Vogt 2025-05-17 16:52:18 UTC
(In reply to Ilya Bizyaev from comment #0)
> SUMMARY
> When browsing directories with restricted (unreadable) subdirectories, such
> as /tmp, the KDE file dialog has major UI lags if the system has many
> groups. This affects file pickers in KDE and Qt apps, as well as the XDG
> portal.
> 
> This is caused by repeated calls to KUser::groups from the paint code, which
> calls getgrouplist, which has to read and parse /etc/group and
> /etc/group.cache over and over again. See the attached flame graph.
> 
> $ cat /etc/group.cache | wc -l
> 27634
> $ du -h /etc/group.cache
> 752K    /etc/group.cache
> 
> There should be no need for this: getgrouplist's result can be cached and
> reused for the duration of the dialog's execution.

IMO it should just use access(R_OK) instead.

Apparently this behaviour of trying to handle permission checks in KIO code was introduced by https://invent.kde.org/frameworks/kio/-/merge_requests/1221, ironically with the motivation to avoid stat() calls.
Comment 3 Méven 2025-05-21 09:41:17 UTC
$ cat /etc/group.cache | wc -l
27634
$ du -h /etc/group.cache
752K    /etc/group.cache

That's proper system stress testing.

> IMO it should just use access(R_OK) instead.

That's a good idea, re implementing this logic wasn't a particular great idea.
Comment 4 tlangner+kde 2025-06-16 14:06:13 UTC
Just for reference, this bug makes the file picker almost unusable on my system. I have a similar setup as Ilya with logs of groups on my system. Is there a chance that someone could look at this in the near future? Thanks a lot!