# Index files autocompletion too slow in big repositories (w / suggestion for improvement)

10 messages from 2017-04-14 to 2017-04-17. Participants: Carlos Pita, Ævar Arnfjörð Bjarmason, Jacob Keller, Junio C Hamano, Johannes Sixt.
Thread: https://gitlist.dev/t/45695

## Carlos Pita, 2017-04-14 20:06

Subject: Index files autocompletion too slow in big repositories (w / suggestion for improvement)
Message-ID: <CAELgYhf1s43p62t6W14S=nDt-O247cPqsPMUDfye1OTnDND3Gg@mail.gmail.com>
URL: https://gitlist.dev/e/CAELgYhf1s43p62t6W14S%3DnDt-O247cPqsPMUDfye1OTnDND3Gg%40mail.gmail.com

```
Hi all,

I'm currently using git annex to manage my entire file collection
(including tons of music and books) and I noticed how slow
autocompletion has become for files in the index (say for git add).
The main offender is a while-read-case-echo bash loop in
__git_index_files that can be readily substituted with a much faster
sed invocation, although I guess you didn't want the sed dependency in
the first place. Anyway, here is my benchmark:

__git_index_files ()
{
    local dir="$(__gitdir)" root="${2-.}" file;
    if [ -d "$dir" ]; then
        __git_ls_files_helper "$root" "$1" | while read -r file; do
            case "$file" in
                ?*/*)
                    echo "${file%%/*}"
                ;;
                *)
                    echo "$file"
                ;;
            esac;
        done | sort | uniq;
    fi
}

time __git_index_files > /dev/null


__git_index_files ()
{
    local dir="$(__gitdir)" root="${2-.}" file;
    if [ -d "$dir" ]; then
        __git_ls_files_helper "$root" "$1" | \
            sed -r 's@^"?([^/]+)/.*$@\1@' | sort | uniq
    fi
}

time __git_index_files > /dev/null

real    0m0.830s
user    0m0.597s
sys    0m0.310s

real    0m0.345s
user    0m0.357s
sys    0m0.000s

Notice I'm also excluding the beginning double quote that appears in
escaped path names.

Best regards
--
Carlos

```

## Carlos Pita, 2017-04-14 22:08

Subject: Re: Index files autocompletion too slow in big repositories (w / suggestion for improvement)
Message-ID: <CAELgYhfwwLZXGN9yHZ04koDwGn3=KbuJOxhLM-+PCbumTmMunw@mail.gmail.com>
URL: https://gitlist.dev/e/CAELgYhfwwLZXGN9yHZ04koDwGn3%3DKbuJOxhLM-%2BPCbumTmMunw%40mail.gmail.com
In-Reply-To: <CAELgYhf1s43p62t6W14S=nDt-O247cPqsPMUDfye1OTnDND3Gg@mail.gmail.com>

```
This is much faster (below 0.1s):

__git_index_files ()
{
    local dir="$(__gitdir)" root="${2-.}" file;
    if [ -d "$dir" ]; then
        __git_ls_files_helper "$root" "$1" | \
            sed -r 's@/.*@@' | uniq | sort | uniq
    fi
}

time __git_index_files

real    0m0.075s
user    0m0.083s
sys    0m0.010s

Most of the improvement is due to the simpler, non-grouping, regex.
Since I expect most of the common prefixes to arrive consecutively,
running uniq before sort also improves things a bit. I'm not removing
leading double quotes anymore (this isn't being done by the current
version, anyway) but this doesn't seem to hurt.

Despite the dependence on sed this is ten times faster than the
original, maybe an option to enable fast index completion or something
like that might be desirable.

Best regards
--
Carlos

```

## Ævar Arnfjörð Bjarmason, 2017-04-14 22:33

Subject: Re: Index files autocompletion too slow in big repositories (w / suggestion for improvement)
Message-ID: <CACBZZX7Ajf1c9YKP=MO0T9SV7d0-XZsT=RthJocqZw4_TcCcQw@mail.gmail.com>
URL: https://gitlist.dev/e/CACBZZX7Ajf1c9YKP%3DMO0T9SV7d0-XZsT%3DRthJocqZw4_TcCcQw%40mail.gmail.com
In-Reply-To: <CAELgYhfwwLZXGN9yHZ04koDwGn3=KbuJOxhLM-+PCbumTmMunw@mail.gmail.com>

