2021-08-21 09:21:49

by Riccardo Mancini

[permalink] [raw]
Subject: [RFC PATCH v1 02/37] libperf cpumap: improve max function

From commit 7074674e7338863e ("perf cpumap: Maintain cpumaps ordered and
without dups"), perf_cpu_map elements are sorted in ascending order.

This patch improves the perf_cpu_map__max function by returning the last
element.

Signed-off-by: Riccardo Mancini <[email protected]>
---
tools/lib/perf/cpumap.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c
index fb633272be3aaed9..80e03b2f0c60cce7 100644
--- a/tools/lib/perf/cpumap.c
+++ b/tools/lib/perf/cpumap.c
@@ -284,14 +284,10 @@ int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)

int perf_cpu_map__max(struct perf_cpu_map *map)
{
- int i, max = -1;
-
- for (i = 0; i < map->nr; i++) {
- if (map->map[i] > max)
- max = map->map[i];
- }
-
- return max;
+ if (map->nr > 0)
+ return map->map[map->nr-1];
+ else
+ return -1;
}

/*
--
2.31.1


2021-08-31 18:50:07

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [RFC PATCH v1 02/37] libperf cpumap: improve max function

Em Sat, Aug 21, 2021 at 11:19:08AM +0200, Riccardo Mancini escreveu:
> >From commit 7074674e7338863e ("perf cpumap: Maintain cpumaps ordered and
> without dups"), perf_cpu_map elements are sorted in ascending order.
>
> This patch improves the perf_cpu_map__max function by returning the last
> element.
>
> Signed-off-by: Riccardo Mancini <[email protected]>
> ---
> tools/lib/perf/cpumap.c | 12 ++++--------
> 1 file changed, 4 insertions(+), 8 deletions(-)
>
> diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c
> index fb633272be3aaed9..80e03b2f0c60cce7 100644
> --- a/tools/lib/perf/cpumap.c
> +++ b/tools/lib/perf/cpumap.c
> @@ -284,14 +284,10 @@ int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
>
> int perf_cpu_map__max(struct perf_cpu_map *map)
> {
> - int i, max = -1;
> -
> - for (i = 0; i < map->nr; i++) {
> - if (map->map[i] > max)
> - max = map->map[i];
> - }
> -
> - return max;
> + if (map->nr > 0)
> + return map->map[map->nr-1];
> + else
> + return -1;

Applying, but adding spaces around the '-',

Thanks.

- Arnaldo

2021-08-31 19:20:08

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [RFC PATCH v1 02/37] libperf cpumap: improve max function

Em Tue, Aug 31, 2021 at 03:47:10PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Sat, Aug 21, 2021 at 11:19:08AM +0200, Riccardo Mancini escreveu:
> > >From commit 7074674e7338863e ("perf cpumap: Maintain cpumaps ordered and
> > without dups"), perf_cpu_map elements are sorted in ascending order.
> >
> > This patch improves the perf_cpu_map__max function by returning the last
> > element.
> >
> > Signed-off-by: Riccardo Mancini <[email protected]>
> > ---
> > tools/lib/perf/cpumap.c | 12 ++++--------
> > 1 file changed, 4 insertions(+), 8 deletions(-)
> >
> > diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c
> > index fb633272be3aaed9..80e03b2f0c60cce7 100644
> > --- a/tools/lib/perf/cpumap.c
> > +++ b/tools/lib/perf/cpumap.c
> > @@ -284,14 +284,10 @@ int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
> >
> > int perf_cpu_map__max(struct perf_cpu_map *map)
> > {
> > - int i, max = -1;
> > -
> > - for (i = 0; i < map->nr; i++) {
> > - if (map->map[i] > max)
> > - max = map->map[i];
> > - }
> > -
> > - return max;
> > + if (map->nr > 0)
> > + return map->map[map->nr-1];
> > + else
> > + return -1;
>
> Applying, but adding spaces around the '-',

I ended up with this, ok?

+++ b/tools/lib/perf/cpumap.c
@@ -282,10 +282,8 @@ int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)

int perf_cpu_map__max(struct perf_cpu_map *map)
{
- if (map->nr > 0)
- return map->map[map->nr-1];
- else
- return -1;
+ // cpu_map__trim_new() qsort()s it, cpu_map__default_new() sorts it as well.
+ return map->nr > 0 ? map->map[map->nr - 1] : -1;
}