Skip to content

KDE 5 applications are slow #21345

Closed
Closed

Description

@ttuegel

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

  1. added
    0.kind: bug
    6.topic: qt/kde
    on Dec 21, 2016
  2. self-assigned this
    on Dec 21, 2016
  3. abbradar commented on Dec 21, 2016

    @abbradar
    Member

    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.

  4. ttuegel commented on Dec 21, 2016

    @ttuegel
    MemberAuthor

    times would be much, much worse for people with spinning drives.

    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.

  5. abbradar commented on Dec 21, 2016

    @abbradar
    Member

    @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).

  6. ttuegel commented on Dec 21, 2016

    @ttuegel
    MemberAuthor

    @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.

  7. abbradar commented on Dec 21, 2016

    @abbradar
    Member

    @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):

    #!/bin/sh
    
    for i in `seq 1 10`; do
      # Drop caches
      echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null
      # Warmup
      xdotool >/dev/null 2>&1
      grep >/dev/null 2>&1
      sleep >/dev/null 2>&1
      killall >/dev/null 2>&1
      # Actual experiment
      time -f 'ELAPSED: %e' result/bin/dolphin >/dev/null &
      # Wait for window to show up
      while true; do
        win=$(xdotool getactivewindow)
        if xdotool getwindowname $win | grep -q Dolphin; then
          break
        fi
        sleep 0.05
      done
      # Kill the app
      killall -9 dolphin
    done
    

    Results for my machine:

    ELAPSED: 1.91
    ELAPSED: 1.77
    ELAPSED: 1.79
    ELAPSED: 1.87
    ELAPSED: 1.78
    ELAPSED: 1.78
    ELAPSED: 1.87
    ELAPSED: 1.87
    ELAPSED: 1.87
    ELAPSED: 1.88
    

    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.1 is used to wait).

  8. ttuegel commented on Dec 21, 2016

    @ttuegel
    MemberAuthor

    I have a patch for smaller symlink farms now; ~10MB instead of ~50MB. I get these timings before:

    ELAPSED: 6.46
    ELAPSED: 6.75
    ELAPSED: 5.95
    ELAPSED: 6.27
    ELAPSED: 6.58
    ELAPSED: 6.52
    ELAPSED: 6.49
    ELAPSED: 6.27
    ELAPSED: 6.45
    ELAPSED: 6.47
    

    and these timings after

    ELAPSED: 4.08
    ELAPSED: 4.63
    ELAPSED: 4.45
    ELAPSED: 4.64
    ELAPSED: 4.87
    ELAPSED: 4.60
    ELAPSED: 4.35
    ELAPSED: 4.86
    ELAPSED: 4.63
    ELAPSED: 4.67
    

    That is a significant speed improvement!

    @edolstra Is a ~10MB per-application overhead acceptable for a ~30% improvement in startup time?

  9. FRidh commented on Dec 22, 2016

    @FRidh
    Member

    kdeWrapper is 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.

  10. added a commit that references this issue on Dec 22, 2016
  11. ttuegel commented on Dec 22, 2016

    @ttuegel
    MemberAuthor

    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.

    @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.

  12. abbradar commented on Dec 22, 2016

    @abbradar
    Member

    What about a hybrid approach? Make individually wrapped applications visible and installable in nix-env but also add kde5.withPackages :: (PackageSet -> [Package]) -> Package and promote it to be used in NixOS configurations?

  13. abbradar commented on Dec 22, 2016

    @abbradar
    Member

    Maybe do it like this:

    • kde5.foobar -- application foobar which is defined as: foobar = wrapPackage (callPackage ./foobar.nix { });;
    • wrapPackage adds passthrough attribute unwrapped;
    • withPackages takes a list of wrapped packages and re-wraps their unwrapped versions in a one big environment.

    This way everybody can use kde5.foobar and those who want more optimized approach can throw it with other packages into withPackages and it will do the right thing.

  14. ttuegel commented on Dec 22, 2016

    @ttuegel
    MemberAuthor

    withPackages takes a list of wrapped packages and re-wraps their unwrapped versions in a one big environment.

    How should withPackages know which targets to wrap?

  15. 12 remaining items

  16. abbradar commented on Dec 28, 2016

    @abbradar
    Member

    @ttuegel Your thoughts on this? If this seems a good idea overall I can make a PR.

  17. ttuegel commented on Dec 28, 2016

    @ttuegel
    MemberAuthor

    @abbradar It sounds good to me. Please do that!

  18. abbradar commented on Jan 15, 2017

    @abbradar
    Member

    Can we close this now?

  19. ttuegel commented on Jan 15, 2017

    @ttuegel
    MemberAuthor

    Yes, everything should be fine now. Thanks!

  20. olejorgenb commented on Jun 2, 2017

    @olejorgenb
    Contributor

    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_DIRS have 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

    How do I make sure the cache is used?

    Every time you install an application that adds or removes icons from one of the themes, the cache needs to be updated. Qt looks at the modification time of the directories, if any directory is newer than the cache, the cache will not be used. You need to run gtk-update-icon-cache after installing new icons, but most distributions take care of this for you.

    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)

    $ cd /run/current-system/sw/share/icons/hicolor
    $ stat 16x16 # snipped
    Modify: 1970-01-01 01:00:01.000000000 +0100
    Change: 2017-05-21 17:37:57.893701963 +0200
    $ stat index.theme
    Modify: 1970-01-01 01:00:01.000000000 +0100
    Change: 2017-05-21 17:37:57.891701952 +0200
    

    #25762 is relevant for this

  21. added a commit that references this issue on Jul 16, 2017
    0d06192
  22. bgamari commented on Oct 18, 2020

    @bgamari
    Contributor

    I am still seeing quite long start-up time due to icon searching on NixOS 20.09:

    $ nix run nixpkgs.kdeApplications.kate -c strace --summary --timestamps=unix,ms -o log kate
    $ tail log
    ...
    % time     seconds  usecs/call     calls    errors syscall
    ------ ----------- ----------- --------- --------- -------------------
     84.58    1.125760           4    251142    250137 access
      4.81    0.063955           4     14939     10959 statx
      2.17    0.028833           4      6838       747 stat
      2.00    0.026677           5      4526      2667 openat
      1.59    0.021119           3      5753         8 lstat
      1.52    0.020274          10      1879           close
      0.65    0.008661           7      1107       212 futex
    ...
    
  23. nixos-discourse commented on May 24, 2021

    @nixos-discourse
  24. vs49688 commented on Apr 3, 2022

    @vs49688
    Contributor

    I'm also seeing this with multiple machines on 22.05pre365448.1063244793d.

    kate:

    % time     seconds  usecs/call     calls    errors syscall
    ------ ----------- ----------- --------- --------- -------------------
     68.96    0.524305           2    212286    198879 statx
      9.22    0.070089           2     32874     30061 readlink
      6.62    0.050304           3     16336      1420 newfstatat
      4.01    0.030460           3      9786      4088 access
      2.47    0.018805           2      7003      4120 openat
      2.34    0.017766           5      3094         2 read
      1.92    0.014630        4876         3           wait4
      0.91    0.006915           5      1350       289 futex
      0.80    0.006065           2      2919           close
    

    konsole:

    % time     seconds  usecs/call     calls    errors syscall
    ------ ----------- ----------- --------- --------- -------------------
     36.86    0.032791           2     13091     12020 statx
     12.89    0.011472           2      4084      3359 openat
      9.37    0.008339        8339         1           wait4
      4.91    0.004372           3      1457      1163 access
      4.78    0.004254           1      2217      1372 newfstatat
      4.62    0.004108           9       449       128 futex
      4.51    0.004013           2      1891      1674 readlink
      4.12    0.003670           3      1034           mmap
      2.77    0.002465           1      1650           getpid
    

    I suspect this is related to XDG_DATA_DIRS which is 6398 characters long. With konsole, clearing it reduces the amount of statx calls to 5656 (and starts faster). The konsole wrapper itself adds another 3345 characters, bringing the total to 9743.

    This is almost certainly related to #126590

  25. nixos-discourse commented on Mar 14, 2025

    @nixos-discourse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

0.kind: bug6.topic: qt/kde

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions