[v5,19/50] perf maps: Do simple merge if given map doesn't overlap

Message ID 20231127220902.1315692-20-irogers@google.com
State New
Headers
Series Improvements to memory use |

Commit Message

Ian Rogers Nov. 27, 2023, 10:08 p.m. UTC
  Simplify merge in for the simple case of a non-overlapping map.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/maps.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
  

Comments

Namhyung Kim Dec. 5, 2023, 12:06 a.m. UTC | #1
On Mon, Nov 27, 2023 at 2:10 PM Ian Rogers <irogers@google.com> wrote:
>
> Simplify merge in for the simple case of a non-overlapping map.
>
> Signed-off-by: Ian Rogers <irogers@google.com>

A nitpick below.

> ---
>  tools/perf/util/maps.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
>
> diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c
> index 40df08dd9bf3..14e1a169433d 100644
> --- a/tools/perf/util/maps.c
> +++ b/tools/perf/util/maps.c
> @@ -696,9 +696,20 @@ void maps__fixup_end(struct maps *maps)
>  int maps__merge_in(struct maps *kmaps, struct map *new_map)
>  {
>         struct map_rb_node *rb_node;
> +       struct rb_node *first;
> +       bool overlaps;
>         LIST_HEAD(merged);
>         int err = 0;
>
> +       down_read(maps__lock(kmaps));
> +       first = first_ending_after(kmaps, new_map);
> +       overlaps = first &&
> +               map__start(rb_entry(first, struct map_rb_node, rb_node)->map) < map__end(new_map);

Can you please break this like below?

    rb_node = first ? rb_entry(first, struct map_rb_node, rb_node) : NULL;
    overlap = rb_node && map__start(rb_node->map) < map_end(new_map);

Thanks,
Namhyung


> +       up_read(maps__lock(kmaps));
> +
> +       if (!overlaps)
> +               return maps__insert(kmaps, new_map);
> +
>         maps__for_each_entry(kmaps, rb_node) {
>                 struct map *old_map = rb_node->map;
>
> --
> 2.43.0.rc1.413.gea7ed67945-goog
>
  
Ian Rogers Dec. 6, 2023, 11:51 p.m. UTC | #2
On Mon, Dec 4, 2023 at 4:06 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> On Mon, Nov 27, 2023 at 2:10 PM Ian Rogers <irogers@google.com> wrote:
> >
> > Simplify merge in for the simple case of a non-overlapping map.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
>
> A nitpick below.
>
> > ---
> >  tools/perf/util/maps.c | 11 +++++++++++
> >  1 file changed, 11 insertions(+)
> >
> > diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c
> > index 40df08dd9bf3..14e1a169433d 100644
> > --- a/tools/perf/util/maps.c
> > +++ b/tools/perf/util/maps.c
> > @@ -696,9 +696,20 @@ void maps__fixup_end(struct maps *maps)
> >  int maps__merge_in(struct maps *kmaps, struct map *new_map)
> >  {
> >         struct map_rb_node *rb_node;
> > +       struct rb_node *first;
> > +       bool overlaps;
> >         LIST_HEAD(merged);
> >         int err = 0;
> >
> > +       down_read(maps__lock(kmaps));
> > +       first = first_ending_after(kmaps, new_map);
> > +       overlaps = first &&
> > +               map__start(rb_entry(first, struct map_rb_node, rb_node)->map) < map__end(new_map);
>
> Can you please break this like below?
>
>     rb_node = first ? rb_entry(first, struct map_rb_node, rb_node) : NULL;
>     overlap = rb_node && map__start(rb_node->map) < map_end(new_map);

Done in v6.

Thanks,
Ian

> Thanks,
> Namhyung
>
>
> > +       up_read(maps__lock(kmaps));
> > +
> > +       if (!overlaps)
> > +               return maps__insert(kmaps, new_map);
> > +
> >         maps__for_each_entry(kmaps, rb_node) {
> >                 struct map *old_map = rb_node->map;
> >
> > --
> > 2.43.0.rc1.413.gea7ed67945-goog
> >
  

Patch

diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c
index 40df08dd9bf3..14e1a169433d 100644
--- a/tools/perf/util/maps.c
+++ b/tools/perf/util/maps.c
@@ -696,9 +696,20 @@  void maps__fixup_end(struct maps *maps)
 int maps__merge_in(struct maps *kmaps, struct map *new_map)
 {
 	struct map_rb_node *rb_node;
+	struct rb_node *first;
+	bool overlaps;
 	LIST_HEAD(merged);
 	int err = 0;
 
+	down_read(maps__lock(kmaps));
+	first = first_ending_after(kmaps, new_map);
+	overlaps = first &&
+		map__start(rb_entry(first, struct map_rb_node, rb_node)->map) < map__end(new_map);
+	up_read(maps__lock(kmaps));
+
+	if (!overlaps)
+		return maps__insert(kmaps, new_map);
+
 	maps__for_each_entry(kmaps, rb_node) {
 		struct map *old_map = rb_node->map;