Re: [RFC PATCH v3 1/3] btf: implement BTF type sorting for accelerated lookups

From: Donglin Peng

Date: Mon Oct 27 2025 - 22:19:02 EST


On Tue, Oct 28, 2025 at 3:06 AM Eduard Zingerman <eddyz87@xxxxxxxxx> wrote:
>
> On Mon, 2025-10-27 at 21:54 +0800, Donglin Peng wrote:
>
> [...]
>
> Question to Andrii, I think.
> It looks a bit asymmetrical, that there is btf_check_sorted() in
> libbpf, but library does not provide comparison or sorting function.
> Wdyt?
>
> > +static void btf_check_sorted(struct btf *btf, int start_id)
> > +{
> > + const struct btf_type *t;
> > + int i, n, nr_sorted_types;
> > +
> > + n = btf__type_cnt(btf);
> > + if (btf->nr_types < BTF_CHECK_SORT_THRESHOLD)
> > + return;
> > +
> > + n--;
> > + nr_sorted_types = 0;
> > + for (i = start_id; i < n; i++) {
> > + int k = i + 1;
> > +
> > + if (btf_compare_type_kinds_names(&i, &k, btf) > 0)
> > + return;
> > +
> > + t = btf_type_by_id(btf, k);
> > + if (!str_is_empty(btf__str_by_offset(btf, t->name_off)))
> > + nr_sorted_types++;
> > + }
> > +
> > + t = btf_type_by_id(btf, start_id);
> > + if (!str_is_empty(btf__str_by_offset(btf, t->name_off)))
> > + nr_sorted_types++;
> > +
> > + if (nr_sorted_types < BTF_CHECK_SORT_THRESHOLD)
> > + return;
>
> Nit: Still think that this is not needed. It trades a couple of CPU
> cycles for this check and a big comment on the top, about why
> it's needed.

Thanks, I will remove it in the next version.

>
> > +
> > + btf->nr_sorted_types = nr_sorted_types;
> > +}
>
> [...]