* [PATCH] c++: use auto_vec in cp_parser_template_argument_list @ 2021-11-09 16:02 Patrick Palka 2021-11-09 16:59 ` Jason Merrill 0 siblings, 1 reply; 5+ messages in thread From: Patrick Palka @ 2021-11-09 16:02 UTC (permalink / raw) To: gcc-patches Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? gcc/cp/ChangeLog: * parser.c (cp_parser_template_argument_list): Use auto_vec instead of manual memory management. --- gcc/cp/parser.c | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 32de97b08bd..8823399529e 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -18558,11 +18558,6 @@ cp_parser_template_name (cp_parser* parser, static tree cp_parser_template_argument_list (cp_parser* parser) { - tree fixed_args[10]; - unsigned n_args = 0; - unsigned alloced = 10; - tree *arg_ary = fixed_args; - tree vec; bool saved_in_template_argument_list_p; bool saved_ice_p; bool saved_non_ice_p; @@ -18581,16 +18576,15 @@ cp_parser_template_argument_list (cp_parser* parser) parser->non_integral_constant_expression_p = false; /* Parse the arguments. */ + auto_vec<tree, 10> args; do { - tree argument; - - if (n_args) + if (!args.is_empty ()) /* Consume the comma. */ cp_lexer_consume_token (parser->lexer); /* Parse the template-argument. */ - argument = cp_parser_template_argument (parser); + tree argument = cp_parser_template_argument (parser); /* If the next token is an ellipsis, we're expanding a template argument pack. */ @@ -18610,29 +18604,16 @@ cp_parser_template_argument_list (cp_parser* parser) argument = make_pack_expansion (argument); } - if (n_args == alloced) - { - alloced *= 2; - - if (arg_ary == fixed_args) - { - arg_ary = XNEWVEC (tree, alloced); - memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); - } - else - arg_ary = XRESIZEVEC (tree, arg_ary, alloced); - } - arg_ary[n_args++] = argument; + args.safe_push (argument); } while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); - vec = make_tree_vec (n_args); + int n_args = args.length (); + tree vec = make_tree_vec (n_args); - while (n_args--) - TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; + for (int i = 0; i < n_args; i++) + TREE_VEC_ELT (vec, i) = args[i]; - if (arg_ary != fixed_args) - free (arg_ary); parser->non_integral_constant_expression_p = saved_non_ice_p; parser->integral_constant_expression_p = saved_ice_p; parser->in_template_argument_list_p = saved_in_template_argument_list_p; -- 2.34.0.rc1.14.g88d915a634 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] c++: use auto_vec in cp_parser_template_argument_list 2021-11-09 16:02 [PATCH] c++: use auto_vec in cp_parser_template_argument_list Patrick Palka @ 2021-11-09 16:59 ` Jason Merrill 2021-11-09 18:42 ` Patrick Palka 0 siblings, 1 reply; 5+ messages in thread From: Jason Merrill @ 2021-11-09 16:59 UTC (permalink / raw) To: Patrick Palka, gcc-patches On 11/9/21 11:02, Patrick Palka wrote: > Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? OK, though I wonder about using releasing_vec instead of auto_vec; reusing a previously allocated vec vs. building one on the stack. > gcc/cp/ChangeLog: > > * parser.c (cp_parser_template_argument_list): Use auto_vec > instead of manual memory management. > --- > gcc/cp/parser.c | 35 ++++++++--------------------------- > 1 file changed, 8 insertions(+), 27 deletions(-) > > diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c > index 32de97b08bd..8823399529e 100644 > --- a/gcc/cp/parser.c > +++ b/gcc/cp/parser.c > @@ -18558,11 +18558,6 @@ cp_parser_template_name (cp_parser* parser, > static tree > cp_parser_template_argument_list (cp_parser* parser) > { > - tree fixed_args[10]; > - unsigned n_args = 0; > - unsigned alloced = 10; > - tree *arg_ary = fixed_args; > - tree vec; > bool saved_in_template_argument_list_p; > bool saved_ice_p; > bool saved_non_ice_p; > @@ -18581,16 +18576,15 @@ cp_parser_template_argument_list (cp_parser* parser) > parser->non_integral_constant_expression_p = false; > > /* Parse the arguments. */ > + auto_vec<tree, 10> args; > do > { > - tree argument; > - > - if (n_args) > + if (!args.is_empty ()) > /* Consume the comma. */ > cp_lexer_consume_token (parser->lexer); > > /* Parse the template-argument. */ > - argument = cp_parser_template_argument (parser); > + tree argument = cp_parser_template_argument (parser); > > /* If the next token is an ellipsis, we're expanding a template > argument pack. */ > @@ -18610,29 +18604,16 @@ cp_parser_template_argument_list (cp_parser* parser) > argument = make_pack_expansion (argument); > } > > - if (n_args == alloced) > - { > - alloced *= 2; > - > - if (arg_ary == fixed_args) > - { > - arg_ary = XNEWVEC (tree, alloced); > - memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); > - } > - else > - arg_ary = XRESIZEVEC (tree, arg_ary, alloced); > - } > - arg_ary[n_args++] = argument; > + args.safe_push (argument); > } > while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); > > - vec = make_tree_vec (n_args); > + int n_args = args.length (); > + tree vec = make_tree_vec (n_args); > > - while (n_args--) > - TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; > + for (int i = 0; i < n_args; i++) > + TREE_VEC_ELT (vec, i) = args[i]; > > - if (arg_ary != fixed_args) > - free (arg_ary); > parser->non_integral_constant_expression_p = saved_non_ice_p; > parser->integral_constant_expression_p = saved_ice_p; > parser->in_template_argument_list_p = saved_in_template_argument_list_p; > ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] c++: use auto_vec in cp_parser_template_argument_list 2021-11-09 16:59 ` Jason Merrill @ 2021-11-09 18:42 ` Patrick Palka 2021-11-10 5:16 ` Jason Merrill 0 siblings, 1 reply; 5+ messages in thread From: Patrick Palka @ 2021-11-09 18:42 UTC (permalink / raw) To: Jason Merrill; +Cc: Patrick Palka, gcc-patches On Tue, 9 Nov 2021, Jason Merrill wrote: > On 11/9/21 11:02, Patrick Palka wrote: > > Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? > > OK, though I wonder about using releasing_vec instead of auto_vec; reusing a > previously allocated vec vs. building one on the stack. Thanks a lot. And hmm, I think using reusing a previously allocated vec here would be tricky since cp_parser_template_argument_list can be called recursively, and so only the outermost call would be able to benefit from reuse unless we perhaps maintain a freelist of such vecs IIUC. Seems like that would complicate the code enough to not be worth it. > > gcc/cp/ChangeLog: > > > > * parser.c (cp_parser_template_argument_list): Use auto_vec > > instead of manual memory management. > > --- > > gcc/cp/parser.c | 35 ++++++++--------------------------- > > 1 file changed, 8 insertions(+), 27 deletions(-) > > > > diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c > > index 32de97b08bd..8823399529e 100644 > > --- a/gcc/cp/parser.c > > +++ b/gcc/cp/parser.c > > @@ -18558,11 +18558,6 @@ cp_parser_template_name (cp_parser* parser, > > static tree > > cp_parser_template_argument_list (cp_parser* parser) > > { > > - tree fixed_args[10]; > > - unsigned n_args = 0; > > - unsigned alloced = 10; > > - tree *arg_ary = fixed_args; > > - tree vec; > > bool saved_in_template_argument_list_p; > > bool saved_ice_p; > > bool saved_non_ice_p; > > @@ -18581,16 +18576,15 @@ cp_parser_template_argument_list (cp_parser* > > parser) > > parser->non_integral_constant_expression_p = false; > > /* Parse the arguments. */ > > + auto_vec<tree, 10> args; > > do > > { > > - tree argument; > > - > > - if (n_args) > > + if (!args.is_empty ()) > > /* Consume the comma. */ > > cp_lexer_consume_token (parser->lexer); > > /* Parse the template-argument. */ > > - argument = cp_parser_template_argument (parser); > > + tree argument = cp_parser_template_argument (parser); > > /* If the next token is an ellipsis, we're expanding a template > > argument pack. */ > > @@ -18610,29 +18604,16 @@ cp_parser_template_argument_list (cp_parser* > > parser) > > argument = make_pack_expansion (argument); > > } > > - if (n_args == alloced) > > - { > > - alloced *= 2; > > - > > - if (arg_ary == fixed_args) > > - { > > - arg_ary = XNEWVEC (tree, alloced); > > - memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); > > - } > > - else > > - arg_ary = XRESIZEVEC (tree, arg_ary, alloced); > > - } > > - arg_ary[n_args++] = argument; > > + args.safe_push (argument); > > } > > while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); > > - vec = make_tree_vec (n_args); > > + int n_args = args.length (); > > + tree vec = make_tree_vec (n_args); > > - while (n_args--) > > - TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; > > + for (int i = 0; i < n_args; i++) > > + TREE_VEC_ELT (vec, i) = args[i]; > > - if (arg_ary != fixed_args) > > - free (arg_ary); > > parser->non_integral_constant_expression_p = saved_non_ice_p; > > parser->integral_constant_expression_p = saved_ice_p; > > parser->in_template_argument_list_p = saved_in_template_argument_list_p; > > > > ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] c++: use auto_vec in cp_parser_template_argument_list 2021-11-09 18:42 ` Patrick Palka @ 2021-11-10 5:16 ` Jason Merrill 2021-11-10 17:13 ` Patrick Palka 0 siblings, 1 reply; 5+ messages in thread From: Jason Merrill @ 2021-11-10 5:16 UTC (permalink / raw) To: Patrick Palka; +Cc: gcc-patches On 11/9/21 13:42, Patrick Palka wrote: > On Tue, 9 Nov 2021, Jason Merrill wrote: > >> On 11/9/21 11:02, Patrick Palka wrote: >>> Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? >> >> OK, though I wonder about using releasing_vec instead of auto_vec; reusing a >> previously allocated vec vs. building one on the stack. > > Thanks a lot. And hmm, I think using reusing a previously allocated vec > here would be tricky since cp_parser_template_argument_list can be > called recursively, and so only the outermost call would be able to > benefit from reuse unless we perhaps maintain a freelist of such vecs > IIUC. We do maintain such a freelist, in make_tree_vector/release_tree_vector, which releasing_vec is a wrapper class for. > Seems like that would complicate the code enough to not be worth > it. > >>> gcc/cp/ChangeLog: >>> >>> * parser.c (cp_parser_template_argument_list): Use auto_vec >>> instead of manual memory management. >>> --- >>> gcc/cp/parser.c | 35 ++++++++--------------------------- >>> 1 file changed, 8 insertions(+), 27 deletions(-) >>> >>> diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c >>> index 32de97b08bd..8823399529e 100644 >>> --- a/gcc/cp/parser.c >>> +++ b/gcc/cp/parser.c >>> @@ -18558,11 +18558,6 @@ cp_parser_template_name (cp_parser* parser, >>> static tree >>> cp_parser_template_argument_list (cp_parser* parser) >>> { >>> - tree fixed_args[10]; >>> - unsigned n_args = 0; >>> - unsigned alloced = 10; >>> - tree *arg_ary = fixed_args; >>> - tree vec; >>> bool saved_in_template_argument_list_p; >>> bool saved_ice_p; >>> bool saved_non_ice_p; >>> @@ -18581,16 +18576,15 @@ cp_parser_template_argument_list (cp_parser* >>> parser) >>> parser->non_integral_constant_expression_p = false; >>> /* Parse the arguments. */ >>> + auto_vec<tree, 10> args; >>> do >>> { >>> - tree argument; >>> - >>> - if (n_args) >>> + if (!args.is_empty ()) >>> /* Consume the comma. */ >>> cp_lexer_consume_token (parser->lexer); >>> /* Parse the template-argument. */ >>> - argument = cp_parser_template_argument (parser); >>> + tree argument = cp_parser_template_argument (parser); >>> /* If the next token is an ellipsis, we're expanding a template >>> argument pack. */ >>> @@ -18610,29 +18604,16 @@ cp_parser_template_argument_list (cp_parser* >>> parser) >>> argument = make_pack_expansion (argument); >>> } >>> - if (n_args == alloced) >>> - { >>> - alloced *= 2; >>> - >>> - if (arg_ary == fixed_args) >>> - { >>> - arg_ary = XNEWVEC (tree, alloced); >>> - memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); >>> - } >>> - else >>> - arg_ary = XRESIZEVEC (tree, arg_ary, alloced); >>> - } >>> - arg_ary[n_args++] = argument; >>> + args.safe_push (argument); >>> } >>> while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); >>> - vec = make_tree_vec (n_args); >>> + int n_args = args.length (); >>> + tree vec = make_tree_vec (n_args); >>> - while (n_args--) >>> - TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; >>> + for (int i = 0; i < n_args; i++) >>> + TREE_VEC_ELT (vec, i) = args[i]; >>> - if (arg_ary != fixed_args) >>> - free (arg_ary); >>> parser->non_integral_constant_expression_p = saved_non_ice_p; >>> parser->integral_constant_expression_p = saved_ice_p; >>> parser->in_template_argument_list_p = saved_in_template_argument_list_p; >>> >> >> > ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] c++: use auto_vec in cp_parser_template_argument_list 2021-11-10 5:16 ` Jason Merrill @ 2021-11-10 17:13 ` Patrick Palka 0 siblings, 0 replies; 5+ messages in thread From: Patrick Palka @ 2021-11-10 17:13 UTC (permalink / raw) To: Jason Merrill; +Cc: GCC Patches On Wed, Nov 10, 2021 at 12:16 AM Jason Merrill <jason@redhat.com> wrote: > > On 11/9/21 13:42, Patrick Palka wrote: > > On Tue, 9 Nov 2021, Jason Merrill wrote: > > > >> On 11/9/21 11:02, Patrick Palka wrote: > >>> Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? > >> > >> OK, though I wonder about using releasing_vec instead of auto_vec; reusing a > >> previously allocated vec vs. building one on the stack. > > > > Thanks a lot. And hmm, I think using reusing a previously allocated vec > > here would be tricky since cp_parser_template_argument_list can be > > called recursively, and so only the outermost call would be able to > > benefit from reuse unless we perhaps maintain a freelist of such vecs > > IIUC. > > We do maintain such a freelist, in make_tree_vector/release_tree_vector, > which releasing_vec is a wrapper class for. Oops sorry, I had totally forgotten about that aspect of make/release_tree_vector. Hmm, so given that a template argument list is very likely to be very small, whereas a vector obtained from the freelist can have an arbitrary large capacity (depending on what it stored for its previous users), it seems more overall resourceful to use a stack-allocated auto_vec here IMHO. > > > Seems like that would complicate the code enough to not be worth > > it. > > > >>> gcc/cp/ChangeLog: > >>> > >>> * parser.c (cp_parser_template_argument_list): Use auto_vec > >>> instead of manual memory management. > >>> --- > >>> gcc/cp/parser.c | 35 ++++++++--------------------------- > >>> 1 file changed, 8 insertions(+), 27 deletions(-) > >>> > >>> diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c > >>> index 32de97b08bd..8823399529e 100644 > >>> --- a/gcc/cp/parser.c > >>> +++ b/gcc/cp/parser.c > >>> @@ -18558,11 +18558,6 @@ cp_parser_template_name (cp_parser* parser, > >>> static tree > >>> cp_parser_template_argument_list (cp_parser* parser) > >>> { > >>> - tree fixed_args[10]; > >>> - unsigned n_args = 0; > >>> - unsigned alloced = 10; > >>> - tree *arg_ary = fixed_args; > >>> - tree vec; > >>> bool saved_in_template_argument_list_p; > >>> bool saved_ice_p; > >>> bool saved_non_ice_p; > >>> @@ -18581,16 +18576,15 @@ cp_parser_template_argument_list (cp_parser* > >>> parser) > >>> parser->non_integral_constant_expression_p = false; > >>> /* Parse the arguments. */ > >>> + auto_vec<tree, 10> args; > >>> do > >>> { > >>> - tree argument; > >>> - > >>> - if (n_args) > >>> + if (!args.is_empty ()) > >>> /* Consume the comma. */ > >>> cp_lexer_consume_token (parser->lexer); > >>> /* Parse the template-argument. */ > >>> - argument = cp_parser_template_argument (parser); > >>> + tree argument = cp_parser_template_argument (parser); > >>> /* If the next token is an ellipsis, we're expanding a template > >>> argument pack. */ > >>> @@ -18610,29 +18604,16 @@ cp_parser_template_argument_list (cp_parser* > >>> parser) > >>> argument = make_pack_expansion (argument); > >>> } > >>> - if (n_args == alloced) > >>> - { > >>> - alloced *= 2; > >>> - > >>> - if (arg_ary == fixed_args) > >>> - { > >>> - arg_ary = XNEWVEC (tree, alloced); > >>> - memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); > >>> - } > >>> - else > >>> - arg_ary = XRESIZEVEC (tree, arg_ary, alloced); > >>> - } > >>> - arg_ary[n_args++] = argument; > >>> + args.safe_push (argument); > >>> } > >>> while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); > >>> - vec = make_tree_vec (n_args); > >>> + int n_args = args.length (); > >>> + tree vec = make_tree_vec (n_args); > >>> - while (n_args--) > >>> - TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; > >>> + for (int i = 0; i < n_args; i++) > >>> + TREE_VEC_ELT (vec, i) = args[i]; > >>> - if (arg_ary != fixed_args) > >>> - free (arg_ary); > >>> parser->non_integral_constant_expression_p = saved_non_ice_p; > >>> parser->integral_constant_expression_p = saved_ice_p; > >>> parser->in_template_argument_list_p = saved_in_template_argument_list_p; > >>> > >> > >> > > > ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-11-10 17:13 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2021-11-09 16:02 [PATCH] c++: use auto_vec in cp_parser_template_argument_list Patrick Palka 2021-11-09 16:59 ` Jason Merrill 2021-11-09 18:42 ` Patrick Palka 2021-11-10 5:16 ` Jason Merrill 2021-11-10 17:13 ` Patrick Palka
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).