Thread View: linux.kernel.git
6 messages
6 total messages
Started by Andy Koppe
Thu, 19 Nov 2026 05:54
Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()
Author: Andy Koppe
Date: Thu, 19 Nov 2026 05:54
Date: Thu, 19 Nov 2026 05:54
22 lines
11973 bytes
11973 bytes
Dirk Gouders wrote:
> +/* Same as above, but for a NUL-terminated string "s". */
> +size_t display_mode_esc_sequence_len(const char *s)
> +{
> + return display_mode_esc_sequence_len_counted(s, strlen(s));
> +}
This is a performance regression compared to the original function.
Calls that previously returned immediately because *s != '\033', or
after a few bytes of scanning, now scan the entire string with strlen.
It turns scanning a whole string for SGRs with repeated calls to the
function from linear to quadratic.
Therefore I think that if the original implementation isn't going to be
kept, it would be better not to provide the wrapper, and thereby make
callers think about what they want to pass as the length.
Uses on strbufs can of course compute it with `sb.buf + sb.len - s`,
but they don't actually need this safety net, because strbufs are
guaranteed to be NUL-terminated and the function stops at NULs.
And the length calculation itself adds a source of errors.
Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()
Author: Andy Koppe
Date: Thu, 19 Nov 2026 20:58
Date: Thu, 19 Nov 2026 20:58
40 lines
13266 bytes
13266 bytes
Dirk Gouders wrote:
>
> On Thu, Nov 19 2026, Andy Koppe wrote:
>
> > Dirk Gouders wrote:
> >> +/* Same as above, but for a NUL-terminated string "s". */
> >> +size_t display_mode_esc_sequence_len(const char *s)
> >> +{
> >> + return display_mode_esc_sequence_len_counted(s, strlen(s));
> >> +}
> >
> > This is a performance regression compared to the original function.
> > Calls that previously returned immediately because *s != '\033', or
> > after a few bytes of scanning, now scan the entire string with strlen.
> > It turns scanning a whole string for SGRs with repeated calls to the
> > function from linear to quadratic.
> >
> > Therefore I think that if the original implementation isn't going to be
> > kept, it would be better not to provide the wrapper, and thereby make
> > callers think about what they want to pass as the length.
> >
> > Uses on strbufs can of course compute it with `sb.buf + sb.len - s`,
> > but they don't actually need this safety net, because strbufs are
> > guaranteed to be NUL-terminated and the function stops at NULs.
> > And the length calculation itself adds a source of errors.
>
> There is only one user in pretty.c that uses the wrapper.
> I can change it to use the _counted version and we could then drop the
> wrapper. Junio's v1 already included such a change and I could steal it
> from there.
>
> Would that be OK?
No objection from me. I would change the make_line_colors_self_contained()
function I'm proposing in [1] to the _counted variant anyway if the original
display_mode_esc_sequence_len() is going away, to avoid the performance issue
with the wrapper.
[1] https://lore.kernel.org/git/e9f88b533dfbd10528c9151e2e87207419d0253d.1794905511.git.andy.koppe@gmail.com
Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()
Author: Andy Koppe
Date: Thu, 19 Nov 2026 21:04
Date: Thu, 19 Nov 2026 21:04
19 lines
12431 bytes
12431 bytes
Andy Koppe wrote: > Dirk Gouders wrote: > > There is only one user in pretty.c that uses the wrapper. > > I can change it to use the _counted version and we could then drop the > > wrapper. Junio's v1 already included such a change and I could steal it > > from there. > > > > Would that be OK? > > No objection from me. I would change the make_line_colors_self_contained() > function I'm proposing in [1] to the _counted variant anyway if the original > display_mode_esc_sequence_len() is going away, to avoid the performance issue > with the wrapper. > > [1] https://lore.kernel.org/git/e9f88b533dfbd10528c9151e2e87207419d0253d.1794905511.git.andy.koppe@gmail.com ps: I guess you might as well just drop the _counted suffix then, and basically just add the len parameter to display_mode_esc_sequence_len().
Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()
Author: Andy Koppe
Date: Fri, 20 Nov 2026 10:16
Date: Fri, 20 Nov 2026 10:16
29 lines
12876 bytes
12876 bytes
On Thu, 19 Nov 2026 at 22:33, Dirk Gouders wrote:
>
> On Thu, Nov 19 2026, Andy Koppe wrote:
>
> > Dirk Gouders wrote:
> >> +/* Same as above, but for a NUL-terminated string "s". */
> >> +size_t display_mode_esc_sequence_len(const char *s)
> >> +{
> >> + return display_mode_esc_sequence_len_counted(s, strlen(s));
> >> +}
> >
> > This is a performance regression compared to the original function.
> > Calls that previously returned immediately because *s != '\033', or
> > after a few bytes of scanning, now scan the entire string with strlen.
> > It turns scanning a whole string for SGRs with repeated calls to the
> > function from linear to quadratic.
>
> How about creating a v3 for display_mode_esc_sequence_len() similar to
> utf8_width() to accept an optional remainder argument?
>
> If NULL is given, display_mode_esc_sequence_len() assumes a
> null-terminated string, otherwise the length is given in *remainder.
> The function can then update the remainder and return the length.
Callers could already sidestep the length check by passing SIZE_MAX, and
the number of consumed bytes is returned, so I don't think that's needed.
Andy
Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()
Author: Junio C Hamano
Date: Fri, 20 Nov 2026 16:36
Date: Fri, 20 Nov 2026 16:36
7 lines
12902 bytes
12902 bytes
Andy Koppe <andy.koppe@gmail.com> writes: > Callers could already sidestep the length check by passing SIZE_MAX, and > the number of consumed bytes is returned, so I don't think that's needed. I like the simplicity of the approach. Thanks.
Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()
Author: Andy Koppe
Date: Fri, 20 Nov 2026 18:03
Date: Fri, 20 Nov 2026 18:03
15 lines
12337 bytes
12337 bytes
On Fri, 20 Nov 2026 at 16:36, Junio C Hamano <gitster@pobox.com> wrote: > > Andy Koppe <andy.koppe@gmail.com> writes: > > > Callers could already sidestep the length check by passing SIZE_MAX, and > > the number of consumed bytes is returned, so I don't think that's needed. > > I like the simplicity of the approach. Thanks. Except, I was wrong, sorry. The 'end = s + len' would be undefined behaviour with SIZE_MAX. Callers do have to pass a valid length. But it didn't sound like there are use cases that don't know the remaining length anyway and can't tolerate calling strlen().
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads