Re: [PATCH v3 02/22] perf cpumap: Use for each loop

Arnaldo Carvalho de Melo
2022-02-11T17:04:28Z

Em Fri, Feb 11, 2022 at 02:33:55AM -0800, Ian Rogers escreveu:
> Improve readability in perf_pmu__cpus_match by using
> perf_cpu_map__for_each_cpu.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> tools/perf/util/pmu.c | 14 ++++++--------
> 1 file changed, 6 insertions(+), 8 deletions(-)
>
> diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
> index 8dfbba15aeb8..9a1c7e63e663 100644
> --- a/tools/perf/util/pmu.c
> +++ b/tools/perf/util/pmu.c
> @@ -1998,7 +1998,8 @@ int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
> {
> struct perf_cpu_map *pmu_cpus = pmu->cpus;
> struct perf_cpu_map *matched_cpus, *unmatched_cpus;
> - int matched_nr = 0, unmatched_nr = 0;
> + struct perf_cpu cpu;
> + int i, matched_nr = 0, unmatched_nr = 0;
>
> matched_cpus = perf_cpu_map__default_new();
> if (!matched_cpus)
> @@ -2010,14 +2011,11 @@ int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
> return -1;
> }
>
> - for (int i = 0; i < cpus->nr; i++) {
> - int cpu;
> -
> - cpu = perf_cpu_map__idx(pmu_cpus, cpus->map[i]);
> - if (cpu == -1)
> - unmatched_cpus->map[unmatched_nr++] = cpus->map[i];
> + perf_cpu_map__for_each_cpu(cpu, i, cpus) {

I'm applying this patch, but I wonder if we couldn't remove the need for
pre-declaring the integer iterator, so that the previous patch hunk
wouldn't be needed.

> + if (!perf_cpu_map__has(pmu_cpus, cpu))
> + unmatched_cpus->map[unmatched_nr++] = cpu;
> else
> - matched_cpus->map[matched_nr++] = cpus->map[i];
> + matched_cpus->map[matched_nr++] = cpu;
> }
>
> unmatched_cpus->nr = unmatched_nr;
> --
> 2.35.1.265.g69c8d7142f-goog

--

- Arnaldo