Subject: Re: bug#48545: 28.0.50; `icomplete-vertical-mode` does not support the `group-function`

From: Dmitry Gutov <[email-masked]>
Date: Sat, 21 Aug 2021 05:09:40 +0300

On 20.08.2021 13:35, João Távora wrote:

>> True, but since sorting has higher complexity, for large N it should
>> take much longer, and for small N anything is fast anyway.
> 
> This is IME a common misunderstanding, that is usually cleared up by the
> phrase "constant factors matter".  <...>
> 
> In the particular case we're discussing k2 isn't even under "our"
> (meaning minibuffer.el or "the framework") control.  It is determined by
> whatever the completion table author put into `group-function`.  So
> eliding the second term from that equation may be premature.

Even if the constant factor is somehow significant (which would be a 
surprise, but OK, some pathological cases might turn up), if you do any 
kind of grouping at all, you will incur the same cost, won't you?

Unless you only apply the grouping to the first V matches (where it's 
the number of lines visible in the minibuffer). But even then my 
suggestion can be adapted to this approach. Using the example from the 
message I replied to, you would put all the matches in 'xref.el' into 
the same group, not two groups.

The cost of the grouping function doesn't matter when making this choice.

>>> And there's a constant factor in front of that O(N). So that's why I
>>> think measurements should be taken, always.
>>
>> Please go ahead, if you like. I just wanted to make a couple of
>> suggestions here.
> 
> I don't think we understand each other.  If I understand correctly, your
> suggestions is to add a re-grouping, meaning grouping on top the current
> sorting, under the presumption that it will not significantly slow
> things down.

I took an existing example of a grouping UI and suggested a slightly 
different one. With no expected difference in performance.

> That's fine.  My suggestion, however, is different.  It is
> to skip the current sorting for these cases altogether.  My suggestion
> has an actual implementation in the form of a patch, has been tested by
> Kévin and has been benchmarked to show that it speeds up the process.

And I offered a reason for why people might still want that sorting.

That reason is not related to performance.

> You suggestion, as far as I can see, has none of these three elements.
> So if you or someone else wants to experiment with the re-grouping (with
> whichever implemention),

Why do you call it re-grouping? Grouping happens after sorting, there is 
no prior grouping step.

>>> Not to mention that if the table is already "naturally" grouped
>>> upfront, your're incurring in both the sorting cost and the grouping
>>> cost, when you could just be skipping both with little to no
>>> downsides, I would presume.
>>
>> Right. I just don't think either is costly, most of the time.
> 
> Did you read my previous message where I reported that C-x 8 RET takes
> currently takes about a second to compute completions and only 0.6
> seconds when sorting is removed?

I was talking about the grouping step, obviously. Not being costly.

Try disabling grouping. Is completion still slow? Then it's a problem 
with sorting speed that needs to be solved separately anyway.

>> Then, at the very least, you will see at the top of the first group
>> the best available match for your input. That's useful, I think. Even
>> if the remaining items in that group are much worse matches.
> 
> I thought we weren't discussing pattern-matching scenarios here, but OK,
> this is what currently happens (meaning, you can try it yourself!).  As
> always with flex the best available match globally is sorted to the top,
> along with its group.  Icomplete will chose about, say, 10 best matches
> to display.  If two first ones happen happens to have same group,
> Icomplete will shown under the same section.  If the third has another
> group, another section header.  If the fourth global best has again the
> same group as the first two, another section header.  This is flex doing
> its thing.

Yup. This is what I suggested to change.