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

Donglin Peng posted 10 patches 2 months ago
There is a newer version of this series
[PATCH bpf-next v9 10/10] libbpf: Optimize the performance of determine_ptr_size
Posted by Donglin Peng 2 months ago
From: pengdonglin <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: pengdonglin <pengdonglin@xiaomi.com>
---
 tools/lib/bpf/btf.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index a53d24704857..6de09a5c4334 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -660,27 +660,41 @@ static int determine_ptr_size(const struct btf *btf)
 	};
 	const struct btf_type *t;
 	const char *name;
-	int i, j, n;
+	int i, j, n, 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))
-			continue;
+	if (btf->sorted_start_id > 0) {
+		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;
 
-		if (t->size != 4 && t->size != 8)
-			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;
+			return t->size;
+		}
+	} else {
+		n = btf__type_cnt(btf);
+		for (i = 1; i < n; i++) {
+			t = btf__type_by_id(btf, i);
+			if (!btf_is_int(t))
+				continue;
+
+			if (t->size != 4 && t->size != 8)
+				continue;
 
-		for (j = 0; j < ARRAY_SIZE(long_aliases); j++) {
-			if (strcmp(name, long_aliases[j]) == 0)
-				return t->size;
+			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;
+			}
 		}
 	}
 
-- 
2.34.1
Re: [PATCH bpf-next v9 10/10] libbpf: Optimize the performance of determine_ptr_size
Posted by Eduard Zingerman 1 month, 3 weeks ago
On Mon, 2025-12-08 at 14:23 +0800, Donglin Peng wrote:
> From: pengdonglin <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.

Is this a big win?
I don't like having two code paths for something which is done once
per BTF load. If it is a big win, maybe just stick with the first loop
(the one that uses btf__find_by_name_kind())? Wdyt?

[...]
Re: [PATCH bpf-next v9 10/10] libbpf: Optimize the performance of determine_ptr_size
Posted by Donglin Peng 1 month, 3 weeks ago
On Wed, Dec 17, 2025 at 3:06 PM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> On Mon, 2025-12-08 at 14:23 +0800, Donglin Peng wrote:
> > From: pengdonglin <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.
>
> Is this a big win?

Here is a comparison:

  w/:     1us
w/o: 351us

> I don't like having two code paths for something which is done once
> per BTF load. If it is a big win, maybe just stick with the first loop
> (the one that uses btf__find_by_name_kind())? Wdyt?

Yes, I agree and will only keep the first loop in the next version.

>
> [...]