```
On Sat, Apr 15, 2017 at 12:08 AM, Carlos Pita <carlosjosepita@gmail.com> wrote:
> This is much faster (below 0.1s):
>
> __git_index_files ()
> {
>     local dir="$(__gitdir)" root="${2-.}" file;
>     if [ -d "$dir" ]; then
>         __git_ls_files_helper "$root" "$1" | \
>             sed -r 's@/.*@@' | uniq | sort | uniq
>     fi
> }
>
> time __git_index_files
>
> real    0m0.075s
> user    0m0.083s
> sys    0m0.010s
>
> Most of the improvement is due to the simpler, non-grouping, regex.
> Since I expect most of the common prefixes to arrive consecutively,
> running uniq before sort also improves things a bit. I'm not removing
> leading double quotes anymore (this isn't being done by the current
> version, anyway) but this doesn't seem to hurt.
>
> Despite the dependence on sed this is ten times faster than the
> original, maybe an option to enable fast index completion or something
> like that might be desirable.
>
> Best regards

It's fine to depend on sed, these shell-scripts are POSIX compatible,
and so is sed, we use sed in a lot of the built-in shellscripts.

I think you should submit this as a patch, see Documentation/SubmittingPatches.

```

## Jacob Keller, 2017-04-15 01:37

Subject: Re: Index files autocompletion too slow in big repositories (w / suggestion for improvement)
Message-ID: <CA+P7+xoBYApdmucEpdxT8qHJ0HP5RR2gOkStykd5Fq=8qsZxZQ@mail.gmail.com>
URL: https://gitlist.dev/e/CA%2BP7%2BxoBYApdmucEpdxT8qHJ0HP5RR2gOkStykd5Fq%3D8qsZxZQ%40mail.gmail.com
In-Reply-To: <CACBZZX7Ajf1c9YKP=MO0T9SV7d0-XZsT=RthJocqZw4_TcCcQw@mail.gmail.com>

```
On Fri, Apr 14, 2017 at 3:33 PM, Ævar Arnfjörð Bjarmason
<avarab@gmail.com> wrote:
> On Sat, Apr 15, 2017 at 12:08 AM, Carlos Pita <carlosjosepita@gmail.com> wrote:
>> This is much faster (below 0.1s):
>>
>> __git_index_files ()
>> {
>>     local dir="$(__gitdir)" root="${2-.}" file;
>>     if [ -d "$dir" ]; then
>>         __git_ls_files_helper "$root" "$1" | \
>>             sed -r 's@/.*@@' | uniq | sort | uniq
>>     fi
>> }
>>
>> time __git_index_files
>>
>> real    0m0.075s
>> user    0m0.083s
>> sys    0m0.010s
>>
>> Most of the improvement is due to the simpler, non-grouping, regex.
>> Since I expect most of the common prefixes to arrive consecutively,
>> running uniq before sort also improves things a bit. I'm not removing
>> leading double quotes anymore (this isn't being done by the current
>> version, anyway) but this doesn't seem to hurt.
>>
>> Despite the dependence on sed this is ten times faster than the
>> original, maybe an option to enable fast index completion or something
>> like that might be desirable.
>>
>> Best regards
>
> It's fine to depend on sed, these shell-scripts are POSIX compatible,
> and so is sed, we use sed in a lot of the built-in shellscripts.
>
> I think you should submit this as a patch, see Documentation/SubmittingPatches.

Yea it should be fine to use sed.

Thanks,
Jake

```

## Junio C Hamano, 2017-04-15 07:52

Subject: Re: Index files autocompletion too slow in big repositories (w / suggestion for improvement)
Message-ID: <xmqqr30um7k8.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqr30um7k8.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CA+P7+xoBYApdmucEpdxT8qHJ0HP5RR2gOkStykd5Fq=8qsZxZQ@mail.gmail.com>

