Skip to content

Interaction with flatpak command completion causes bad performance #628

@ataraxia937

Description

@ataraxia937
$ ble summary

GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu) [Ubuntu 24.04.3 LTS]
ble.sh, version 0.4.0-devel4+8060b7ad (noarch) [git 2.43.0, GNU Make 4.3, GNU Awk 5.2.1, API 3.2, PMA Avon 8-g1, (GNU MPFR 4.2.1, GNU MP 6.3.0)]
bash-completion, version 2.11 (hash:9abb523dbcb688e7fae4b87f5fc9d155b658d9ba, 76898 bytes) (noarch)
atuin, version 18.8.0 (/home/rkohler/.cargo/bin/atuin)
locale: LANG=en_US.UTF-8
terminal: TERM=xterm-256color wcwidth=15.0-west/16.0-2+ri, vte:8002 (61;8002;1)
options: +extglob +histappend -hostcomplete +inherit_errexit

The command flatpak provides a fancy completion function, for use with bash-completion. The function looks like this:

# Check for bash
[ -z "$BASH_VERSION" ] && return

####################################################################################################

__flatpak() {
    local IFS=$'\n'
    local cur=`_get_cword :`
    RES=($(flatpak complete "${COMP_LINE}" "${COMP_POINT}" "${cur}"))

    COMPREPLY=()
    for i in "${!RES[@]}"; do
        if [[ "${RES[$i]}" = "__FLATPAK_FILE" ]]; then
            declare -a COMPGEN_OPTS=('-f')
        elif [[ "${RES[$i]}" = "__FLATPAK_BUNDLE_FILE" ]]; then
            declare -a COMPGEN_OPTS=('-f' '-X' '!*.flatpak')
        elif [[ "${RES[$i]}" = "__FLATPAK_BUNDLE_OR_REF_FILE" ]]; then
            declare -a COMPGEN_OPTS=('-f' '-X' '!*.flatpak@(|ref)')
        elif [[ "${RES[$i]}" = "__FLATPAK_DIR" ]]; then
            declare -a COMPGEN_OPTS=('-d')
        else
            declare -a COMPGEN_OPTS=()
        fi

        if [[ ${#COMPGEN_OPTS[@]} -ne 0 ]]; then
            if [[ "${cur}" = "=" ]]; then
                CUR=""
            else
                CUR="${cur}"
            fi
            COMPREPLY=("${COMPREPLY[@]}" $(compgen ${COMPGEN_OPTS[@]} -- "${CUR}") )
        else
            COMPREPLY=("${COMPREPLY[@]}" "${RES[$i]}")
        fi
    done
}

####################################################################################################

complete -o nospace -F __flatpak flatpak

You can see that it's calling an (undocumented) flatpak complete command. This works fine with the native bash tab-completion. To avoid performance problems when completing remote package IDs, flatpak keeps a cache of the remote package data in $HOME/.cache/flatpak/system-cache/summaries/ (specifically, a file with a .sub extension). Completing a remote package ID this way takes under 600ms:

$ time flatpak complete 'flatpak remote-info -m flathub o' 33 o
[package list skipped here...]
real	0m0.053s
user	0m0.040s
sys	0m0.012s

For some reason, this functionality interacts poorly with ble.sh's auto-complete. When I type in something like flatpak remote-info -m flathub and it calls the completion to show me a list of package IDs, bash becomes unresponsive for 10+ seconds. After each new character I input, I get another similar delay as it generates the new list. As I type more, it does get faster each time, as I narrow the completions down and the list becomes shorter. Sometimes I'm unable to abort the completion with Ctrl-, and sometimes I'm able to do so.

I wonder why it behaves so differently with ble.sh, than it does on its own?

I've worked around this by disabling the smart completion for the flatpak command, since it seems to be the only thing affected. I also considered setting complete_auto_delay to a larger value, to give me more time to keep typing the rest of the ID before completion can run. That does work, but it's not desirable to have it for all the other, well-behaved, commands I want to type. 😃

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions