Repository navigation
Description
The KDE 5 tests on Hydra regularly time-out due to KDE 5 applications being so slow. This is mainly due to searching so many paths in XDG_DATA_DIRS, as @edolstra pointed out here. I thought it would be more appropriate to open a ticket for this issue because it has come up in different contexts. Let me review the problem itself and possible solutions.
Cause
This problem affects all KDE 5 applications, but the application in particular which triggered this issue was Dolphin, so I will address it specifically. Dolphin is slow because loading icons requires searching every directory in XDG_DATA_DIRS for icons. Dolphin renders many icons, so this issue hits harder than other applications. kdeWrapper adds many paths to this list so that Dolphin and all the libraries it depends on may find their runtime dependencies. The wrapper allows the dependencies to be resolved whether the application is installed in the user or the system profile and it allows the application to run cleanly from the Nix store. It is also necessary to allow installation of applications depending on different versions of KDE Frameworks or even Qt.
Solution (-1): symlink farming
Until recently, kdeWrapper worked by creating a symlink farm for all the runtime dependencies and adding that path to XDG_DATA_DIRS. This is theoretically nice: a filesystem is a tree, so this basically amounts to creating a lookup tree for all the runtime dependencies. The problem is, every symlink takes up one filesystem block; at 4 KB each, that adds up. As @edolstra pointed out in #20433, this can add up to ~50 MB per application and up to ~400 MB for a typical KDE 5 installation. This is obviously unacceptable!
Solution 0: KDE unwrapped
If we remove the wrappers and resort to propagatedUserEnvPkgs, we gain the speed of symlink farming without the overhead. However, it will no longer be possible to run KDE 5 applications from the Nix store. As @abbradar pointed out, this will resurrect the long-standing problem of Qt applications "poisoning" the system against applications using other Qt versions. As a result, it will probably be impossible to install KDE 5 applications using nix-env. It will also break KDE 5 applications running outside of KDE, except by an obscure workaround.
Solution 1: dynamic symlink farming
The problem with symlink farming is the filesystem block occupied by each symlink. Placing the symlinks on a block-less filesystem like tmpfs removes this overhead! This would require creating the symlink farm dynamically. That is non-negligible overhead, but in principle it only needs to be done once per store path per boot, so it may be an acceptable compromise. Unfortunately, it would be a security nightmare: it would essentially be possible for a non-privileged user to modify the XDG_DATA_DIRS of any running application at any time!
Since the only remotely-acceptable solution on this list is "just let KDE be slow," I'm open to any suggestions!
Activity
abbradar commentedon Dec 21, 2016
Just my humble opinion (again, I'm not even using KDE!) but although all those options seem bad, I'd go with -1 instead of status quo -- disk space is much cheaper than our time that we spend waiting applications to load. 400 MB is bad but not as bad, and (as I've already mentioned in another comment) times would be much, much worse for people with spinning drives.
EDIT: disclaimer: I've tested times in a VM with an SDD, so 9P was involved -- this is entirely unscientific. I waited for a minute (also entirely subjective) but maybe it's because of VM, and on host machines times are better.
ttuegel commentedon Dec 21, 2016
I have spinning drives; it's not noticeable because of the filesystem cache, but on the virtual machine, the extra cache pressure causes a cascading failure.
Ironically, the huge size increase doesn't affect the graphical installer because squashfs is block-less! Now, if only there was a way to get a bunch of squashfs images mounted into the Nix store...
EDIT: sorry, squashfs is block-ful, but it compresses away the overhead.
abbradar commentedon Dec 21, 2016
@ttuegel Hm, sounds good but relying on cache still seems flaky to me. How long are cold boot times?
On the other hand my worries may be entirely ungrounded indeed -- let's bring in some data! I'll try to measure time of cold-booting Dolphin on my host and in a VM (with SSD). I don't have KDE so it would be a more or less good measure. Test plan: use xdotool to wait for window and close it, measure overall process time.
EDIT: by "good measure" I meant that I don't have anything needed for Dolphin in memory, so we get the worst time possible (which is what we want).
ttuegel commentedon Dec 21, 2016
@abbradar Before you run any tests, let me check something about the symlink farm overhead. I think I can reduce it significantly because I was doing something wrong before.
abbradar commentedon Dec 21, 2016
@ttuegel It's too late, I was interested in how to measure this ~_^.
Here's my script (EDIT: updated to do warmup of helper utilities):
Results for my machine:
This is big but manageable if this is a cold boot. I'll postpone measurements in a VM until your experiment with symlink farm. Notice that this is still a very crude measurement (
sleep 0.1is used to wait).ttuegel commentedon Dec 21, 2016
I have a patch for smaller symlink farms now; ~10MB instead of ~50MB. I get these timings before:
and these timings after
That is a significant speed improvement!
@edolstra Is a ~10MB per-application overhead acceptable for a ~30% improvement in startup time?
FRidh commentedon Dec 22, 2016
kdeWrapperis now used for each application. How about considering an env with KDE5 applications, and symlink everything that is needed for that environment (kde5.withPackages). If you will have only one application in an env, the size increase will remain the same. If you have multiple applications in an env, I would expect a smaller size increase because libraries are symlinked only once.samba4: 4.3.11 -> 4.3.13
ttuegel commentedon Dec 22, 2016
@FRidh This is possible. It is essentially what we do already for the Plasma desktop. Plasma consists of a collection of applications; rather than wrapping them individually, we wrap them into a single desktop. We can make this wrapper more expansive, but that will break application installation by
nix-env.abbradar commentedon Dec 22, 2016
What about a hybrid approach? Make individually wrapped applications visible and installable in
nix-envbut also addkde5.withPackages :: (PackageSet -> [Package]) -> Packageand promote it to be used in NixOS configurations?abbradar commentedon Dec 22, 2016
Maybe do it like this:
kde5.foobar-- applicationfoobarwhich is defined as:foobar = wrapPackage (callPackage ./foobar.nix { });;wrapPackageadds passthrough attributeunwrapped;withPackagestakes a list of wrapped packages and re-wraps theirunwrappedversions in a one big environment.This way everybody can use
kde5.foobarand those who want more optimized approach can throw it with other packages intowithPackagesand it will do the right thing.ttuegel commentedon Dec 22, 2016
How should
withPackagesknow which targets to wrap?12 remaining items
abbradar commentedon Dec 28, 2016
@ttuegel Your thoughts on this? If this seems a good idea overall I can make a PR.
ttuegel commentedon Dec 28, 2016
@abbradar It sounds good to me. Please do that!
abbradar commentedon Jan 15, 2017
Can we close this now?
ttuegel commentedon Jan 15, 2017
Yes, everything should be fine now. Thanks!
olejorgenb commentedon Jun 2, 2017
Starting okular 5 is still slowish and stats a zillion files. 2-4 seconds with SSD disk.
Running strace and grepping for "stat(.*icon" i get 433078 stat calls. (
XDG_DATA_DIRShave 9 entries)https://woboq.com/blog/qicon-reads-gtk-icon-cache-in-qt57.html mentions something about an optimization for icon lookup using the gtk icon cache
I see that the cache file is present in /run/current-system/sw/share/icons/hicolor but okular still stats every icon variant in that root directory..d
EDIT: digging more:
https://github.com/qt/qtbase/blob/d1ea4813458b383e66ce4df69d1833b8b6a279c4/src/gui/image/qiconloader.cpp#L200
Checks that the cache is valid by checking timestamps of containing directory and all indexed directories.
Depending of what qt's lastModifed returns it might seem like some subdirectory is younger than the cache on my system: (preventing the cache from being used)(lastModified return the "Modify" field which is the same for all files in the store)#25762 is relevant for this
breeze-icons: generate icon cache
bgamari commentedon Oct 18, 2020
I am still seeing quite long start-up time due to icon searching on NixOS 20.09:
nixos-discourse commentedon May 24, 2021
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/benchmarking-why-nixos-higher-average-cpu-usage/13248/1
vs49688 commentedon Apr 3, 2022
I'm also seeing this with multiple machines on
22.05pre365448.1063244793d.kate:
konsole:
I suspect this is related to
XDG_DATA_DIRSwhich is6398characters long. With konsole, clearing it reduces the amount ofstatxcalls to5656(and starts faster). The konsole wrapper itself adds another3345characters, bringing the total to9743.This is almost certainly related to #126590
nixos-discourse commentedon Mar 14, 2025
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/why-nixos-has-consistently-worse-cpu-performance-compared-to-ubuntu/61677/9
nixos/yuki: add KDE Plasma KDE XDG_DATA_DIRS hack