{"thread":{"id":"47624","subject":"[PATCH v2 1/2] sha1_file: remove static strbuf from sha1_file_name()","startedAt":"2018-01-17T17:55:21Z","lastAt":"2018-01-17T22:27:14Z","messageCount":6,"participants":["Christian Couder","Jeff Hostetler","Junio C Hamano","Jeff King"],"isPatch":true,"patchVersion":2,"patchTotal":2},"messages":[{"id":"336701","messageId":"20180117175455.9316-1-chriscool@tuxfamily.org","threadId":"47624","inReplyTo":null,"subject":"[PATCH v2 1/2] sha1_file: remove static strbuf from sha1_file_name()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2018-01-17T17:54:54Z","receivedAt":"2018-01-17T17:55:21Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Using a static buffer in sha1_file_name() is error prone\nand the performance improvements it gives are not needed\nin many of the callers.\n\nSo let's get rid of this static buffer and, if necessary\nor helpful, let's use one in the caller.\n\nSuggested-by: Jeff Hostetler <git@jeffhostetler.com>\nHelped-by: Kevin Daudt <me@ikke.info>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n cache.h       |  8 +++-----\n http-walker.c |  6 ++++--\n http.c        | 16 ++++++++++------\n sha1_file.c   | 38 +++++++++++++++++++++++++-------------\n 4 files changed, 42 insertions(+), 26 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex d8b975a571..6db565408e 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -957,12 +957,10 @@ extern void check_repository_format(void);\n #define TYPE_CHANGED    0x0040\n \n /*\n- * Return the name of the file in the local object database that would\n- * be used to store a loose object with the specified sha1.  The\n- * return value is a pointer to a statically allocated buffer that is\n- * overwritten each time the function is called.\n+ * Put in `buf` the name of the file in the local object database that\n+ * would be used to store a loose object with the specified sha1.\n  */\n-extern const char *sha1_file_name(const unsigned char *sha1);\n+extern void sha1_file_name(struct strbuf *buf, const unsigned char *sha1);\n \n /*\n  * Return an abbreviated sha1 unique within this repository's object database.\ndiff --git a/http-walker.c b/http-walker.c\nindex 1ae8363de2..07c2b1af82 100644\n--- a/http-walker.c\n+++ b/http-walker.c\n@@ -544,8 +544,10 @@ static int fetch_object(struct walker *walker, unsigned char *sha1)\n \t} else if (hashcmp(obj_req->sha1, req->real_sha1)) {\n \t\tret = error(\"File %s has bad hash\", hex);\n \t} else if (req->rename < 0) {\n-\t\tret = error(\"unable to write sha1 filename %s\",\n-\t\t\t    sha1_file_name(req->sha1));\n+\t\tstruct strbuf buf = STRBUF_INIT;\n+\t\tsha1_file_name(&buf, req->sha1);\n+\t\tret = error(\"unable to write sha1 filename %s\", buf.buf);\n+\t\tstrbuf_release(&buf);\n \t}\n \n \trelease_http_object_request(req);\ndiff --git a/http.c b/http.c\nindex 5977712712..5979305bc9 100644\n--- a/http.c\n+++ b/http.c\n@@ -2168,7 +2168,7 @@ struct http_object_request *new_http_object_request(const char *base_url,\n \tunsigned char *sha1)\n {\n \tchar *hex = sha1_to_hex(sha1);\n-\tconst char *filename;\n+\tstruct strbuf filename = STRBUF_INIT;\n \tchar prevfile[PATH_MAX];\n \tint prevlocal;\n \tchar prev_buf[PREV_BUF_SIZE];\n@@ -2180,14 +2180,15 @@ struct http_object_request *new_http_object_request(const char *base_url,\n \thashcpy(freq->sha1, sha1);\n \tfreq->localfile = -1;\n \n-\tfilename = sha1_file_name(sha1);\n+\tsha1_file_name(&filename, sha1);\n \tsnprintf(freq->tmpfile, sizeof(freq->tmpfile),\n-\t\t \"%s.temp\", filename);\n+\t\t \"%s.temp\", filename.buf);\n \n-\tsnprintf(prevfile, sizeof(prevfile), \"%s.prev\", filename);\n+\tsnprintf(prevfile, sizeof(prevfile), \"%s.prev\", filename.buf);\n \tunlink_or_warn(prevfile);\n \trename(freq->tmpfile, prevfile);\n \tunlink_or_warn(freq->tmpfile);\n+\tstrbuf_release(&filename);\n \n \tif (freq->localfile != -1)\n \t\terror(\"fd leakage in start: %d\", freq->localfile);\n@@ -2302,6 +2303,7 @@ void process_http_object_request(struct http_object_request *freq)\n int finish_http_object_request(struct http_object_request *freq)\n {\n \tstruct stat st;\n+\tstruct strbuf filename = STRBUF_INIT;\n \n \tclose(freq->localfile);\n \tfreq->localfile = -1;\n@@ -2327,8 +2329,10 @@ int finish_http_object_request(struct http_object_request *freq)\n \t\tunlink_or_warn(freq->tmpfile);\n \t\treturn -1;\n \t}\n-\tfreq->rename =\n-\t\tfinalize_object_file(freq->tmpfile, sha1_file_name(freq->sha1));\n+\n+\tsha1_file_name(&filename, freq->sha1);\n+\tfreq->rename = finalize_object_file(freq->tmpfile, filename.buf);\n+\tstrbuf_release(&filename);\n \n \treturn freq->rename;\n }\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 3da70ac650..f66c21b2da 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -321,15 +321,11 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)\n \t}\n }\n \n-const char *sha1_file_name(const unsigned char *sha1)\n+void sha1_file_name(struct strbuf *buf, const unsigned char *sha1)\n {\n-\tstatic struct strbuf buf = STRBUF_INIT;\n-\n-\tstrbuf_reset(&buf);\n-\tstrbuf_addf(&buf, \"%s/\", get_object_directory());\n+\tstrbuf_addf(buf, \"%s/\", get_object_directory());\n \n-\tfill_sha1_path(&buf, sha1);\n-\treturn buf.buf;\n+\tfill_sha1_path(buf, sha1);\n }\n \n struct strbuf *alt_scratch_buf(struct alternate_object_database *alt)\n@@ -710,7 +706,12 @@ int check_and_freshen_file(const char *fn, int freshen)\n \n static int check_and_freshen_local(const unsigned char *sha1, int freshen)\n {\n-\treturn check_and_freshen_file(sha1_file_name(sha1), freshen);\n+\tstatic struct strbuf buf = STRBUF_INIT;\n+\n+\tstrbuf_reset(&buf);\n+\tsha1_file_name(&buf, sha1);\n+\n+\treturn check_and_freshen_file(buf.buf, freshen);\n }\n \n static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)\n@@ -866,8 +867,12 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st,\n \t\t\t  const char **path)\n {\n \tstruct alternate_object_database *alt;\n+\tstatic struct strbuf buf = STRBUF_INIT;\n+\n+\tstrbuf_reset(&buf);\n+\tsha1_file_name(&buf, sha1);\n+\t*path = buf.buf;\n \n-\t*path = sha1_file_name(sha1);\n \tif (!lstat(*path, st))\n \t\treturn 0;\n \n@@ -891,8 +896,12 @@ static int open_sha1_file(const unsigned char *sha1, const char **path)\n \tint fd;\n \tstruct alternate_object_database *alt;\n \tint most_interesting_errno;\n+\tstatic struct strbuf buf = STRBUF_INIT;\n+\n+\tstrbuf_reset(&buf);\n+\tsha1_file_name(&buf, sha1);\n+\t*path = buf.buf;\n \n-\t*path = sha1_file_name(sha1);\n \tfd = git_open(*path);\n \tif (fd >= 0)\n \t\treturn fd;\n@@ -1557,9 +1566,12 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,\n \tgit_SHA_CTX c;\n \tunsigned char parano_sha1[20];\n \tstatic struct strbuf tmp_file = STRBUF_INIT;\n-\tconst char *filename = sha1_file_name(sha1);\n+\tstatic struct strbuf filename = STRBUF_INIT;\n+\n+\tstrbuf_reset(&filename);\n+\tsha1_file_name(&filename, sha1);\n \n-\tfd = create_tmpfile(&tmp_file, filename);\n+\tfd = create_tmpfile(&tmp_file, filename.buf);\n \tif (fd < 0) {\n \t\tif (errno == EACCES)\n \t\t\treturn error(\"insufficient permission for adding an object to repository database %s\", get_object_directory());\n@@ -1612,7 +1624,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,\n \t\t\twarning_errno(\"failed utime() on %s\", tmp_file.buf);\n \t}\n \n-\treturn finalize_object_file(tmp_file.buf, filename);\n+\treturn finalize_object_file(tmp_file.buf, filename.buf);\n }\n \n static int freshen_loose_object(const unsigned char *sha1)\n-- \n2.16.0.rc2.3.g254af8b974\n\n"},{"id":"336702","messageId":"20180117175455.9316-2-chriscool@tuxfamily.org","threadId":"47624","inReplyTo":"20180117175455.9316-1-chriscool@tuxfamily.org","subject":"[PATCH v2 2/2] sha1_file: improve sha1_file_name() perfs","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2018-01-17T17:54:55Z","receivedAt":"2018-01-17T17:55:24Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"As sha1_file_name() could be performance sensitive, let's\ntry to make it faster by seeding the initial buffer size\nto avoid any need to realloc and by using strbuf_addstr()\nand strbuf_addc() instead of strbuf_addf().\n\nHelped-by: Derrick Stolee <stolee@gmail.com>\nHelped-by: Jeff Hostetler <git@jeffhostetler.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n sha1_file.c | 8 +++++++-\n 1 file changed, 7 insertions(+), 1 deletion(-)\n\ndiff --git a/sha1_file.c b/sha1_file.c\nindex f66c21b2da..1a94716962 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -323,8 +323,14 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)\n \n void sha1_file_name(struct strbuf *buf, const unsigned char *sha1)\n {\n-\tstrbuf_addf(buf, \"%s/\", get_object_directory());\n+\tconst char *obj_dir = get_object_directory();\n+\tsize_t extra = strlen(obj_dir) + 1 + GIT_MAX_HEXSZ;\n \n+\tif (extra > strbuf_avail(buf))\n+\t\tstrbuf_grow(buf, extra);\n+\n+\tstrbuf_addstr(buf, obj_dir);\n+\tstrbuf_addch(buf, '/');\n \tfill_sha1_path(buf, sha1);\n }\n \n-- \n2.16.0.rc2.3.g254af8b974\n\n"},{"id":"336719","messageId":"3cbace45-61df-3074-a48c-66c68c96d542@jeffhostetler.com","threadId":"47624","inReplyTo":"20180117175455.9316-2-chriscool@tuxfamily.org","subject":"Re: [PATCH v2 2/2] sha1_file: improve sha1_file_name() perfs","fromName":"Jeff Hostetler","fromEmail":"git@jeffhostetler.com","sentAt":"2018-01-17T20:37:55Z","receivedAt":"2018-01-17T20:38:03Z","isPatch":true,"sender":{"key":"git@jeffhostetler.com","avatar":null},"body":"\n\nOn 1/17/2018 12:54 PM, Christian Couder wrote:\n> As sha1_file_name() could be performance sensitive, let's\n> try to make it faster by seeding the initial buffer size\n> to avoid any need to realloc and by using strbuf_addstr()\n> and strbuf_addc() instead of strbuf_addf().\n> \n> Helped-by: Derrick Stolee <stolee@gmail.com>\n> Helped-by: Jeff Hostetler <git@jeffhostetler.com>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>   sha1_file.c | 8 +++++++-\n>   1 file changed, 7 insertions(+), 1 deletion(-)\n> \n> diff --git a/sha1_file.c b/sha1_file.c\n> index f66c21b2da..1a94716962 100644\n> --- a/sha1_file.c\n> +++ b/sha1_file.c\n> @@ -323,8 +323,14 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)\n>   \n>   void sha1_file_name(struct strbuf *buf, const unsigned char *sha1)\n>   {\n> -\tstrbuf_addf(buf, \"%s/\", get_object_directory());\n> +\tconst char *obj_dir = get_object_directory();\n> +\tsize_t extra = strlen(obj_dir) + 1 + GIT_MAX_HEXSZ;\n\nVery minor nit.  Should this be \"+3\" rather than \"+1\"?\nOne for the slash after obj_dir, one for the slash between\n\"xx/y[38]\", and one for the trailing NUL.\n\n>   \n> +\tif (extra > strbuf_avail(buf))\n> +\t\tstrbuf_grow(buf, extra);\n> +\n> +\tstrbuf_addstr(buf, obj_dir);\n> +\tstrbuf_addch(buf, '/');\n>   \tfill_sha1_path(buf, sha1);\n>   }\n>   \n> \n"},{"id":"336720","messageId":"xmqq4lnkryt2.fsf@gitster.mtv.corp.google.com","threadId":"47624","inReplyTo":"3cbace45-61df-3074-a48c-66c68c96d542@jeffhostetler.com","subject":"Re: [PATCH v2 2/2] sha1_file: improve sha1_file_name() perfs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-01-17T20:54:33Z","receivedAt":"2018-01-17T20:54:44Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff Hostetler <git@jeffhostetler.com> writes:\n\n>>     void sha1_file_name(struct strbuf *buf, const unsigned char\n>> *sha1)\n>>   {\n>> -\tstrbuf_addf(buf, \"%s/\", get_object_directory());\n>> +\tconst char *obj_dir = get_object_directory();\n>> +\tsize_t extra = strlen(obj_dir) + 1 + GIT_MAX_HEXSZ;\n>\n> Very minor nit.  Should this be \"+3\" rather than \"+1\"?\n> One for the slash after obj_dir, one for the slash between\n> \"xx/y[38]\", and one for the trailing NUL.\n>\n>>   +\tif (extra > strbuf_avail(buf))\n>> +\t\tstrbuf_grow(buf, extra);\n\nThe callers who care use static strbuf with 1/2, which lets them\ngrow it to an appropriate size after they make their first call.\n\nOn the other hand, the ones to which performance does not matter by\ndefinition do not care.\n\nI actually think this whole \"extra -> grow\" business should be\ndiscarded.  With a miscomputed \"extra\" like this, it does not help\nanybody---everybody may pay cost for one extra realloc due to the\nmiscalculation, and the ones that do care also do during their first\ncall.\n\n\n"},{"id":"336725","messageId":"CAP8UFD16OLcmATtr8wg7KcnyUJJLYv5+8n+ygSZEOUbHMTgopQ@mail.gmail.com","threadId":"47624","inReplyTo":"3cbace45-61df-3074-a48c-66c68c96d542@jeffhostetler.com","subject":"Re: [PATCH v2 2/2] sha1_file: improve sha1_file_name() perfs","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2018-01-17T21:44:20Z","receivedAt":"2018-01-17T21:44:26Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Jan 17, 2018 at 9:37 PM, Jeff Hostetler <git@jeffhostetler.com> wrote:\n>\n>\n> On 1/17/2018 12:54 PM, Christian Couder wrote:\n>>\n>> As sha1_file_name() could be performance sensitive, let's\n>> try to make it faster by seeding the initial buffer size\n>> to avoid any need to realloc and by using strbuf_addstr()\n>> and strbuf_addc() instead of strbuf_addf().\n>>\n>> Helped-by: Derrick Stolee <stolee@gmail.com>\n>> Helped-by: Jeff Hostetler <git@jeffhostetler.com>\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>>   sha1_file.c | 8 +++++++-\n>>   1 file changed, 7 insertions(+), 1 deletion(-)\n>>\n>> diff --git a/sha1_file.c b/sha1_file.c\n>> index f66c21b2da..1a94716962 100644\n>> --- a/sha1_file.c\n>> +++ b/sha1_file.c\n>> @@ -323,8 +323,14 @@ static void fill_sha1_path(struct strbuf *buf, const\n>> unsigned char *sha1)\n>>     void sha1_file_name(struct strbuf *buf, const unsigned char *sha1)\n>>   {\n>> -       strbuf_addf(buf, \"%s/\", get_object_directory());\n>> +       const char *obj_dir = get_object_directory();\n>> +       size_t extra = strlen(obj_dir) + 1 + GIT_MAX_HEXSZ;\n>\n> Very minor nit.  Should this be \"+3\" rather than \"+1\"?\n> One for the slash after obj_dir, one for the slash between\n> \"xx/y[38]\", and one for the trailing NUL.\n\nI think the trailing NUL is handled by strbuf_grow(), but I forgot\nabout the / between \"xx/y[38]\", so I think it should be \"+2\" .\n\nAnyway I agree with Junio that using strbuf_grow() is not really needed.\n\n>>   +     if (extra > strbuf_avail(buf))\n>> +               strbuf_grow(buf, extra);\n>> +\n>> +       strbuf_addstr(buf, obj_dir);\n>> +       strbuf_addch(buf, '/');\n>>         fill_sha1_path(buf, sha1);\n>>   }\n"},{"id":"336735","messageId":"20180117222707.GA16031@sigill.intra.peff.net","threadId":"47624","inReplyTo":"xmqq4lnkryt2.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v2 2/2] sha1_file: improve sha1_file_name() perfs","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2018-01-17T22:27:07Z","receivedAt":"2018-01-17T22:27:14Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Jan 17, 2018 at 12:54:33PM -0800, Junio C Hamano wrote:\n\n> Jeff Hostetler <git@jeffhostetler.com> writes:\n> \n> >>     void sha1_file_name(struct strbuf *buf, const unsigned char\n> >> *sha1)\n> >>   {\n> >> -\tstrbuf_addf(buf, \"%s/\", get_object_directory());\n> >> +\tconst char *obj_dir = get_object_directory();\n> >> +\tsize_t extra = strlen(obj_dir) + 1 + GIT_MAX_HEXSZ;\n> >\n> > Very minor nit.  Should this be \"+3\" rather than \"+1\"?\n> > One for the slash after obj_dir, one for the slash between\n> > \"xx/y[38]\", and one for the trailing NUL.\n> >\n> >>   +\tif (extra > strbuf_avail(buf))\n> >> +\t\tstrbuf_grow(buf, extra);\n> \n> The callers who care use static strbuf with 1/2, which lets them\n> grow it to an appropriate size after they make their first call.\n> \n> On the other hand, the ones to which performance does not matter by\n> definition do not care.\n> \n> I actually think this whole \"extra -> grow\" business should be\n> discarded.  With a miscomputed \"extra\" like this, it does not help\n> anybody---everybody may pay cost for one extra realloc due to the\n> miscalculation, and the ones that do care also do during their first\n> call.\n\nLet me second that. The diffstat here, along with the magic numbers, is\nnot really encouraging unless we have a demonstrable speedup. In which\ncase we can then measure and compare other approaches, like pushing a\nstatic strbuf farther up the stack. But without that, it feels like\nstumbling around in the dark.\n\n-Peff\n"}]}