{"thread":{"id":"59110","subject":"[PATCH] git: replace strbuf_addstr with strbuf_addch for all strings of length 2","startedAt":"2023-01-17T22:41:24Z","lastAt":"2024-12-18T03:06:16Z","messageCount":6,"participants":["Rose via GitGitGadget","Junio C Hamano","Eric Sunshine","AreaZR via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"470549","messageId":"pull.1436.git.git.1673992498572.gitgitgadget@gmail.com","threadId":"59110","inReplyTo":null,"subject":"[PATCH] git: replace strbuf_addstr with strbuf_addch for all strings of length 2","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-01-17T21:54:58Z","receivedAt":"2023-01-17T22:41:24Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nThis helps reduce overhead of calculating the length\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    git: replace strbuf_addstr with strbuf_addch for all strings of length 2\n    \n    This helps reduce overhead of calculating the length\n    \n    Signed-off-by: Seija Kijin doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1436%2FAtariDreams%2Fstrbuf-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1436/AtariDreams/strbuf-v1\nPull-Request: https://github.com/git/git/pull/1436\n\n bisect.c                  |  2 +-\n builtin/am.c              |  6 ++----\n builtin/blame.c           |  8 +++-----\n builtin/ls-tree.c         |  2 +-\n diff.c                    |  2 +-\n log-tree.c                |  2 +-\n merge-ort.c               |  3 +--\n path.c                    |  2 +-\n protocol-caps.c           |  2 +-\n reftable/readwrite_test.c |  2 +-\n reftable/refname.c        |  2 +-\n reftable/stack.c          | 12 ++++++------\n reftable/stack_test.c     |  2 +-\n reftable/writer.c         |  2 +-\n sequencer.c               |  2 +-\n setup.c                   |  2 +-\n trace2/tr2_tgt_normal.c   |  2 +-\n 17 files changed, 25 insertions(+), 30 deletions(-)\n\ndiff --git a/bisect.c b/bisect.c\nindex ec7487e6836..ea534ad3777 100644\n--- a/bisect.c\n+++ b/bisect.c\n@@ -443,7 +443,7 @@ static int register_ref(const char *refname, const struct object_id *oid,\n {\n \tstruct strbuf good_prefix = STRBUF_INIT;\n \tstrbuf_addstr(&good_prefix, term_good);\n-\tstrbuf_addstr(&good_prefix, \"-\");\n+\tstrbuf_addch(&good_prefix, '-');\n \n \tif (!strcmp(refname, term_bad)) {\n \t\tcurrent_bad_oid = xmalloc(sizeof(*current_bad_oid));\ndiff --git a/builtin/am.c b/builtin/am.c\nindex 7e88d2426d7..c96886e0433 100644\n--- a/builtin/am.c\n+++ b/builtin/am.c\n@@ -329,13 +329,11 @@ static void write_author_script(const struct am_state *state)\n \n \tstrbuf_addstr(&sb, \"GIT_AUTHOR_NAME=\");\n \tsq_quote_buf(&sb, state->author_name);\n-\tstrbuf_addch(&sb, '\\n');\n \n-\tstrbuf_addstr(&sb, \"GIT_AUTHOR_EMAIL=\");\n+\tstrbuf_addstr(&sb, \"\\nGIT_AUTHOR_EMAIL=\");\n \tsq_quote_buf(&sb, state->author_email);\n-\tstrbuf_addch(&sb, '\\n');\n \n-\tstrbuf_addstr(&sb, \"GIT_AUTHOR_DATE=\");\n+\tstrbuf_addstr(&sb, \"\\nGIT_AUTHOR_DATE=\");\n \tsq_quote_buf(&sb, state->author_date);\n \tstrbuf_addch(&sb, '\\n');\n \ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex 71f925e456c..3ab4cc0a56b 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -143,11 +143,9 @@ static void get_ac_line(const char *inbuf, const char *what,\n \n \tif (split_ident_line(&ident, tmp, len)) {\n \terror_out:\n-\t\t/* Ugh */\n-\t\ttmp = \"(unknown)\";\n-\t\tstrbuf_addstr(name, tmp);\n-\t\tstrbuf_addstr(mail, tmp);\n-\t\tstrbuf_addstr(tz, tmp);\n+\t\tstrbuf_addstr(name, \"(unknown)\");\n+\t\tstrbuf_addstr(mail, \"(unknown)\");\n+\t\tstrbuf_addstr(tz, \"(unknown)\");\n \t\t*time = 0;\n \t\treturn;\n \t}\ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex c3ea09281af..73b755029ee 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -60,7 +60,7 @@ static void expand_objectsize(struct strbuf *line, const struct object_id *oid,\n \t} else if (padded) {\n \t\tstrbuf_addf(line, \"%7s\", \"-\");\n \t} else {\n-\t\tstrbuf_addstr(line, \"-\");\n+\t\tstrbuf_addch(line, '-');\n \t}\n }\n \ndiff --git a/diff.c b/diff.c\nindex 329eebf16a0..b379660c42b 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -1702,7 +1702,7 @@ static void add_line_count(struct strbuf *out, int count)\n \t\tstrbuf_addstr(out, \"0,0\");\n \t\tbreak;\n \tcase 1:\n-\t\tstrbuf_addstr(out, \"1\");\n+\t\tstrbuf_addch(out, '1');\n \t\tbreak;\n \tdefault:\n \t\tstrbuf_addf(out, \"1,%d\", count);\ndiff --git a/log-tree.c b/log-tree.c\nindex 1dd5fcbf7be..23f2a62c5ac 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -388,7 +388,7 @@ void fmt_output_subject(struct strbuf *filename,\n \n \t\tstrbuf_addf(&temp, \"v%s\", info->reroll_count);\n \t\tformat_sanitized_subject(filename, temp.buf, temp.len);\n-\t\tstrbuf_addstr(filename, \"-\");\n+\t\tstrbuf_addch(filename, '-');\n \t\tstrbuf_release(&temp);\n \t}\n \tstrbuf_addf(filename, \"%04d-%s\", nr, subject);\ndiff --git a/merge-ort.c b/merge-ort.c\nindex d1611ca400a..3132ac22aba 100644\n--- a/merge-ort.c\n+++ b/merge-ort.c\n@@ -801,8 +801,7 @@ static void path_msg(struct merge_options *opt,\n \n \tva_start(ap, fmt);\n \tif (opt->priv->call_depth) {\n-\t\tstrbuf_addchars(dest, ' ', 2);\n-\t\tstrbuf_addstr(dest, \"From inner merge:\");\n+\t\tstrbuf_addstr(dest, \"  From inner merge:\");\n \t\tstrbuf_addchars(dest, ' ', opt->priv->call_depth * 2);\n \t}\n \tstrbuf_vaddf(dest, fmt, ap);\ndiff --git a/path.c b/path.c\nindex 492e17ad121..05d3b6d9059 100644\n--- a/path.c\n+++ b/path.c\n@@ -1082,7 +1082,7 @@ const char *remove_leading_path(const char *in, const char *prefix)\n \n \tstrbuf_reset(&buf);\n \tif (!in[j])\n-\t\tstrbuf_addstr(&buf, \".\");\n+\t\tstrbuf_addch(&buf, '.');\n \telse\n \t\tstrbuf_addstr(&buf, in + j);\n \treturn buf.buf;\ndiff --git a/protocol-caps.c b/protocol-caps.c\nindex bbde91810ac..80ec75e1131 100644\n--- a/protocol-caps.c\n+++ b/protocol-caps.c\n@@ -63,7 +63,7 @@ static void send_info(struct repository *r, struct packet_writer *writer,\n \n \t\tif (info->size) {\n \t\t\tif (oid_object_info(r, &oid, &object_size) < 0) {\n-\t\t\t\tstrbuf_addstr(&send_buffer, \" \");\n+\t\t\t\tstrbuf_addch(&send_buffer, ' ');\n \t\t\t} else {\n \t\t\t\tstrbuf_addf(&send_buffer, \" %lu\", object_size);\n \t\t\t}\ndiff --git a/reftable/readwrite_test.c b/reftable/readwrite_test.c\nindex 469ab79a5ad..ed0f6058ba9 100644\n--- a/reftable/readwrite_test.c\n+++ b/reftable/readwrite_test.c\n@@ -483,7 +483,7 @@ static void test_table_read_write_seek(int index, int hash_id)\n \t}\n \n \tstrbuf_addstr(&pastLast, names[N - 1]);\n-\tstrbuf_addstr(&pastLast, \"/\");\n+\tstrbuf_addch(&pastLast, '/');\n \n \terr = reftable_reader_seek_ref(&rd, &it, pastLast.buf);\n \tif (err == 0) {\ndiff --git a/reftable/refname.c b/reftable/refname.c\nindex 95734969324..b6d5b76a8fe 100644\n--- a/reftable/refname.c\n+++ b/reftable/refname.c\n@@ -179,7 +179,7 @@ int modification_validate(struct modification *mod)\n \t\t\tgoto done;\n \t\tstrbuf_reset(&slashed);\n \t\tstrbuf_addstr(&slashed, mod->add[i]);\n-\t\tstrbuf_addstr(&slashed, \"/\");\n+\t\tstrbuf_addch(&slashed, '/');\n \n \t\terr = modification_has_ref_with_prefix(mod, slashed.buf);\n \t\tif (err == 0) {\ndiff --git a/reftable/stack.c b/reftable/stack.c\nindex ddbdf1b9c8b..479658e428d 100644\n--- a/reftable/stack.c\n+++ b/reftable/stack.c\n@@ -35,7 +35,7 @@ static void stack_filename(struct strbuf *dest, struct reftable_stack *st,\n {\n \tstrbuf_reset(dest);\n \tstrbuf_addstr(dest, st->reftable_dir);\n-\tstrbuf_addstr(dest, \"/\");\n+\tstrbuf_addch(dest, '/');\n \tstrbuf_addstr(dest, name);\n }\n \n@@ -547,11 +547,11 @@ int reftable_addition_commit(struct reftable_addition *add)\n \n \tfor (i = 0; i < add->stack->merged->stack_len; i++) {\n \t\tstrbuf_addstr(&table_list, add->stack->readers[i]->name);\n-\t\tstrbuf_addstr(&table_list, \"\\n\");\n+\t\tstrbuf_addch(&table_list, '\\n');\n \t}\n \tfor (i = 0; i < add->new_tables_len; i++) {\n \t\tstrbuf_addstr(&table_list, add->new_tables[i]);\n-\t\tstrbuf_addstr(&table_list, \"\\n\");\n+\t\tstrbuf_addch(&table_list, '\\n');\n \t}\n \n \terr = write(add->lock_file_fd, table_list.buf, table_list.len);\n@@ -1013,15 +1013,15 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last,\n \n \tfor (i = 0; i < first; i++) {\n \t\tstrbuf_addstr(&ref_list_contents, st->readers[i]->name);\n-\t\tstrbuf_addstr(&ref_list_contents, \"\\n\");\n+\t\tstrbuf_addch(&ref_list_contents, '\\n');\n \t}\n \tif (!is_empty_table) {\n \t\tstrbuf_addbuf(&ref_list_contents, &new_table_name);\n-\t\tstrbuf_addstr(&ref_list_contents, \"\\n\");\n+\t\tstrbuf_addch(&ref_list_contents, '\\n');\n \t}\n \tfor (i = last + 1; i < st->merged->stack_len; i++) {\n \t\tstrbuf_addstr(&ref_list_contents, st->readers[i]->name);\n-\t\tstrbuf_addstr(&ref_list_contents, \"\\n\");\n+\t\tstrbuf_addch(&ref_list_contents, '\\n');\n \t}\n \n \terr = write(lock_file_fd, ref_list_contents.buf, ref_list_contents.len);\ndiff --git a/reftable/stack_test.c b/reftable/stack_test.c\nindex d0b717510fa..4fcaefd3ddb 100644\n--- a/reftable/stack_test.c\n+++ b/reftable/stack_test.c\n@@ -181,7 +181,7 @@ static void test_reftable_stack_add_one(void)\n \n \tstrbuf_reset(&scratch);\n \tstrbuf_addstr(&scratch, dir);\n-\tstrbuf_addstr(&scratch, \"/\");\n+\tstrbuf_addch(&scratch, '/');\n \t/* do not try at home; not an external API for reftable. */\n \tstrbuf_addstr(&scratch, st->readers[0]->name);\n \terr = stat(scratch.buf, &stat_result);\ndiff --git a/reftable/writer.c b/reftable/writer.c\nindex 2e322a5683d..61d6f3229f3 100644\n--- a/reftable/writer.c\n+++ b/reftable/writer.c\n@@ -351,7 +351,7 @@ int reftable_writer_add_log(struct reftable_writer *w,\n \t\t\terr = REFTABLE_API_ERROR;\n \t\t\tgoto done;\n \t\t}\n-\t\tstrbuf_addstr(&cleaned_message, \"\\n\");\n+\t\tstrbuf_addch(&cleaned_message, '\\n');\n \t\tlog->value.update.message = cleaned_message.buf;\n \t}\n \ndiff --git a/sequencer.c b/sequencer.c\nindex bcb662e23be..27f41a027d2 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -2233,7 +2233,7 @@ static int do_pick_commit(struct repository *r,\n \t\t} else {\n \t\t\tstrbuf_addstr(&msgbuf, \"Revert \\\"\");\n \t\t\tstrbuf_addstr(&msgbuf, msg.subject);\n-\t\t\tstrbuf_addstr(&msgbuf, \"\\\"\");\n+\t\t\tstrbuf_addch(&msgbuf, '\\\"');\n \t\t}\n \t\tstrbuf_addstr(&msgbuf, \"\\n\\nThis reverts commit \");\n \t\trefer_to_commit(opts, &msgbuf, commit);\ndiff --git a/setup.c b/setup.c\nindex cefd5f63c46..865ed8fbe98 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1349,7 +1349,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,\n \t\t\t\treturn GIT_DIR_DISALLOWED_BARE;\n \t\t\tif (!ensure_valid_ownership(NULL, NULL, dir->buf, report))\n \t\t\t\treturn GIT_DIR_INVALID_OWNERSHIP;\n-\t\t\tstrbuf_addstr(gitdir, \".\");\n+\t\t\tstrbuf_addch(gitdir, '.');\n \t\t\treturn GIT_DIR_BARE;\n \t\t}\n \ndiff --git a/trace2/tr2_tgt_normal.c b/trace2/tr2_tgt_normal.c\nindex fbbef68dfc0..e99ee58ca38 100644\n--- a/trace2/tr2_tgt_normal.c\n+++ b/trace2/tr2_tgt_normal.c\n@@ -224,7 +224,7 @@ static void fn_child_start_fl(const char *file, int line,\n \tif (cmd->dir) {\n \t\tstrbuf_addstr(&buf_payload, \" cd \");\n \t\tsq_quote_buf_pretty(&buf_payload, cmd->dir);\n-\t\tstrbuf_addstr(&buf_payload, \";\");\n+\t\tstrbuf_addch(&buf_payload, ';');\n \t}\n \n \t/*\n\nbase-commit: a7caae2729742fc80147bca1c02ae848cb55921a\n-- \ngitgitgadget\n"},{"id":"470627","messageId":"xmqqr0vr6d80.fsf@gitster.g","threadId":"59110","inReplyTo":"pull.1436.git.git.1673992498572.gitgitgadget@gmail.com","subject":"Re: [PATCH] git: replace strbuf_addstr with strbuf_addch for all strings of length 2","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-01-18T16:04:31Z","receivedAt":"2023-01-18T16:09:01Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Rose via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Seija Kijin <doremylover123@gmail.com>\n>\n> This helps reduce overhead of calculating the length\n\nHave you measured how much overhead this change is saving, or is\nthis a 300 line e-mail message that churns code without giving us\nany measurable improvement?\n\nThere also seem to be some totally unrelated changes of dubious\nmerit hidden in these patches (see below).\n\n> Subject: Re: [PATCH] git: replace strbuf_addstr with strbuf_addch for all strings of length 2\n\nI think you are touching strings of length 1, not 2.  Running\nstrlen() on such a string returns will return 1 without counting the\nterminating NUL.\n\n> diff --git a/builtin/am.c b/builtin/am.c\n> index 7e88d2426d7..c96886e0433 100644\n> --- a/builtin/am.c\n> +++ b/builtin/am.c\n> @@ -329,13 +329,11 @@ static void write_author_script(const struct am_state *state)\n>  \n>  \tstrbuf_addstr(&sb, \"GIT_AUTHOR_NAME=\");\n>  \tsq_quote_buf(&sb, state->author_name);\n> -\tstrbuf_addch(&sb, '\\n');\n>  \n> -\tstrbuf_addstr(&sb, \"GIT_AUTHOR_EMAIL=\");\n> +\tstrbuf_addstr(&sb, \"\\nGIT_AUTHOR_EMAIL=\");\n>  \tsq_quote_buf(&sb, state->author_email);\n> -\tstrbuf_addch(&sb, '\\n');\n>  \n> -\tstrbuf_addstr(&sb, \"GIT_AUTHOR_DATE=\");\n> +\tstrbuf_addstr(&sb, \"\\nGIT_AUTHOR_DATE=\");\n>  \tsq_quote_buf(&sb, state->author_date);\n>  \tstrbuf_addch(&sb, '\\n');\n\nThis may reduce the number of lines, but markedly worsens the\nreadability of the resulting code.  Each of the three-line blocks in\nthe original used to be logically complete and independent unit, but\nnow each of them depend on what the last block wants.\n\nIn any case, this has nothing to do with \"addstr() of constant string\ncan be replaced with add() with a constant length or addch() of a\nsing character to make it unnecessary to compute the length\".\n\nBy the way, the current implementation of strbuf_addstr() looks like\nthis:\n\n        static inline void strbuf_addstr(struct strbuf *sb, const char *s)\n        {\n                strbuf_add(sb, s, strlen(s));\n        }\n\nDecent optimizing compilers should be able to see through the code\nlike this you write:\n\n\tstrbuf_addstr(&sb, \"constant\");\n\nwhich becomes\n\n\tstrbuf_add(&sb, \"constant\", strlen(\"constant\"))\n\nwhen inlined, and realize strlen(\"constant\") can be computed at\ncompile time, turning it into\n\n\tstrbuf_add(&sb, \"constant\", 8);\n\nThat way, people can make their strbuf_addstr() calls in the way\nthey find the most natural, i.e. without having to choose between\n_addstr() and _add().  If the compilers can do their unnecessary\nthinking for us humans, we should make them do so to help us.\n\nIf somebody can prove that between these two\n\n\tstrbuf_add(&sb, \"c\", 1);\n\tstrbuf_addch(&sb, 'c');\n\nthere is a meaningful difference in overhead to encourage us to\nrewrite the former to the latter, perhaps a similar trick can be\nemployed in the implementation of strbuf_add(), perhaps like:\n\n        static inline void strbuf_add(struct strbuf *sb, const void *data, size_t len)\n        {\n                if (len == 1) {\n                        strbuf_addch(sb, *((char *)sb));\n                } else {\n                        strbuf_grow(sb, len);\n                        memcpy(sb->buf + sb->len, data, len);\n                        strbuf_setlen(sb, sb->len + len);\n                }\n        }\n\nThat way, people can make their strbuf_add() and strbuf_addstr()\ncalls in the way they find the most natural, i.e. without having to\nchoose between _add() and _addch() depending on the length of the\nstring.  This makes the code easier to maintain, as we do not have\nto change the code all that much when the length of the string we\nneed to append to a strbuf changes from 1 to more (or the other way\naround).\n\nBut I somehow doubt it is worth it.\n\n> diff --git a/builtin/blame.c b/builtin/blame.c\n> index 71f925e456c..3ab4cc0a56b 100644\n> --- a/builtin/blame.c\n> +++ b/builtin/blame.c\n> @@ -143,11 +143,9 @@ static void get_ac_line(const char *inbuf, const char *what,\n>  \n>  \tif (split_ident_line(&ident, tmp, len)) {\n>  \terror_out:\n> -\t\t/* Ugh */\n> -\t\ttmp = \"(unknown)\";\n> -\t\tstrbuf_addstr(name, tmp);\n> -\t\tstrbuf_addstr(mail, tmp);\n> -\t\tstrbuf_addstr(tz, tmp);\n> +\t\tstrbuf_addstr(name, \"(unknown)\");\n> +\t\tstrbuf_addstr(mail, \"(unknown)\");\n> +\t\tstrbuf_addstr(tz, \"(unknown)\");\n\nThis is another unrelated change that has not much to do with the\ntheme of the change, to use addch() when the string is of length 1.\n\n>  \tif (opt->priv->call_depth) {\n> -\t\tstrbuf_addchars(dest, ' ', 2);\n> -\t\tstrbuf_addstr(dest, \"From inner merge:\");\n> +\t\tstrbuf_addstr(dest, \"  From inner merge:\");\n>  \t\tstrbuf_addchars(dest, ' ', opt->priv->call_depth * 2);\n\nDitto, even though this is not as horrible as the change to builtin/am.c\nwe saw earlier.\n\nI'll stop here.\n"},{"id":"470655","messageId":"CAPig+cTrc7V+P6h039EsPwwRE4rw-qYGKHgGnGmp0TohfzHykA@mail.gmail.com","threadId":"59110","inReplyTo":"xmqqr0vr6d80.fsf@gitster.g","subject":"Re: [PATCH] git: replace strbuf_addstr with strbuf_addch for all strings of length 2","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2023-01-18T18:53:07Z","receivedAt":"2023-01-18T18:53:22Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Wed, Jan 18, 2023 at 11:14 AM Junio C Hamano <gitster@pobox.com> wrote:\n> > From: Seija Kijin <doremylover123@gmail.com>\n> > This helps reduce overhead of calculating the length\n>\n> > diff --git a/builtin/am.c b/builtin/am.c\n> >       strbuf_addstr(&sb, \"GIT_AUTHOR_NAME=\");\n> >       sq_quote_buf(&sb, state->author_name);\n> > -     strbuf_addch(&sb, '\\n');\n> >\n> > -     strbuf_addstr(&sb, \"GIT_AUTHOR_EMAIL=\");\n> > +     strbuf_addstr(&sb, \"\\nGIT_AUTHOR_EMAIL=\");\n>\n> This may reduce the number of lines, but markedly worsens the\n> readability of the resulting code.  Each of the three-line blocks in\n> the original used to be logically complete and independent unit, but\n> now each of them depend on what the last block wants.\n\nVery much agree with this and all your other review comments.\n\n> > -             strbuf_addchars(dest, ' ', 2);\n> > -             strbuf_addstr(dest, \"From inner merge:\");\n> > +             strbuf_addstr(dest, \"  From inner merge:\");\n> >               strbuf_addchars(dest, ' ', opt->priv->call_depth * 2);\n>\n> Ditto, even though this is not as horrible as the change to builtin/am.c\n> we saw earlier.\n\nAdditionally, if this literal string ever gets wrapped in `_(...)`,\nthen the above change is even more undesirable due to the extra burden\nit places on translators.\n"},{"id":"509250","messageId":"pull.1436.v2.git.git.1734481897905.gitgitgadget@gmail.com","threadId":"59110","inReplyTo":"pull.1436.git.git.1673992498572.gitgitgadget@gmail.com","subject":"[PATCH v2] git: replace strbuf_addstr with strbuf_addch for all strings of length 2","fromName":"AreaZR via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-12-18T00:31:37Z","receivedAt":"2024-12-18T00:31:41Z","isPatch":true,"sender":{"key":"name:AreaZR","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nThis helps reduce overhead of calculating the length\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    git: replace strbuf_addstr with strbuf_addch for all strings of length 2\n    \n    This helps reduce overhead of calculating the length\n    \n    Signed-off-by: Seija Kijin doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1436%2FAreaZR%2Fstrbuf-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1436/AreaZR/strbuf-v2\nPull-Request: https://github.com/git/git/pull/1436\n\nRange-diff vs v1:\n\n 1:  8eed09a9df1 ! 1:  22c37ed6677 git: replace strbuf_addstr with strbuf_addch for all strings of length 2\n     @@ Commit message\n          Signed-off-by: Seija Kijin <doremylover123@gmail.com>\n      \n       ## bisect.c ##\n     -@@ bisect.c: static int register_ref(const char *refname, const struct object_id *oid,\n     +@@ bisect.c: static int register_ref(const char *refname, const char *referent UNUSED, const\n       {\n       \tstruct strbuf good_prefix = STRBUF_INIT;\n       \tstrbuf_addstr(&good_prefix, term_good);\n     @@ bisect.c: static int register_ref(const char *refname, const struct object_id *o\n      +\tstrbuf_addch(&good_prefix, '-');\n       \n       \tif (!strcmp(refname, term_bad)) {\n     - \t\tcurrent_bad_oid = xmalloc(sizeof(*current_bad_oid));\n     + \t\tfree(current_bad_oid);\n      \n       ## builtin/am.c ##\n      @@ builtin/am.c: static void write_author_script(const struct am_state *state)\n     @@ protocol-caps.c: static void send_info(struct repository *r, struct packet_write\n       \t\t\t\tstrbuf_addf(&send_buffer, \" %lu\", object_size);\n       \t\t\t}\n      \n     - ## reftable/readwrite_test.c ##\n     -@@ reftable/readwrite_test.c: static void test_table_read_write_seek(int index, int hash_id)\n     - \t}\n     - \n     - \tstrbuf_addstr(&pastLast, names[N - 1]);\n     --\tstrbuf_addstr(&pastLast, \"/\");\n     -+\tstrbuf_addch(&pastLast, '/');\n     - \n     - \terr = reftable_reader_seek_ref(&rd, &it, pastLast.buf);\n     - \tif (err == 0) {\n     -\n     - ## reftable/refname.c ##\n     -@@ reftable/refname.c: int modification_validate(struct modification *mod)\n     - \t\t\tgoto done;\n     - \t\tstrbuf_reset(&slashed);\n     - \t\tstrbuf_addstr(&slashed, mod->add[i]);\n     --\t\tstrbuf_addstr(&slashed, \"/\");\n     -+\t\tstrbuf_addch(&slashed, '/');\n     - \n     - \t\terr = modification_has_ref_with_prefix(mod, slashed.buf);\n     - \t\tif (err == 0) {\n     -\n     - ## reftable/stack.c ##\n     -@@ reftable/stack.c: static void stack_filename(struct strbuf *dest, struct reftable_stack *st,\n     - {\n     - \tstrbuf_reset(dest);\n     - \tstrbuf_addstr(dest, st->reftable_dir);\n     --\tstrbuf_addstr(dest, \"/\");\n     -+\tstrbuf_addch(dest, '/');\n     - \tstrbuf_addstr(dest, name);\n     - }\n     - \n     -@@ reftable/stack.c: int reftable_addition_commit(struct reftable_addition *add)\n     - \n     - \tfor (i = 0; i < add->stack->merged->stack_len; i++) {\n     - \t\tstrbuf_addstr(&table_list, add->stack->readers[i]->name);\n     --\t\tstrbuf_addstr(&table_list, \"\\n\");\n     -+\t\tstrbuf_addch(&table_list, '\\n');\n     - \t}\n     - \tfor (i = 0; i < add->new_tables_len; i++) {\n     - \t\tstrbuf_addstr(&table_list, add->new_tables[i]);\n     --\t\tstrbuf_addstr(&table_list, \"\\n\");\n     -+\t\tstrbuf_addch(&table_list, '\\n');\n     - \t}\n     - \n     - \terr = write(add->lock_file_fd, table_list.buf, table_list.len);\n     -@@ reftable/stack.c: static int stack_compact_range(struct reftable_stack *st, int first, int last,\n     - \n     - \tfor (i = 0; i < first; i++) {\n     - \t\tstrbuf_addstr(&ref_list_contents, st->readers[i]->name);\n     --\t\tstrbuf_addstr(&ref_list_contents, \"\\n\");\n     -+\t\tstrbuf_addch(&ref_list_contents, '\\n');\n     - \t}\n     - \tif (!is_empty_table) {\n     - \t\tstrbuf_addbuf(&ref_list_contents, &new_table_name);\n     --\t\tstrbuf_addstr(&ref_list_contents, \"\\n\");\n     -+\t\tstrbuf_addch(&ref_list_contents, '\\n');\n     - \t}\n     - \tfor (i = last + 1; i < st->merged->stack_len; i++) {\n     - \t\tstrbuf_addstr(&ref_list_contents, st->readers[i]->name);\n     --\t\tstrbuf_addstr(&ref_list_contents, \"\\n\");\n     -+\t\tstrbuf_addch(&ref_list_contents, '\\n');\n     - \t}\n     - \n     - \terr = write(lock_file_fd, ref_list_contents.buf, ref_list_contents.len);\n     -\n     - ## reftable/stack_test.c ##\n     -@@ reftable/stack_test.c: static void test_reftable_stack_add_one(void)\n     - \n     - \tstrbuf_reset(&scratch);\n     - \tstrbuf_addstr(&scratch, dir);\n     --\tstrbuf_addstr(&scratch, \"/\");\n     -+\tstrbuf_addch(&scratch, '/');\n     - \t/* do not try at home; not an external API for reftable. */\n     - \tstrbuf_addstr(&scratch, st->readers[0]->name);\n     - \terr = stat(scratch.buf, &stat_result);\n     -\n     - ## reftable/writer.c ##\n     -@@ reftable/writer.c: int reftable_writer_add_log(struct reftable_writer *w,\n     - \t\t\terr = REFTABLE_API_ERROR;\n     - \t\t\tgoto done;\n     - \t\t}\n     --\t\tstrbuf_addstr(&cleaned_message, \"\\n\");\n     -+\t\tstrbuf_addch(&cleaned_message, '\\n');\n     - \t\tlog->value.update.message = cleaned_message.buf;\n     - \t}\n     - \n     -\n     - ## sequencer.c ##\n     -@@ sequencer.c: static int do_pick_commit(struct repository *r,\n     - \t\t} else {\n     - \t\t\tstrbuf_addstr(&msgbuf, \"Revert \\\"\");\n     - \t\t\tstrbuf_addstr(&msgbuf, msg.subject);\n     --\t\t\tstrbuf_addstr(&msgbuf, \"\\\"\");\n     -+\t\t\tstrbuf_addch(&msgbuf, '\\\"');\n     - \t\t}\n     - \t\tstrbuf_addstr(&msgbuf, \"\\n\\nThis reverts commit \");\n     - \t\trefer_to_commit(opts, &msgbuf, commit);\n     -\n       ## setup.c ##\n      @@ setup.c: static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,\n       \t\t\t\treturn GIT_DIR_DISALLOWED_BARE;\n\n\n bisect.c                | 2 +-\n builtin/am.c            | 6 ++----\n builtin/blame.c         | 8 +++-----\n builtin/ls-tree.c       | 2 +-\n diff.c                  | 2 +-\n log-tree.c              | 2 +-\n merge-ort.c             | 3 +--\n path.c                  | 2 +-\n protocol-caps.c         | 2 +-\n setup.c                 | 2 +-\n trace2/tr2_tgt_normal.c | 2 +-\n 11 files changed, 14 insertions(+), 19 deletions(-)\n\ndiff --git a/bisect.c b/bisect.c\nindex d71c4e4b44b..94bb53c9bf6 100644\n--- a/bisect.c\n+++ b/bisect.c\n@@ -454,7 +454,7 @@ static int register_ref(const char *refname, const char *referent UNUSED, const\n {\n \tstruct strbuf good_prefix = STRBUF_INIT;\n \tstrbuf_addstr(&good_prefix, term_good);\n-\tstrbuf_addstr(&good_prefix, \"-\");\n+\tstrbuf_addch(&good_prefix, '-');\n \n \tif (!strcmp(refname, term_bad)) {\n \t\tfree(current_bad_oid);\ndiff --git a/builtin/am.c b/builtin/am.c\nindex bfa95147cf4..6cb9fd4f45f 100644\n--- a/builtin/am.c\n+++ b/builtin/am.c\n@@ -341,13 +341,11 @@ static void write_author_script(const struct am_state *state)\n \n \tstrbuf_addstr(&sb, \"GIT_AUTHOR_NAME=\");\n \tsq_quote_buf(&sb, state->author_name);\n-\tstrbuf_addch(&sb, '\\n');\n \n-\tstrbuf_addstr(&sb, \"GIT_AUTHOR_EMAIL=\");\n+\tstrbuf_addstr(&sb, \"\\nGIT_AUTHOR_EMAIL=\");\n \tsq_quote_buf(&sb, state->author_email);\n-\tstrbuf_addch(&sb, '\\n');\n \n-\tstrbuf_addstr(&sb, \"GIT_AUTHOR_DATE=\");\n+\tstrbuf_addstr(&sb, \"\\nGIT_AUTHOR_DATE=\");\n \tsq_quote_buf(&sb, state->author_date);\n \tstrbuf_addch(&sb, '\\n');\n \ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex 6a7bb3b0724..c1ee2585ee8 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -148,11 +148,9 @@ static void get_ac_line(const char *inbuf, const char *what,\n \n \tif (split_ident_line(&ident, tmp, len)) {\n \terror_out:\n-\t\t/* Ugh */\n-\t\ttmp = \"(unknown)\";\n-\t\tstrbuf_addstr(name, tmp);\n-\t\tstrbuf_addstr(mail, tmp);\n-\t\tstrbuf_addstr(tz, tmp);\n+\t\tstrbuf_addstr(name, \"(unknown)\");\n+\t\tstrbuf_addstr(mail, \"(unknown)\");\n+\t\tstrbuf_addstr(tz, \"(unknown)\");\n \t\t*time = 0;\n \t\treturn;\n \t}\ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex 8542b5d53e4..605ddb5a719 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -37,7 +37,7 @@ static void expand_objectsize(struct strbuf *line, const struct object_id *oid,\n \t} else if (padded) {\n \t\tstrbuf_addf(line, \"%7s\", \"-\");\n \t} else {\n-\t\tstrbuf_addstr(line, \"-\");\n+\t\tstrbuf_addch(line, '-');\n \t}\n }\n \ndiff --git a/diff.c b/diff.c\nindex 266ddf18e73..61434c6cb45 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -1763,7 +1763,7 @@ static void add_line_count(struct strbuf *out, int count)\n \t\tstrbuf_addstr(out, \"0,0\");\n \t\tbreak;\n \tcase 1:\n-\t\tstrbuf_addstr(out, \"1\");\n+\t\tstrbuf_addch(out, '1');\n \t\tbreak;\n \tdefault:\n \t\tstrbuf_addf(out, \"1,%d\", count);\ndiff --git a/log-tree.c b/log-tree.c\nindex 83cc4b1cfb7..d0dc065e4f3 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -452,7 +452,7 @@ void fmt_output_subject(struct strbuf *filename,\n \n \t\tstrbuf_addf(&temp, \"v%s\", info->reroll_count);\n \t\tformat_sanitized_subject(filename, temp.buf, temp.len);\n-\t\tstrbuf_addstr(filename, \"-\");\n+\t\tstrbuf_addch(filename, '-');\n \t\tstrbuf_release(&temp);\n \t}\n \tstrbuf_addf(filename, \"%04d-%s\", nr, subject);\ndiff --git a/merge-ort.c b/merge-ort.c\nindex 11029c10be3..8bda8170863 100644\n--- a/merge-ort.c\n+++ b/merge-ort.c\n@@ -827,8 +827,7 @@ static void path_msg(struct merge_options *opt,\n \n \tva_start(ap, fmt);\n \tif (opt->priv->call_depth) {\n-\t\tstrbuf_addchars(dest, ' ', 2);\n-\t\tstrbuf_addstr(dest, \"From inner merge:\");\n+\t\tstrbuf_addstr(dest, \"  From inner merge:\");\n \t\tstrbuf_addchars(dest, ' ', opt->priv->call_depth * 2);\n \t}\n \tstrbuf_vaddf(dest, fmt, ap);\ndiff --git a/path.c b/path.c\nindex 4dcf3c8d40d..04085c164d0 100644\n--- a/path.c\n+++ b/path.c\n@@ -982,7 +982,7 @@ const char *remove_leading_path(const char *in, const char *prefix)\n \n \tstrbuf_reset(&buf);\n \tif (!in[j])\n-\t\tstrbuf_addstr(&buf, \".\");\n+\t\tstrbuf_addch(&buf, '.');\n \telse\n \t\tstrbuf_addstr(&buf, in + j);\n \treturn buf.buf;\ndiff --git a/protocol-caps.c b/protocol-caps.c\nindex 855f279c2f7..a841a457bbd 100644\n--- a/protocol-caps.c\n+++ b/protocol-caps.c\n@@ -65,7 +65,7 @@ static void send_info(struct repository *r, struct packet_writer *writer,\n \n \t\tif (info->size) {\n \t\t\tif (oid_object_info(r, &oid, &object_size) < 0) {\n-\t\t\t\tstrbuf_addstr(&send_buffer, \" \");\n+\t\t\t\tstrbuf_addch(&send_buffer, ' ');\n \t\t\t} else {\n \t\t\t\tstrbuf_addf(&send_buffer, \" %lu\", object_size);\n \t\t\t}\ndiff --git a/setup.c b/setup.c\nindex 39ff48d9dc5..27535f9f9a2 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1550,7 +1550,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,\n \t\t\t\treturn GIT_DIR_DISALLOWED_BARE;\n \t\t\tif (!ensure_valid_ownership(NULL, NULL, dir->buf, report))\n \t\t\t\treturn GIT_DIR_INVALID_OWNERSHIP;\n-\t\t\tstrbuf_addstr(gitdir, \".\");\n+\t\t\tstrbuf_addch(gitdir, '.');\n \t\t\treturn GIT_DIR_BARE;\n \t\t}\n \ndiff --git a/trace2/tr2_tgt_normal.c b/trace2/tr2_tgt_normal.c\nindex baef48aa698..8a05cf2109a 100644\n--- a/trace2/tr2_tgt_normal.c\n+++ b/trace2/tr2_tgt_normal.c\n@@ -226,7 +226,7 @@ static void fn_child_start_fl(const char *file, int line,\n \tif (cmd->dir) {\n \t\tstrbuf_addstr(&buf_payload, \" cd \");\n \t\tsq_quote_buf_pretty(&buf_payload, cmd->dir);\n-\t\tstrbuf_addstr(&buf_payload, \";\");\n+\t\tstrbuf_addch(&buf_payload, ';');\n \t}\n \n \t/*\n\nbase-commit: 2ccc89b0c16c51561da90d21cfbb4b58cc877bf6\n-- \ngitgitgadget\n"},{"id":"509268","messageId":"pull.1436.v3.git.git.1734490498710.gitgitgadget@gmail.com","threadId":"59110","inReplyTo":"pull.1436.v2.git.git.1734481897905.gitgitgadget@gmail.com","subject":"[PATCH v3] git: replace strbuf_addstr with strbuf_addch for all strings of length 2","fromName":"AreaZR via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-12-18T02:54:58Z","receivedAt":"2024-12-18T02:55:02Z","isPatch":true,"sender":{"key":"name:AreaZR","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nAdding the char directly instead of a string of length 2\nis clearer and more efficient.\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    git: replace strbuf_addstr with strbuf_addch for all strings of length 2\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1436%2FAreaZR%2Fstrbuf-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1436/AreaZR/strbuf-v3\nPull-Request: https://github.com/git/git/pull/1436\n\nRange-diff vs v2:\n\n 1:  22c37ed6677 ! 1:  96a093dec36 git: replace strbuf_addstr with strbuf_addch for all strings of length 2\n     @@ Metadata\n       ## Commit message ##\n          git: replace strbuf_addstr with strbuf_addch for all strings of length 2\n      \n     -    This helps reduce overhead of calculating the length\n     +    Adding the char directly instead of a string of length 2\n     +    is clearer and more efficient.\n      \n          Signed-off-by: Seija Kijin <doremylover123@gmail.com>\n      \n     @@ log-tree.c: void fmt_output_subject(struct strbuf *filename,\n       \t}\n       \tstrbuf_addf(filename, \"%04d-%s\", nr, subject);\n      \n     - ## merge-ort.c ##\n     -@@ merge-ort.c: static void path_msg(struct merge_options *opt,\n     - \n     - \tva_start(ap, fmt);\n     - \tif (opt->priv->call_depth) {\n     --\t\tstrbuf_addchars(dest, ' ', 2);\n     --\t\tstrbuf_addstr(dest, \"From inner merge:\");\n     -+\t\tstrbuf_addstr(dest, \"  From inner merge:\");\n     - \t\tstrbuf_addchars(dest, ' ', opt->priv->call_depth * 2);\n     - \t}\n     - \tstrbuf_vaddf(dest, fmt, ap);\n     -\n       ## path.c ##\n      @@ path.c: const char *remove_leading_path(const char *in, const char *prefix)\n       \n\n\n bisect.c                | 2 +-\n builtin/am.c            | 6 ++----\n builtin/blame.c         | 8 +++-----\n builtin/ls-tree.c       | 2 +-\n diff.c                  | 2 +-\n log-tree.c              | 2 +-\n path.c                  | 2 +-\n protocol-caps.c         | 2 +-\n setup.c                 | 2 +-\n trace2/tr2_tgt_normal.c | 2 +-\n 10 files changed, 13 insertions(+), 17 deletions(-)\n\ndiff --git a/bisect.c b/bisect.c\nindex d71c4e4b44b..94bb53c9bf6 100644\n--- a/bisect.c\n+++ b/bisect.c\n@@ -454,7 +454,7 @@ static int register_ref(const char *refname, const char *referent UNUSED, const\n {\n \tstruct strbuf good_prefix = STRBUF_INIT;\n \tstrbuf_addstr(&good_prefix, term_good);\n-\tstrbuf_addstr(&good_prefix, \"-\");\n+\tstrbuf_addch(&good_prefix, '-');\n \n \tif (!strcmp(refname, term_bad)) {\n \t\tfree(current_bad_oid);\ndiff --git a/builtin/am.c b/builtin/am.c\nindex bfa95147cf4..6cb9fd4f45f 100644\n--- a/builtin/am.c\n+++ b/builtin/am.c\n@@ -341,13 +341,11 @@ static void write_author_script(const struct am_state *state)\n \n \tstrbuf_addstr(&sb, \"GIT_AUTHOR_NAME=\");\n \tsq_quote_buf(&sb, state->author_name);\n-\tstrbuf_addch(&sb, '\\n');\n \n-\tstrbuf_addstr(&sb, \"GIT_AUTHOR_EMAIL=\");\n+\tstrbuf_addstr(&sb, \"\\nGIT_AUTHOR_EMAIL=\");\n \tsq_quote_buf(&sb, state->author_email);\n-\tstrbuf_addch(&sb, '\\n');\n \n-\tstrbuf_addstr(&sb, \"GIT_AUTHOR_DATE=\");\n+\tstrbuf_addstr(&sb, \"\\nGIT_AUTHOR_DATE=\");\n \tsq_quote_buf(&sb, state->author_date);\n \tstrbuf_addch(&sb, '\\n');\n \ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex 6a7bb3b0724..c1ee2585ee8 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -148,11 +148,9 @@ static void get_ac_line(const char *inbuf, const char *what,\n \n \tif (split_ident_line(&ident, tmp, len)) {\n \terror_out:\n-\t\t/* Ugh */\n-\t\ttmp = \"(unknown)\";\n-\t\tstrbuf_addstr(name, tmp);\n-\t\tstrbuf_addstr(mail, tmp);\n-\t\tstrbuf_addstr(tz, tmp);\n+\t\tstrbuf_addstr(name, \"(unknown)\");\n+\t\tstrbuf_addstr(mail, \"(unknown)\");\n+\t\tstrbuf_addstr(tz, \"(unknown)\");\n \t\t*time = 0;\n \t\treturn;\n \t}\ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex 8542b5d53e4..605ddb5a719 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -37,7 +37,7 @@ static void expand_objectsize(struct strbuf *line, const struct object_id *oid,\n \t} else if (padded) {\n \t\tstrbuf_addf(line, \"%7s\", \"-\");\n \t} else {\n-\t\tstrbuf_addstr(line, \"-\");\n+\t\tstrbuf_addch(line, '-');\n \t}\n }\n \ndiff --git a/diff.c b/diff.c\nindex 266ddf18e73..61434c6cb45 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -1763,7 +1763,7 @@ static void add_line_count(struct strbuf *out, int count)\n \t\tstrbuf_addstr(out, \"0,0\");\n \t\tbreak;\n \tcase 1:\n-\t\tstrbuf_addstr(out, \"1\");\n+\t\tstrbuf_addch(out, '1');\n \t\tbreak;\n \tdefault:\n \t\tstrbuf_addf(out, \"1,%d\", count);\ndiff --git a/log-tree.c b/log-tree.c\nindex 83cc4b1cfb7..d0dc065e4f3 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -452,7 +452,7 @@ void fmt_output_subject(struct strbuf *filename,\n \n \t\tstrbuf_addf(&temp, \"v%s\", info->reroll_count);\n \t\tformat_sanitized_subject(filename, temp.buf, temp.len);\n-\t\tstrbuf_addstr(filename, \"-\");\n+\t\tstrbuf_addch(filename, '-');\n \t\tstrbuf_release(&temp);\n \t}\n \tstrbuf_addf(filename, \"%04d-%s\", nr, subject);\ndiff --git a/path.c b/path.c\nindex 4dcf3c8d40d..04085c164d0 100644\n--- a/path.c\n+++ b/path.c\n@@ -982,7 +982,7 @@ const char *remove_leading_path(const char *in, const char *prefix)\n \n \tstrbuf_reset(&buf);\n \tif (!in[j])\n-\t\tstrbuf_addstr(&buf, \".\");\n+\t\tstrbuf_addch(&buf, '.');\n \telse\n \t\tstrbuf_addstr(&buf, in + j);\n \treturn buf.buf;\ndiff --git a/protocol-caps.c b/protocol-caps.c\nindex 855f279c2f7..a841a457bbd 100644\n--- a/protocol-caps.c\n+++ b/protocol-caps.c\n@@ -65,7 +65,7 @@ static void send_info(struct repository *r, struct packet_writer *writer,\n \n \t\tif (info->size) {\n \t\t\tif (oid_object_info(r, &oid, &object_size) < 0) {\n-\t\t\t\tstrbuf_addstr(&send_buffer, \" \");\n+\t\t\t\tstrbuf_addch(&send_buffer, ' ');\n \t\t\t} else {\n \t\t\t\tstrbuf_addf(&send_buffer, \" %lu\", object_size);\n \t\t\t}\ndiff --git a/setup.c b/setup.c\nindex 39ff48d9dc5..27535f9f9a2 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1550,7 +1550,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,\n \t\t\t\treturn GIT_DIR_DISALLOWED_BARE;\n \t\t\tif (!ensure_valid_ownership(NULL, NULL, dir->buf, report))\n \t\t\t\treturn GIT_DIR_INVALID_OWNERSHIP;\n-\t\t\tstrbuf_addstr(gitdir, \".\");\n+\t\t\tstrbuf_addch(gitdir, '.');\n \t\t\treturn GIT_DIR_BARE;\n \t\t}\n \ndiff --git a/trace2/tr2_tgt_normal.c b/trace2/tr2_tgt_normal.c\nindex baef48aa698..8a05cf2109a 100644\n--- a/trace2/tr2_tgt_normal.c\n+++ b/trace2/tr2_tgt_normal.c\n@@ -226,7 +226,7 @@ static void fn_child_start_fl(const char *file, int line,\n \tif (cmd->dir) {\n \t\tstrbuf_addstr(&buf_payload, \" cd \");\n \t\tsq_quote_buf_pretty(&buf_payload, cmd->dir);\n-\t\tstrbuf_addstr(&buf_payload, \";\");\n+\t\tstrbuf_addch(&buf_payload, ';');\n \t}\n \n \t/*\n\nbase-commit: d882f382b3d939d90cfa58d17b17802338f05d66\n-- \ngitgitgadget\n"},{"id":"509269","messageId":"pull.1436.v4.git.git.1734491173098.gitgitgadget@gmail.com","threadId":"59110","inReplyTo":"pull.1436.v3.git.git.1734490498710.gitgitgadget@gmail.com","subject":"[PATCH v4] git: replace strbuf_addstr with strbuf_addch for all strings of length 2","fromName":"AreaZR via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-12-18T03:06:12Z","receivedAt":"2024-12-18T03:06:16Z","isPatch":true,"sender":{"key":"name:AreaZR","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nAdding the char directly instead of a string of length 2\nis clearer and more efficient.\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    git: replace strbuf_addstr with strbuf_addch for all strings of length 2\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1436%2FAreaZR%2Fstrbuf-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1436/AreaZR/strbuf-v4\nPull-Request: https://github.com/git/git/pull/1436\n\nRange-diff vs v3:\n\n 1:  96a093dec36 ! 1:  413c5ab1f1b git: replace strbuf_addstr with strbuf_addch for all strings of length 2\n     @@ bisect.c: static int register_ref(const char *refname, const char *referent UNUS\n       \tif (!strcmp(refname, term_bad)) {\n       \t\tfree(current_bad_oid);\n      \n     - ## builtin/am.c ##\n     -@@ builtin/am.c: static void write_author_script(const struct am_state *state)\n     - \n     - \tstrbuf_addstr(&sb, \"GIT_AUTHOR_NAME=\");\n     - \tsq_quote_buf(&sb, state->author_name);\n     --\tstrbuf_addch(&sb, '\\n');\n     - \n     --\tstrbuf_addstr(&sb, \"GIT_AUTHOR_EMAIL=\");\n     -+\tstrbuf_addstr(&sb, \"\\nGIT_AUTHOR_EMAIL=\");\n     - \tsq_quote_buf(&sb, state->author_email);\n     --\tstrbuf_addch(&sb, '\\n');\n     - \n     --\tstrbuf_addstr(&sb, \"GIT_AUTHOR_DATE=\");\n     -+\tstrbuf_addstr(&sb, \"\\nGIT_AUTHOR_DATE=\");\n     - \tsq_quote_buf(&sb, state->author_date);\n     - \tstrbuf_addch(&sb, '\\n');\n     - \n     -\n     - ## builtin/blame.c ##\n     -@@ builtin/blame.c: static void get_ac_line(const char *inbuf, const char *what,\n     - \n     - \tif (split_ident_line(&ident, tmp, len)) {\n     - \terror_out:\n     --\t\t/* Ugh */\n     --\t\ttmp = \"(unknown)\";\n     --\t\tstrbuf_addstr(name, tmp);\n     --\t\tstrbuf_addstr(mail, tmp);\n     --\t\tstrbuf_addstr(tz, tmp);\n     -+\t\tstrbuf_addstr(name, \"(unknown)\");\n     -+\t\tstrbuf_addstr(mail, \"(unknown)\");\n     -+\t\tstrbuf_addstr(tz, \"(unknown)\");\n     - \t\t*time = 0;\n     - \t\treturn;\n     - \t}\n     -\n       ## builtin/ls-tree.c ##\n      @@ builtin/ls-tree.c: static void expand_objectsize(struct strbuf *line, const struct object_id *oid,\n       \t} else if (padded) {\n     @@ builtin/ls-tree.c: static void expand_objectsize(struct strbuf *line, const stru\n       }\n       \n      \n     + ## convert.c ##\n     +@@ convert.c: static void trace_encoding(const char *context, const char *path,\n     + \t\t\t((i+1) % 8 && (i+1) < len ? ' ' : '\\n')\n     + \t\t);\n     + \t}\n     +-\tstrbuf_addchars(&trace, '\\n', 1);\n     ++\tstrbuf_addch(&trace, '\\n');\n     + \n     + \ttrace_strbuf(&coe, &trace);\n     + \tstrbuf_release(&trace);\n     +\n       ## diff.c ##\n      @@ diff.c: static void add_line_count(struct strbuf *out, int count)\n       \t\tstrbuf_addstr(out, \"0,0\");\n     @@ log-tree.c: void fmt_output_subject(struct strbuf *filename,\n       \t}\n       \tstrbuf_addf(filename, \"%04d-%s\", nr, subject);\n      \n     + ## merge-ort.c ##\n     +@@ merge-ort.c: static void path_msg(struct merge_options *opt,\n     + \n     + \tva_start(ap, fmt);\n     + \tif (opt->priv->call_depth) {\n     +-\t\tstrbuf_addchars(dest, ' ', 2);\n     ++\t\tstrbuf_addstr(dest, \"  \");\n     + \t\tstrbuf_addstr(dest, \"From inner merge:\");\n     + \t\tstrbuf_addchars(dest, ' ', opt->priv->call_depth * 2);\n     + \t}\n     +\n       ## path.c ##\n      @@ path.c: const char *remove_leading_path(const char *in, const char *prefix)\n       \n     @@ path.c: const char *remove_leading_path(const char *in, const char *prefix)\n       \t\tstrbuf_addstr(&buf, in + j);\n       \treturn buf.buf;\n      \n     + ## pretty.c ##\n     +@@ pretty.c: void pp_user_info(struct pretty_print_context *pp,\n     + \n     + \t\tstrbuf_addf(sb, \"%s: \", what);\n     + \t\tif (pp->fmt == CMIT_FMT_FULLER)\n     +-\t\t\tstrbuf_addchars(sb, ' ', 4);\n     ++\t\t\tstrbuf_addstr(sb, \"    \");\n     + \n     + \t\tstrbuf_addf(&id, \"%.*s <%.*s>\", (int)namelen, namebuf,\n     + \t\t\t    (int)maillen, mailbuf);\n     +\n       ## protocol-caps.c ##\n      @@ protocol-caps.c: static void send_info(struct repository *r, struct packet_writer *writer,\n       \n     @@ protocol-caps.c: static void send_info(struct repository *r, struct packet_write\n       \t\t\t\tstrbuf_addf(&send_buffer, \" %lu\", object_size);\n       \t\t\t}\n      \n     + ## send-pack.c ##\n     +@@ send-pack.c: static int generate_push_cert(struct strbuf *req_buf,\n     + \tif (args->push_options)\n     + \t\tfor_each_string_list_item(item, args->push_options)\n     + \t\t\tstrbuf_addf(&cert, \"push-option %s\\n\", item->string);\n     +-\tstrbuf_addstr(&cert, \"\\n\");\n     ++\tstrbuf_addch(&cert, '\\n');\n     + \n     + \tfor (ref = remote_refs; ref; ref = ref->next) {\n     + \t\tif (check_to_send_update(ref, args) < 0)\n     +\n       ## setup.c ##\n      @@ setup.c: static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,\n       \t\t\t\treturn GIT_DIR_DISALLOWED_BARE;\n\n\n bisect.c                | 2 +-\n builtin/ls-tree.c       | 2 +-\n convert.c               | 2 +-\n diff.c                  | 2 +-\n log-tree.c              | 2 +-\n merge-ort.c             | 2 +-\n path.c                  | 2 +-\n pretty.c                | 2 +-\n protocol-caps.c         | 2 +-\n send-pack.c             | 2 +-\n setup.c                 | 2 +-\n trace2/tr2_tgt_normal.c | 2 +-\n 12 files changed, 12 insertions(+), 12 deletions(-)\n\ndiff --git a/bisect.c b/bisect.c\nindex d71c4e4b44b..94bb53c9bf6 100644\n--- a/bisect.c\n+++ b/bisect.c\n@@ -454,7 +454,7 @@ static int register_ref(const char *refname, const char *referent UNUSED, const\n {\n \tstruct strbuf good_prefix = STRBUF_INIT;\n \tstrbuf_addstr(&good_prefix, term_good);\n-\tstrbuf_addstr(&good_prefix, \"-\");\n+\tstrbuf_addch(&good_prefix, '-');\n \n \tif (!strcmp(refname, term_bad)) {\n \t\tfree(current_bad_oid);\ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex 8542b5d53e4..605ddb5a719 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -37,7 +37,7 @@ static void expand_objectsize(struct strbuf *line, const struct object_id *oid,\n \t} else if (padded) {\n \t\tstrbuf_addf(line, \"%7s\", \"-\");\n \t} else {\n-\t\tstrbuf_addstr(line, \"-\");\n+\t\tstrbuf_addch(line, '-');\n \t}\n }\n \ndiff --git a/convert.c b/convert.c\nindex c9a31eb4f03..3ee5a22b2a8 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -337,7 +337,7 @@ static void trace_encoding(const char *context, const char *path,\n \t\t\t((i+1) % 8 && (i+1) < len ? ' ' : '\\n')\n \t\t);\n \t}\n-\tstrbuf_addchars(&trace, '\\n', 1);\n+\tstrbuf_addch(&trace, '\\n');\n \n \ttrace_strbuf(&coe, &trace);\n \tstrbuf_release(&trace);\ndiff --git a/diff.c b/diff.c\nindex 266ddf18e73..61434c6cb45 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -1763,7 +1763,7 @@ static void add_line_count(struct strbuf *out, int count)\n \t\tstrbuf_addstr(out, \"0,0\");\n \t\tbreak;\n \tcase 1:\n-\t\tstrbuf_addstr(out, \"1\");\n+\t\tstrbuf_addch(out, '1');\n \t\tbreak;\n \tdefault:\n \t\tstrbuf_addf(out, \"1,%d\", count);\ndiff --git a/log-tree.c b/log-tree.c\nindex 83cc4b1cfb7..d0dc065e4f3 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -452,7 +452,7 @@ void fmt_output_subject(struct strbuf *filename,\n \n \t\tstrbuf_addf(&temp, \"v%s\", info->reroll_count);\n \t\tformat_sanitized_subject(filename, temp.buf, temp.len);\n-\t\tstrbuf_addstr(filename, \"-\");\n+\t\tstrbuf_addch(filename, '-');\n \t\tstrbuf_release(&temp);\n \t}\n \tstrbuf_addf(filename, \"%04d-%s\", nr, subject);\ndiff --git a/merge-ort.c b/merge-ort.c\nindex 11029c10be3..a36c2c936fe 100644\n--- a/merge-ort.c\n+++ b/merge-ort.c\n@@ -827,7 +827,7 @@ static void path_msg(struct merge_options *opt,\n \n \tva_start(ap, fmt);\n \tif (opt->priv->call_depth) {\n-\t\tstrbuf_addchars(dest, ' ', 2);\n+\t\tstrbuf_addstr(dest, \"  \");\n \t\tstrbuf_addstr(dest, \"From inner merge:\");\n \t\tstrbuf_addchars(dest, ' ', opt->priv->call_depth * 2);\n \t}\ndiff --git a/path.c b/path.c\nindex 4dcf3c8d40d..04085c164d0 100644\n--- a/path.c\n+++ b/path.c\n@@ -982,7 +982,7 @@ const char *remove_leading_path(const char *in, const char *prefix)\n \n \tstrbuf_reset(&buf);\n \tif (!in[j])\n-\t\tstrbuf_addstr(&buf, \".\");\n+\t\tstrbuf_addch(&buf, '.');\n \telse\n \t\tstrbuf_addstr(&buf, in + j);\n \treturn buf.buf;\ndiff --git a/pretty.c b/pretty.c\nindex 098378720a4..84c96fc5a80 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -590,7 +590,7 @@ void pp_user_info(struct pretty_print_context *pp,\n \n \t\tstrbuf_addf(sb, \"%s: \", what);\n \t\tif (pp->fmt == CMIT_FMT_FULLER)\n-\t\t\tstrbuf_addchars(sb, ' ', 4);\n+\t\t\tstrbuf_addstr(sb, \"    \");\n \n \t\tstrbuf_addf(&id, \"%.*s <%.*s>\", (int)namelen, namebuf,\n \t\t\t    (int)maillen, mailbuf);\ndiff --git a/protocol-caps.c b/protocol-caps.c\nindex 855f279c2f7..a841a457bbd 100644\n--- a/protocol-caps.c\n+++ b/protocol-caps.c\n@@ -65,7 +65,7 @@ static void send_info(struct repository *r, struct packet_writer *writer,\n \n \t\tif (info->size) {\n \t\t\tif (oid_object_info(r, &oid, &object_size) < 0) {\n-\t\t\t\tstrbuf_addstr(&send_buffer, \" \");\n+\t\t\t\tstrbuf_addch(&send_buffer, ' ');\n \t\t\t} else {\n \t\t\t\tstrbuf_addf(&send_buffer, \" %lu\", object_size);\n \t\t\t}\ndiff --git a/send-pack.c b/send-pack.c\nindex 6677c44e8ac..9eb7cdc6ee7 100644\n--- a/send-pack.c\n+++ b/send-pack.c\n@@ -373,7 +373,7 @@ static int generate_push_cert(struct strbuf *req_buf,\n \tif (args->push_options)\n \t\tfor_each_string_list_item(item, args->push_options)\n \t\t\tstrbuf_addf(&cert, \"push-option %s\\n\", item->string);\n-\tstrbuf_addstr(&cert, \"\\n\");\n+\tstrbuf_addch(&cert, '\\n');\n \n \tfor (ref = remote_refs; ref; ref = ref->next) {\n \t\tif (check_to_send_update(ref, args) < 0)\ndiff --git a/setup.c b/setup.c\nindex 39ff48d9dc5..27535f9f9a2 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1550,7 +1550,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,\n \t\t\t\treturn GIT_DIR_DISALLOWED_BARE;\n \t\t\tif (!ensure_valid_ownership(NULL, NULL, dir->buf, report))\n \t\t\t\treturn GIT_DIR_INVALID_OWNERSHIP;\n-\t\t\tstrbuf_addstr(gitdir, \".\");\n+\t\t\tstrbuf_addch(gitdir, '.');\n \t\t\treturn GIT_DIR_BARE;\n \t\t}\n \ndiff --git a/trace2/tr2_tgt_normal.c b/trace2/tr2_tgt_normal.c\nindex baef48aa698..8a05cf2109a 100644\n--- a/trace2/tr2_tgt_normal.c\n+++ b/trace2/tr2_tgt_normal.c\n@@ -226,7 +226,7 @@ static void fn_child_start_fl(const char *file, int line,\n \tif (cmd->dir) {\n \t\tstrbuf_addstr(&buf_payload, \" cd \");\n \t\tsq_quote_buf_pretty(&buf_payload, cmd->dir);\n-\t\tstrbuf_addstr(&buf_payload, \";\");\n+\t\tstrbuf_addch(&buf_payload, ';');\n \t}\n \n \t/*\n\nbase-commit: d882f382b3d939d90cfa58d17b17802338f05d66\n-- \ngitgitgadget\n"}]}