```
Jacob Keller <jacob.keller@gmail.com> writes:

> On Fri, Apr 14, 2017 at 3:33 PM, Ævar Arnfjörð Bjarmason
> <avarab@gmail.com> wrote:
>> On Sat, Apr 15, 2017 at 12:08 AM, Carlos Pita <carlosjosepita@gmail.com> wrote:
>>> This is much faster (below 0.1s):
>>>
>>> __git_index_files ()
>>> {
>>>     local dir="$(__gitdir)" root="${2-.}" file;
>>>     if [ -d "$dir" ]; then
>>>         __git_ls_files_helper "$root" "$1" | \
>>>             sed -r 's@/.*@@' | uniq | sort | uniq
>>>     fi
>>> }
>>>
>>> time __git_index_files
>>>
>>> real    0m0.075s
>>> user    0m0.083s
>>> sys    0m0.010s
>>>
>>> Most of the improvement is due to the simpler, non-grouping, regex.
>>> Since I expect most of the common prefixes to arrive consecutively,
>>> running uniq before sort also improves things a bit. I'm not removing
>>> leading double quotes anymore (this isn't being done by the current
>>> version, anyway) but this doesn't seem to hurt.
>>>
>>> Despite the dependence on sed this is ten times faster than the
>>> original, maybe an option to enable fast index completion or something
>>> like that might be desirable.
>>>
>>> Best regards
>>
>> It's fine to depend on sed, these shell-scripts are POSIX compatible,
>> and so is sed, we use sed in a lot of the built-in shellscripts.
>>
>> I think you should submit this as a patch, see Documentation/SubmittingPatches.
>
> Yea it should be fine to use sed.

As long as the use of "sed" is in line with POSIX.1; I do not think
you need the non-portable "-r" merely to strip out everything that
follow the first slash, so perhaps "s|-r|-e|" with the above (and do
not write backslash after pipe at the end of the line---shell knows
you haven't finished talking to it yet if you end a line with a
pipe, and there is no need for backslash), you'd be golden.

```

## Johannes Sixt, 2017-04-15 11:59

Subject: Re: Index files autocompletion too slow in big repositories (w / suggestion for improvement)
Message-ID: <7a07a2a8-07a6-4342-80d0-7684e6242326@kdbg.org>
URL: https://gitlist.dev/e/7a07a2a8-07a6-4342-80d0-7684e6242326%40kdbg.org
In-Reply-To: <CACBZZX7Ajf1c9YKP=MO0T9SV7d0-XZsT=RthJocqZw4_TcCcQw@mail.gmail.com>

```
Cc Gábor.

Am 15.04.2017 um 00:33 schrieb Ævar Arnfjörð Bjarmason:
> On Sat, Apr 15, 2017 at 12:08 AM, Carlos Pita <carlosjosepita@gmail.com> wrote:
>> This is much faster (below 0.1s):
>>
>> __git_index_files ()
>> {
>>     local dir="$(__gitdir)" root="${2-.}" file;
>>     if [ -d "$dir" ]; then
>>         __git_ls_files_helper "$root" "$1" | \
>>             sed -r 's@/.*@@' | uniq | sort | uniq
>>     fi
>> }
>>
>> time __git_index_files
>>
>> real    0m0.075s
>> user    0m0.083s
>> sys    0m0.010s
>>
>> Most of the improvement is due to the simpler, non-grouping, regex.
>> Since I expect most of the common prefixes to arrive consecutively,
>> running uniq before sort also improves things a bit. I'm not removing
>> leading double quotes anymore (this isn't being done by the current
>> version, anyway) but this doesn't seem to hurt.
>>
>> Despite the dependence on sed this is ten times faster than the
>> original, maybe an option to enable fast index completion or something
>> like that might be desirable.
>
> It's fine to depend on sed, these shell-scripts are POSIX compatible,
> and so is sed, we use sed in a lot of the built-in shellscripts.

This is about command line completion. We go a long way to avoid forking 
processes there. What is 10x faster on Linux despite of forking a 
process may not be so on Windows.

(I'm not using bash command line completion on Windows, so I can't tell 
what the effect of your suggested change is on Windows. I hope Gábor can 
comment on it.)

-- Hannes


```

## Johannes Sixt, 2017-04-15 12:30

