[PATCH bpf-next v12 10/11] libbpf: Optimize the performance of determine_ptr_size

Donglin Peng posted 11 patches 1 month ago
[PATCH bpf-next v12 10/11] libbpf: Optimize the performance of determine_ptr_size
Posted by Donglin Peng 1 month ago
From: Donglin Peng <pengdonglin@xiaomi.com>

Leverage the performance improvement of btf__find_by_name_kind() when
BTF is sorted. For sorted BTF, the function uses binary search with
O(log n) complexity instead of linear search, providing significant
performance benefits, especially for large BTF like vmlinux.

Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alan Maguire <alan.maguire@oracle.com>
Cc: Ihor Solodrai <ihor.solodrai@linux.dev>
Cc: Xiaoqin Zhang <zhangxiaoqin@xiaomi.com>
Signed-off-by: Donglin Peng <pengdonglin@xiaomi.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/lib/bpf/btf.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 9a864de59597..918d9fa6ec36 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -659,29 +659,21 @@ static int determine_ptr_size(const struct btf *btf)
 		"int long unsigned",
 	};
 	const struct btf_type *t;
-	const char *name;
-	int i, j, n;
+	int i, id;
 
 	if (btf->base_btf && btf->base_btf->ptr_sz > 0)
 		return btf->base_btf->ptr_sz;
 
