Re: [PATCH bpf-next v12 06/11] btf: Optimize type lookup with binary search

From: Donglin Peng

Date: Tue Jan 13 2026 - 20:50:07 EST


On Wed, Jan 14, 2026 at 8:29 AM Andrii Nakryiko
<andrii.nakryiko@xxxxxxxxx> wrote:
>
> On Fri, Jan 9, 2026 at 5:00 AM Donglin Peng <dolinux.peng@xxxxxxxxx> wrote:
> >
> > From: Donglin Peng <pengdonglin@xxxxxxxxxx>
> >
> > Improve btf_find_by_name_kind() performance by adding binary search
> > support for sorted types. Falls back to linear search for compatibility.
> >
> > Cc: Eduard Zingerman <eddyz87@xxxxxxxxx>
> > Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
> > Cc: Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx>
> > Cc: Alan Maguire <alan.maguire@xxxxxxxxxx>
> > Cc: Ihor Solodrai <ihor.solodrai@xxxxxxxxx>
> > Cc: Xiaoqin Zhang <zhangxiaoqin@xxxxxxxxxx>
> > Signed-off-by: Donglin Peng <pengdonglin@xxxxxxxxxx>
> > ---
> > include/linux/btf.h | 1 +
> > kernel/bpf/btf.c | 91 ++++++++++++++++++++++++++++++++++++++++-----
> > 2 files changed, 83 insertions(+), 9 deletions(-)
> >
>
> [...]
>
> > s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
> > {
> > + const struct btf *base_btf = btf_base_btf(btf);
> > const struct btf_type *t;
> > const char *tname;
> > - u32 i, total;
> > + s32 idx;
> >
> > - total = btf_nr_types(btf);
> > - for (i = 1; i < total; i++) {
> > - t = btf_type_by_id(btf, i);
> > - if (BTF_INFO_KIND(t->info) != kind)
> > - continue;
> > + if (base_btf) {
> > + idx = btf_find_by_name_kind(base_btf, name, kind);
> > + if (idx > 0)
> > + return idx;
> > + }
> >
> > - tname = btf_name_by_offset(btf, t->name_off);
> > - if (!strcmp(tname, name))
> > - return i;
> > + if (btf->named_start_id > 0 && name[0]) {
> > + idx = btf_find_by_name_kind_bsearch(btf, name);
> > + for (; idx < btf_nr_types(btf); idx++) {
>
> same nit about inconsistent btf_nr_types() usage between two branches:
> compute once early and use in both branches
>
> (fixed up similarly to libbpf implementation; also fixed up comment style )

Thanks, I will fix it.

>
> > + t = btf_type_by_id(btf, idx);
> > + tname = btf_name_by_offset(btf, t->name_off);
> > + if (strcmp(tname, name) != 0)
> > + return -ENOENT;
> > + if (BTF_INFO_KIND(t->info) == kind)
> > + return idx;
> > + }
>
> [...]