Subject: Re: Index files autocompletion too slow in big repositories (w / suggestion for improvement)
Message-ID: <3711baf1-af54-b5c9-25e8-9613312c536a@kdbg.org>
URL: https://gitlist.dev/e/3711baf1-af54-b5c9-25e8-9613312c536a%40kdbg.org
In-Reply-To: <CACBZZX7Ajf1c9YKP=MO0T9SV7d0-XZsT=RthJocqZw4_TcCcQw@mail.gmail.com>

```
Cc Gábor, resent with working email (hopefully); please follow-up on 
this mail.

Am 15.04.2017 um 00:33 schrieb Ævar Arnfjörð Bjarmason:
> On Sat, Apr 15, 2017 at 12:08 AM, Carlos Pita <carlosjosepita@gmail.com> wrote:
>> This is much faster (below 0.1s):
>>
>> __git_index_files ()
>> {
>>     local dir="$(__gitdir)" root="${2-.}" file;
>>     if [ -d "$dir" ]; then
>>         __git_ls_files_helper "$root" "$1" | \
>>             sed -r 's@/.*@@' | uniq | sort | uniq
>>     fi
>> }
>>
>> time __git_index_files
>>
>> real    0m0.075s
>> user    0m0.083s
>> sys    0m0.010s
>>
>> Most of the improvement is due to the simpler, non-grouping, regex.
>> Since I expect most of the common prefixes to arrive consecutively,
>> running uniq before sort also improves things a bit. I'm not removing
>> leading double quotes anymore (this isn't being done by the current
>> version, anyway) but this doesn't seem to hurt.
>>
>> Despite the dependence on sed this is ten times faster than the
>> original, maybe an option to enable fast index completion or something
>> like that might be desirable.
>
> It's fine to depend on sed, these shell-scripts are POSIX compatible,
> and so is sed, we use sed in a lot of the built-in shellscripts.

This is about command line completion. We go a long way to avoid forking 
processes there. What is 10x faster on Linux despite of forking a 
process may not be so on Windows.

(I'm not using bash command line completion on Windows, so I can't tell 
what the effect of your suggested change is on Windows. I hope Gábor can 
comment on it.)

-- Hannes

```

## Jacob Keller, 2017-04-16 00:31

Subject: Re: Index files autocompletion too slow in big repositories (w / suggestion for improvement)
Message-ID: <CA+P7+xo+wiTkByd_BrLUEyGc9z7USYT8Joo7OhjrYCUmbHjZ0Q@mail.gmail.com>
URL: https://gitlist.dev/e/CA%2BP7%2Bxo%2BwiTkByd_BrLUEyGc9z7USYT8Joo7OhjrYCUmbHjZ0Q%40mail.gmail.com
In-Reply-To: <7a07a2a8-07a6-4342-80d0-7684e6242326@kdbg.org>

```
On Sat, Apr 15, 2017 at 4:59 AM, Johannes Sixt <j6t@kdbg.org> wrote:
> Cc Gábor.
>
> Am 15.04.2017 um 00:33 schrieb Ævar Arnfjörð Bjarmason:
>>
>> On Sat, Apr 15, 2017 at 12:08 AM, Carlos Pita <carlosjosepita@gmail.com>
>> wrote:
>>>
>>> This is much faster (below 0.1s):
>>>
>>> __git_index_files ()
>>> {
>>>     local dir="$(__gitdir)" root="${2-.}" file;
>>>     if [ -d "$dir" ]; then
>>>         __git_ls_files_helper "$root" "$1" | \
>>>             sed -r 's@/.*@@' | uniq | sort | uniq
>>>     fi
>>> }
>>>
>>> time __git_index_files
>>>
>>> real    0m0.075s
>>> user    0m0.083s
>>> sys    0m0.010s
>>>
>>> Most of the improvement is due to the simpler, non-grouping, regex.
>>> Since I expect most of the common prefixes to arrive consecutively,
>>> running uniq before sort also improves things a bit. I'm not removing
>>> leading double quotes anymore (this isn't being done by the current
>>> version, anyway) but this doesn't seem to hurt.
>>>
>>> Despite the dependence on sed this is ten times faster than the
>>> original, maybe an option to enable fast index completion or something
>>> like that might be desirable.
>>
>>
>> It's fine to depend on sed, these shell-scripts are POSIX compatible,
>> and so is sed, we use sed in a lot of the built-in shellscripts.
>
>
> This is about command line completion. We go a long way to avoid forking
> processes there. What is 10x faster on Linux despite of forking a process
> may not be so on Windows.
>
> (I'm not using bash command line completion on Windows, so I can't tell what
> the effect of your suggested change is on Windows. I hope Gábor can comment
> on it.)
>
> -- Hannes
>

In cases like this, might it be worth somehow splitting it so Linux
can use the best thing, and Windows can continue using what's best for
it, since it is a pretty significant advantage on Linux.

Thanks,
Jake

```

