[PATCH v5 22/25] perf evlist: Try to avoid computing evsel from sample

Ian Rogers
2026-04-03T20:41:16Z

If the sample has an evsel, don't recompute using the sample.id.

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-top.c | 4 +++-
tools/perf/tests/mmap-basic.c | 4 +++-
tools/perf/tests/switch-tracking.c | 5 ++++-
3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index a66666e89f67..f8bca1cb5769 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1164,7 +1164,9 @@ static int deliver_event(struct ordered_events *qe,
goto next_event;
}

- evsel = evlist__id2evsel(session->evlist, sample.id);
+ evsel = sample.evsel;
+ if (!evsel)
+ evsel = evlist__id2evsel(session->evlist, sample.id);
assert(evsel != NULL);

if (event->header.type == PERF_RECORD_SAMPLE) {
diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c
index 3313c236104e..a18d84d858aa 100644
--- a/tools/perf/tests/mmap-basic.c
+++ b/tools/perf/tests/mmap-basic.c
@@ -142,7 +142,9 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest
}

err = -1;
- evsel = evlist__id2evsel(evlist, sample.id);
+ evsel = sample.evsel;
+ if (!evsel)
+ evsel = evlist__id2evsel(evlist, sample.id);
perf_sample__exit(&sample);
if (evsel == NULL) {
pr_debug("event with id %" PRIu64
diff --git a/tools/perf/tests/switch-tracking.c b/tools/perf/tests/switch-tracking.c
index 22b0302252db..e32331fee277 100644
--- a/tools/perf/tests/switch-tracking.c
+++ b/tools/perf/tests/switch-tracking.c
@@ -138,7 +138,10 @@ static int process_sample_event(struct evlist *evlist,
goto out;
}

- evsel = evlist__id2evsel(evlist, sample.id);
+ evsel = sample.evsel;
+ if (!evsel)
+ evsel = evlist__id2evsel(evlist, sample.id);
+
if (evsel == switch_tracking->switch_evsel) {
next_tid = perf_sample__intval(&sample, "next_pid");
prev_tid = perf_sample__intval(&sample, "prev_pid");
--
2.53.0.1213.gd9a14994de-goog