use strvec_pushv() to add another strvec
PATCH 1/1 use strvec_pushv() to add another strvec
Simplify the code by letting strvec_pushv() add the items of a second strvec instead of pushing them one by one.
--- fetch-pack.c | 8 ++------ git.c | 3 +-- submodule.c | 4 +--- 3 files changed, 4 insertions(+), 11 deletions(-)
fetch-pack.c +2 -6
@@ -1024,12 +1024,8 @@ static int get_pack(struct fetch_pack_args *args, fsck_msg_types.buf); } -if (index_pack_args) { -int i; - -for (i =0 ; i < cmd.args.nr; i++) -strvec_push (index_pack_args, cmd.args.v[i]); - } +if (index_pack_args) +strvec_pushv (index_pack_args, cmd.args.v);sigchain_push (SIGPIPE ,SIG_IGN );
git.c +1 -2
@@ -877,8 +877,7 @@ static int run_argv(struct strvec *args)commit_pager_choice ();strvec_push (&cmd.args,"git" ); -for (size_t i =0 ; i < args->nr; i++) -strvec_push (&cmd.args, args->v[i]); +strvec_pushv (&cmd.args, args->v);trace_argv_printf (cmd.args.v,"trace: exec:" );
submodule.c +1 -3
@@ -1815,7 +1815,6 @@ int fetch_submodules(struct repository *r,int default_option, int quiet, int max_parallel_jobs) { - int i; struct submodule_parallel_fetch spf = SPF_INIT; const struct run_process_parallel_opts opts = { .tr2_category = "submodule",
@@ -1842,8 +1841,7 @@ int fetch_submodules(struct repository *r,die (_ ("index file corrupt" ));strvec_push (&spf.args,"fetch" ); -for (i =0 ; i < options->nr; i++) -strvec_push (&spf.args, options->v[i]); +strvec_pushv (&spf.args, options->v);strvec_push (&spf.args,"--recurse-submodules-default" );/* default value, "--submodule-prefix" and its value are added later */
Signature · 1 line
2.53.0
Quoted 9 lines · René Scharfe
Simplify the code by letting
strvec_pushv()add the items of a second strvec instead of pushing them one by one.Signed-off-by René Scharfe <[email protected]>--- fetch-pack.c | 8 ++------ git.c | 3 +-- submodule.c | 4 +--- 3 files changed, 4 insertions(+), 11 deletions(-)
Nice. Is this something we can make a coccinelle rule for?
Something like
@@
struct strvec SRC;
struct strvec DST;size_t I;
@@
- for (size_t I = 0; I < SRC.nr; I++)
- strvec_push(&DST, SRC.v[I]);
+ strvec_pushv(&DST, SRC.v);
perhaps?
Will queue.
Quoted 54 lines
fetch-pack.c +2
-6@@ -1024,12 +1024,8 @@ static int get_pack(struct fetch_pack_args *args, fsck_msg_types.buf); } -if (index_pack_args) { -int i; - -for (i =0 ; i < cmd.args.nr; i++) -strvec_push (index_pack_args, cmd.args.v[i]); - } +if (index_pack_args) +strvec_pushv (index_pack_args, cmd.args.v);sigchain_push (SIGPIPE ,SIG_IGN );git.c +1
-2@@ -877,8 +877,7 @@ static int run_argv(struct strvec *args)commit_pager_choice ();strvec_push (&cmd.args,"git" ); -for (size_t i =0 ; i < args->nr; i++) -strvec_push (&cmd.args, args->v[i]); +strvec_pushv (&cmd.args, args->v);trace_argv_printf (cmd.args.v,"trace: exec:" );submodule.c +1
-3@@ -1815,7 +1815,6 @@ int fetch_submodules(struct repository *r,int default_option, int quiet, int max_parallel_jobs) { - int i; struct submodule_parallel_fetch spf = SPF_INIT; const struct run_process_parallel_opts opts = { .tr2_category = "submodule",@@ -1842,8 +1841,7 @@ int fetch_submodules(struct repository *r,die (_ ("index file corrupt" ));strvec_push (&spf.args,"fetch" ); -for (i =0 ; i < options->nr; i++) -strvec_push (&spf.args, options->v[i]); +strvec_pushv (&spf.args, options->v);strvec_push (&spf.args,"--recurse-submodules-default" );/* default value, "--submodule-prefix" and its value are added later */
Quoted 25 lines · Junio C Hamano
Quoted 9 lines · René Scharfe
Simplify the code by letting
strvec_pushv()add the items of a second strvec instead of pushing them one by one.Signed-off-by René Scharfe <[email protected]>--- fetch-pack.c | 8 ++------ git.c | 3 +-- submodule.c | 4 +--- 3 files changed, 4 insertions(+), 11 deletions(-)Nice. Is this something we can make a coccinelle rule for?
Something like
@@
struct strvec SRC;
struct strvec DST;size_tI;
@@(quoted diff) +1
-2- for (size_t I = 0; I < SRC.nr; I++) - strvec_push(&DST, SRC.v[I]); + strvec_pushv(&DST, SRC.v);
Quoted 1 line
perhaps?
We can. Should we? It does find a fourth case from the 18th batch that has landed a few hours ago, at least. Will send as v2.
René
PATCH v2 1/1 use strvec_pushv() to add another strvec
In reply to René ScharfeAdd and apply a semantic patch that simplifies the code by letting strvec_pushv() append the items of a second strvec instead of pushing them one by one.
---
Changes since v1:
- Semantic patch.
- New conversion in builtin/rebase.c.
builtin/rebase.c | 3 +-- contrib/coccinelle/strvec.cocci | 46 +++++++++++++++++++++++++++++++++ fetch-pack.c | 8 ++---- git.c | 3 +-- submodule.c | 4 +-- 5 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 contrib/coccinelle/strvec.cocci
builtin/rebase.c +1 -2
@@ -182,8 +182,7 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts) replay.signoff = opts->signoff; -for (size_t i =0 ; i < opts->trailer_args.nr; i++) -strvec_push (&replay.trailer_args, opts->trailer_args.v[i]); +strvec_pushv (&replay.trailer_args, opts->trailer_args.v); replay.allow_ff = !(opts->flags &REBASE_FORCE );if (opts->allow_rerere_autoupdate)
contrib/coccinelle/strvec.cocci new file +46 -0
46 lines not on this page · raw message
fetch-pack.c +2 -6
@@ -1024,12 +1024,8 @@ static int get_pack(struct fetch_pack_args *args, fsck_msg_types.buf); } -if (index_pack_args) { -int i; - -for (i =0 ; i < cmd.args.nr; i++) -strvec_push (index_pack_args, cmd.args.v[i]); - } +if (index_pack_args) +strvec_pushv (index_pack_args, cmd.args.v);sigchain_push (SIGPIPE ,SIG_IGN );
git.c +1 -2
@@ -877,8 +877,7 @@ static int run_argv(struct strvec *args)commit_pager_choice ();strvec_push (&cmd.args,"git" ); -for (size_t i =0 ; i < args->nr; i++) -strvec_push (&cmd.args, args->v[i]); +strvec_pushv (&cmd.args, args->v);trace_argv_printf (cmd.args.v,"trace: exec:" );
submodule.c +1 -3
@@ -1815,7 +1815,6 @@ int fetch_submodules(struct repository *r,int default_option, int quiet, int max_parallel_jobs) { - int i; struct submodule_parallel_fetch spf = SPF_INIT; const struct run_process_parallel_opts opts = { .tr2_category = "submodule",
@@ -1842,8 +1841,7 @@ int fetch_submodules(struct repository *r,die (_ ("index file corrupt" ));strvec_push (&spf.args,"fetch" ); -for (i =0 ; i < options->nr; i++) -strvec_push (&spf.args, options->v[i]); +strvec_pushv (&spf.args, options->v);strvec_push (&spf.args,"--recurse-submodules-default" );/* default value, "--submodule-prefix" and its value are added later */
Signature · 1 line
2.53.0
Quoted 10 lines · René Scharfe
Add and apply a semantic patch that simplifies the code by letting
strvec_pushv()append the items of a second strvec instead of pushing them one by one.Suggested-by Junio C Hamano <[email protected]>Signed-off-by René Scharfe <[email protected]>---
Changes since v1:
- Semantic patch.
- New conversion inbuiltin/rebase.c.
Thanks. The fixup does not apply to plain vanilla v2.53 but that is OK, as a code-cleanup patch like this is not maint material.
Quoted 27 lines
contrib/coccinelle/strvec.cocci new file +21
-0@@ -0,0 +1,46 @@ +@@ +type T; +identifier i; +expression dst; +struct strvec *src_ptr; +struct strvec src_arr; +@@ +( +- for (T i = 0; i < src_ptr->nr; i++) { strvec_push(dst, src_ptr->v[i]); } ++ strvec_pushv(dst, src_ptr->v); +| +- for (T i = 0; i < src_arr.nr; i++) { strvec_push(dst, src_arr.v[i]); } ++ strvec_pushv(dst, src_arr.v); +) + +@ separate_loop_index @ +type T; +identifier i; +expression dst; +struct strvec *src_ptr; +struct strvec src_arr;
contrib/coccinelle/strvec.cocci line 31
Quoted 10 lines
contrib/coccinelle/strvec.cocci new file +10
-0+@@ + T i; + ... +( +- for (i = 0; i < src_ptr->nr; i++) { strvec_push(dst, src_ptr->v[i]); } ++ strvec_pushv(dst, src_ptr->v); +| +- for (i = 0; i < src_arr.nr; i++) { strvec_push(dst, src_arr.v[i]); } ++ strvec_pushv(dst, src_arr.v); +)
It is a bit unfortunate that we need to write these four cases separately.
contrib/coccinelle/strvec.cocci line 39
Quoted 7 lines
contrib/coccinelle/strvec.cocci new file +7
-0+@ unused_loop_index extends separate_loop_index @ +@@ + { + ... +- T i; + ... when != i + }
I do not grok this one (not an objection, but a statement of fact that I have to look up what "when !=" is doing there and I haven't).
contrib/coccinelle/strvec.cocci line 46
Quoted 6 lines
contrib/coccinelle/strvec.cocci new file +6
-0+@ depends on unused_loop_index @ +@@ + if (...) +- { + strvec_pushv(...); +- }
This is a bit questionable, in that we would probably want to remove excess {} around any simple single-statement block, and not limited to a call to strvec_pushv().
I think it leads to a philosophical question: should Coccinelle rules used in the context of this project aim to produce the ideal result that does not require any human clean-up, or is it OK to make humans notice there is a questionable construction without updating it to the final ideal form? I've been assuming the latter somehow but I do not recall we had a discussion or decision on this point.
Quoted 6 lines
fetch-pack.c +0
-0@@ -1024,12 +1024,8 @@ static int get_pack(struct fetch_pack_args *args, fsck_msg_types.buf);
fetch-pack.c static int get_pack(struct fetch_pack_args *args, · line 1028
Quoted 10 lines
fetch-pack.c +2
-6} -if (index_pack_args) { -int i; - -for (i =0 ; i < cmd.args.nr; i++) -strvec_push (index_pack_args, cmd.args.v[i]); - } +if (index_pack_args) +strvec_pushv (index_pack_args, cmd.args.v);
This does lead to a great result, and I presume that this is the doing of the last two rules?
Quoted 33 lines · Junio C Hamano
Quoted 31 lines · René Scharfe
contrib/coccinelle/strvec.cocci new file +25
-0@@ -0,0 +1,46 @@ +@@ +type T; +identifier i; +expression dst; +struct strvec *src_ptr; +struct strvec src_arr; +@@ +( +- for (T i = 0; i < src_ptr->nr; i++) { strvec_push(dst, src_ptr->v[i]); } ++ strvec_pushv(dst, src_ptr->v); +| +- for (T i = 0; i < src_arr.nr; i++) { strvec_push(dst, src_arr.v[i]); } ++ strvec_pushv(dst, src_arr.v); +) + +@ separate_loop_index @ +type T; +identifier i; +expression dst; +struct strvec *src_ptr; +struct strvec src_arr; +@@ + T i; + ... +(
Quoted 8 lines
contrib/coccinelle/strvec.cocci line 31
Quoted 6 lines
contrib/coccinelle/strvec.cocci new file +6
-0+- for (i = 0; i < src_ptr->nr; i++) { strvec_push(dst, src_ptr->v[i]); } ++ strvec_pushv(dst, src_ptr->v); +| +- for (i = 0; i < src_arr.nr; i++) { strvec_push(dst, src_arr.v[i]); } ++ strvec_pushv(dst, src_arr.v); +)It is a bit unfortunate that we need to write these four cases separately.
An abstraction that matches both struct and struct pointer as well as the appropriate member access operator would be nice.
"_arr" is a misnomer. struct strvec is not an array, even though it contains one and its purpose is to store multiple items.
Quoted 10 lines
contrib/coccinelle/strvec.cocci line 39
Quoted 7 lines
contrib/coccinelle/strvec.cocci new file +7
-0+@ unused_loop_index extends separate_loop_index @ +@@ + { + ... +- T i; + ... when != i + }I do not grok this one (not an objection, but a statement of fact that I have to look up what "when !=" is doing there and I haven't).
This line matches code that doesn't contain i, which is loop counter from the rule above.
Quoted 10 lines
contrib/coccinelle/strvec.cocci line 46
Quoted 6 lines
contrib/coccinelle/strvec.cocci new file +6
-0+@ depends on unused_loop_index @ +@@ + if (...) +- { + strvec_pushv(...); +- }This is a bit questionable, in that we would probably want to remove excess {} around any simple single-statement block, and not limited to a call to
strvec_pushv().
Perhaps, but this rule only exists to mop up after the one it depends on, which can create such a thing.
Even if we had a general rule for that (and I'm not sure if that would be a good idea), it would probably live in a different file. And without this one here we'd need to run coccicheck and apply its results twice -- first for strvec_pushv(), then again for brace removal. Unnecessarily annoying.
Quoted 6 lines
I think it leads to a philosophical question: should Coccinelle rules used in the context of this project aim to produce the ideal result that does not require any human clean-up, or is it OK to make humans notice there is a questionable construction without updating it to the final ideal form? I've been assuming the latter somehow but I do not recall we had a discussion or decision on this point
The README doesn't say, but mentions "cost-benefit ratio" twice.
Without brace-removal I get (with "make clean" between runs):
Benchmark 1: make tools/coccinelle/strvec.cocci.patch
Time (mean ± σ): 18.864 s ± 0.084 s [User: 51.865 s, System: 17.521 s]
Range (min … max): 18.677 s … 18.974 s 10 runs
... and with it:
Benchmark 1: make tools/coccinelle/strvec.cocci.patch
Time (mean ± σ): 19.066 s ± 0.132 s [User: 52.462 s, System: 17.665 s]
Range (min … max): 18.956 s … 19.300 s 10 runs
I think a more interesting question is whether we want to keep the overall rule. If we do then automatic brace-removal doesn't add too much a cost on top.
Quoted 8 lines
Quoted 8 lines
fetch-pack.c +0
-0@@ -1024,12 +1024,8 @@ static int get_pack(struct fetch_pack_args *args, fsck_msg_types.buf); }
Quoted 11 lines
fetch-pack.c static int get_pack(struct fetch_pack_args *args, · line 1028
Quoted 8 lines
fetch-pack.c +2
-6-if (index_pack_args) { -int i; - -for (i =0 ; i < cmd.args.nr; i++) -strvec_push (index_pack_args, cmd.args.v[i]); - } +if (index_pack_args) +strvec_pushv (index_pack_args, cmd.args.v);This does lead to a great result, and I presume that this is the doing of the last two rules?
Right, and the one above them, of course.
René