-	n = btf__type_cnt(btf);
-	for (i = 1; i < n; i++) {
-		t = btf__type_by_id(btf, i);
-		if (!btf_is_int(t))
+	for (i = 0; i < ARRAY_SIZE(long_aliases); i++) {
+		id = btf__find_by_name_kind(btf, long_aliases[i], BTF_KIND_INT);
+		if (id < 0)
 			continue;
 
+		t = btf__type_by_id(btf, id);
 		if (t->size != 4 && t->size != 8)
 			continue;
 
-		name = btf__name_by_offset(btf, t->name_off);
-		if (!name)
-			continue;
-
-		for (j = 0; j < ARRAY_SIZE(long_aliases); j++) {
-			if (strcmp(name, long_aliases[j]) == 0)
-				return t->size;
-		}
+		return t->size;
 	}
 
 	return -1;
-- 
2.34.1
Re: [PATCH bpf-next v12 10/11] libbpf: Optimize the performance of determine_ptr_size
Posted by Andrii Nakryiko 3 weeks, 4 days ago
On Fri, Jan 9, 2026 at 5:00 AM Donglin Peng <dolinux.peng@gmail.com> wrote:
>
> From: Donglin Peng <pengdonglin@xiaomi.com>
>
> Leverage the performance improvement of btf__find_by_name_kind() when
> BTF is sorted. For sorted BTF, the function uses binary search with
> O(log n) complexity instead of linear search, providing significant
> performance benefits, especially for large BTF like vmlinux.
>
> Cc: Eduard Zingerman <eddyz87@gmail.com>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
> Cc: Alan Maguire <alan.maguire@oracle.com>
> Cc: Ihor Solodrai <ihor.solodrai@linux.dev>
> Cc: Xiaoqin Zhang <zhangxiaoqin@xiaomi.com>
> Signed-off-by: Donglin Peng <pengdonglin@xiaomi.com>
> Acked-by: Eduard Zingerman <eddyz87@gmail.com>
> Acked-by: Andrii Nakryiko <andrii@kernel.org>
> ---
>  tools/lib/bpf/btf.c | 20 ++++++--------------
>  1 file changed, 6 insertions(+), 14 deletions(-)
>

This change will be beneficial only if btf is sorted, otherwise the
previous approach is generally faster. So on older kernels this will
be significantly slower.

If we want to optimize determine_ptr_size() at all, I think we will
have to take into account whether BTF is sorted or not.

Or just not bother at all with this optimization.

I'll drop this patch.


> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 9a864de59597..918d9fa6ec36 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -659,29 +659,21 @@ static int determine_ptr_size(const struct btf *btf)
>                 "int long unsigned",
>         };
>         const struct btf_type *t;
> -       const char *name;
> -       int i, j, n;
> +       int i, id;
>
>         if (btf->base_btf && btf->base_btf->ptr_sz > 0)
>                 return btf->base_btf->ptr_sz;
>
> -       n = btf__type_cnt(btf);
> -       for (i = 1; i < n; i++) {
> -               t = btf__type_by_id(btf, i);
> -               if (!btf_is_int(t))
> +       for (i = 0; i < ARRAY_SIZE(long_aliases); i++) {
> +               id = btf__find_by_name_kind(btf, long_aliases[i], BTF_KIND_INT);
> +               if (id < 0)
>                         continue;
>
> +               t = btf__type_by_id(btf, id);
>                 if (t->size != 4 && t->size != 8)
>                         continue;
>
> -               name = btf__name_by_offset(btf, t->name_off);
> -               if (!name)
> -                       continue;
> -
> -               for (j = 0; j < ARRAY_SIZE(long_aliases); j++) {
> -                       if (strcmp(name, long_aliases[j]) == 0)
> -                               return t->size;
> -               }
> +               return t->size;
>         }
>
>         return -1;
> --
> 2.34.1
>
Re: [PATCH bpf-next v12 10/11] libbpf: Optimize the performance of determine_ptr_size
Posted by Donglin Peng 3 weeks, 4 days ago
On Wed, Jan 14, 2026 at 8:30 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Jan 9, 2026 at 5:00 AM Donglin Peng <dolinux.peng@gmail.com> wrote:
> >
> > From: Donglin Peng <pengdonglin@xiaomi.com>
> >
> > Leverage the performance improvement of btf__find_by_name_kind() when
> > BTF is sorted. For sorted BTF, the function uses binary search with
> > O(log n) complexity instead of linear search, providing significant
> > performance benefits, especially for large BTF like vmlinux.
> >
> > Cc: Eduard Zingerman <eddyz87@gmail.com>
> > Cc: Alexei Starovoitov <ast@kernel.org>
> > Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
> > Cc: Alan Maguire <alan.maguire@oracle.com>
> > Cc: Ihor Solodrai <ihor.solodrai@linux.dev>
> > Cc: Xiaoqin Zhang <zhangxiaoqin@xiaomi.com>
> > Signed-off-by: Donglin Peng <pengdonglin@xiaomi.com>
> > Acked-by: Eduard Zingerman <eddyz87@gmail.com>
> > Acked-by: Andrii Nakryiko <andrii@kernel.org>
> > ---
> >  tools/lib/bpf/btf.c | 20 ++++++--------------
> >  1 file changed, 6 insertions(+), 14 deletions(-)
> >
>
> This change will be beneficial only if btf is sorted, otherwise the
> previous approach is generally faster. So on older kernels this will
> be significantly slower.

Yes, I agree.

>
> If we want to optimize determine_ptr_size() at all, I think we will
> have to take into account whether BTF is sorted or not.
>
> Or just not bother at all with this optimization.
>
> I'll drop this patch.

Yes, that's correct. The actual lookup executes only once, so the
optimization provides limited value.

>
>
> > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> > index 9a864de59597..918d9fa6ec36 100644
> > --- a/tools/lib/bpf/btf.c
> > +++ b/tools/lib/bpf/btf.c
> > @@ -659,29 +659,21 @@ static int determine_ptr_size(const struct btf *btf)
> >                 "int long unsigned",
> >         };
> >         const struct btf_type *t;
> > -       const char *name;
> > -       int i, j, n;
> > +       int i, id;
> >
> >         if (btf->base_btf && btf->base_btf->ptr_sz > 0)
> >                 return btf->base_btf->ptr_sz;
> >
> > -       n = btf__type_cnt(btf);
> > -       for (i = 1; i < n; i++) {
> > -               t = btf__type_by_id(btf, i);
> > -               if (!btf_is_int(t))
> > +       for (i = 0; i < ARRAY_SIZE(long_aliases); i++) {
> > +               id = btf__find_by_name_kind(btf, long_aliases[i], BTF_KIND_INT);
> > +               if (id < 0)
> >                         continue;
> >
> > +               t = btf__type_by_id(btf, id);
> >                 if (t->size != 4 && t->size != 8)
> >                         continue;
> >
> > -               name = btf__name_by_offset(btf, t->name_off);
> > -               if (!name)
> > -                       continue;
> > -
> > -               for (j = 0; j < ARRAY_SIZE(long_aliases); j++) {
> > -                       if (strcmp(name, long_aliases[j]) == 0)
> > -                               return t->size;
> > -               }
> > +               return t->size;
> >         }
> >
> >         return -1;
> > --
> > 2.34.1
> >