diff mbox series

[v2,5/9] perf tools: avoid a malloc for array events

Message ID 20191023005337.196160-6-irogers@google.com
State Not Applicable
Delegated to: David Miller
Headers show
Series Improvements to memory usage by parse events | expand

Commit Message

Ian Rogers Oct. 23, 2019, 12:53 a.m. UTC
Use realloc rather than malloc+memcpy to possibly avoid a memory
allocation when appending array elements.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/parse-events.y | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

Comments

Jiri Olsa Oct. 23, 2019, 8:58 a.m. UTC | #1
On Tue, Oct 22, 2019 at 05:53:33PM -0700, Ian Rogers wrote:
> Use realloc rather than malloc+memcpy to possibly avoid a memory
> allocation when appending array elements.
> 
> Signed-off-by: Ian Rogers <irogers@google.com>

Acked-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka

> ---
>  tools/perf/util/parse-events.y | 8 +++-----
>  1 file changed, 3 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y
> index 26cb65798522..545ab7cefc20 100644
> --- a/tools/perf/util/parse-events.y
> +++ b/tools/perf/util/parse-events.y
> @@ -691,14 +691,12 @@ array_terms ',' array_term
>  	struct parse_events_array new_array;
>  
>  	new_array.nr_ranges = $1.nr_ranges + $3.nr_ranges;
> -	new_array.ranges = malloc(sizeof(new_array.ranges[0]) *
> -				  new_array.nr_ranges);
> +	new_array.ranges = realloc($1.ranges,
> +				sizeof(new_array.ranges[0]) *
> +				new_array.nr_ranges);
>  	ABORT_ON(!new_array.ranges);
> -	memcpy(&new_array.ranges[0], $1.ranges,
> -	       $1.nr_ranges * sizeof(new_array.ranges[0]));
>  	memcpy(&new_array.ranges[$1.nr_ranges], $3.ranges,
>  	       $3.nr_ranges * sizeof(new_array.ranges[0]));
> -	free($1.ranges);
>  	free($3.ranges);
>  	$$ = new_array;
>  }
> -- 
> 2.23.0.866.gb869b98d4c-goog
>
Arnaldo Carvalho de Melo Oct. 23, 2019, 12:38 p.m. UTC | #2
Em Wed, Oct 23, 2019 at 10:58:30AM +0200, Jiri Olsa escreveu:
> On Tue, Oct 22, 2019 at 05:53:33PM -0700, Ian Rogers wrote:
> > Use realloc rather than malloc+memcpy to possibly avoid a memory
> > allocation when appending array elements.
> > 
> > Signed-off-by: Ian Rogers <irogers@google.com>
> 
> Acked-by: Jiri Olsa <jolsa@kernel.org>

Thanks, applied,

- Arnaldo
diff mbox series

Patch

diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y
index 26cb65798522..545ab7cefc20 100644
--- a/tools/perf/util/parse-events.y
+++ b/tools/perf/util/parse-events.y
@@ -691,14 +691,12 @@  array_terms ',' array_term
 	struct parse_events_array new_array;
 
 	new_array.nr_ranges = $1.nr_ranges + $3.nr_ranges;
-	new_array.ranges = malloc(sizeof(new_array.ranges[0]) *
-				  new_array.nr_ranges);
+	new_array.ranges = realloc($1.ranges,
+				sizeof(new_array.ranges[0]) *
+				new_array.nr_ranges);
 	ABORT_ON(!new_array.ranges);
-	memcpy(&new_array.ranges[0], $1.ranges,
-	       $1.nr_ranges * sizeof(new_array.ranges[0]));
 	memcpy(&new_array.ranges[$1.nr_ranges], $3.ranges,
 	       $3.nr_ranges * sizeof(new_array.ranges[0]));
-	free($1.ranges);
 	free($3.ranges);
 	$$ = new_array;
 }