## Junio C Hamano, 2017-04-17 04:05

Subject: Re: Index files autocompletion too slow in big repositories (w / suggestion for improvement)
Message-ID: <xmqqefwrk7ao.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqefwrk7ao.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <7a07a2a8-07a6-4342-80d0-7684e6242326@kdbg.org>

```
Johannes Sixt <j6t@kdbg.org> writes:

> Cc Gábor.
>
> Am 15.04.2017 um 00:33 schrieb Ævar Arnfjörð Bjarmason:
>> On Sat, Apr 15, 2017 at 12:08 AM, Carlos Pita <carlosjosepita@gmail.com> wrote:
>>> This is much faster (below 0.1s):
>>>
>>> __git_index_files ()
>>> {
>>>     local dir="$(__gitdir)" root="${2-.}" file;
>>>     if [ -d "$dir" ]; then
>>>         __git_ls_files_helper "$root" "$1" | \
>>>             sed -r 's@/.*@@' | uniq | sort | uniq
>>>     fi
>>> }
>>>
>>> time __git_index_files
>>>
>>> real    0m0.075s
>>> user    0m0.083s
>>> sys    0m0.010s
>>>
>>> Most of the improvement is due to the simpler, non-grouping, regex.
>>> Since I expect most of the common prefixes to arrive consecutively,
>>> running uniq before sort also improves things a bit. I'm not removing
>>> leading double quotes anymore (this isn't being done by the current
>>> version, anyway) but this doesn't seem to hurt.
>>>
>>> Despite the dependence on sed this is ten times faster than the
>>> original, maybe an option to enable fast index completion or something
>>> like that might be desirable.
>>
>> It's fine to depend on sed, these shell-scripts are POSIX compatible,
>> and so is sed, we use sed in a lot of the built-in shellscripts.
>
> This is about command line completion. We go a long way to avoid
> forking processes there. What is 10x faster on Linux despite of
> forking a process may not be so on Windows.

Doesn't this depend on how many paths there are?  If there are only
a few paths, the loop in shell would beat a pipe into sed even on
Linux, I suspect, and if there are tons of paths, at some number,
loop in shell would become slower than a single spawning of sed on
platforms with slower fork, no?

```

## Johannes Sixt, 2017-04-17 08:03

Subject: Re: Index files autocompletion too slow in big repositories (w / suggestion for improvement)
Message-ID: <2304c00a-ed87-7119-3753-95f61bcf28f6@kdbg.org>
URL: https://gitlist.dev/e/2304c00a-ed87-7119-3753-95f61bcf28f6%40kdbg.org
In-Reply-To: <xmqqefwrk7ao.fsf@gitster.mtv.corp.google.com>

```
Am 17.04.2017 um 06:05 schrieb Junio C Hamano:
> Johannes Sixt <j6t@kdbg.org> writes:
>> This is about command line completion. We go a long way to avoid
>> forking processes there. What is 10x faster on Linux despite of
>> forking a process may not be so on Windows.
>
> Doesn't this depend on how many paths there are?  If there are only
> a few paths, the loop in shell would beat a pipe into sed even on
> Linux, I suspect, and if there are tons of paths, at some number,
> loop in shell would become slower than a single spawning of sed on
> platforms with slower fork, no?

Absolutely. I just want to make sure a suggested change takes into 
account the situation on Windows, not only the "YESSSS!" and "VERY 
WELL!" votes of Linux users ;)

-- Hannes


```
