git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jacob Keller <jacob.keller@gmail.com>
To: Jeff King <peff@peff.net>
Cc: "Git mailing list" <git@vger.kernel.org>, "René Scharfe" <l.s.r@web.de>
Subject: Re: [PATCH 15/18] fill_sha1_file: write into a strbuf
Date: Mon, 3 Oct 2016 23:44:37 -0700	[thread overview]
Message-ID: <CA+P7+xrVSsNfHo_+tT2+tmXkzAiETVRDVwud-2ADGX8G42W+GQ@mail.gmail.com> (raw)
In-Reply-To: <20161003203609.4hig3e24lyvswdcf@sigill.intra.peff.net>

On Mon, Oct 3, 2016 at 1:36 PM, Jeff King <peff@peff.net> wrote:
> It's currently the responsibility of the caller to give
> fill_sha1_file() enough bytes to write into, leading them to
> manually compute the required lengths. Instead, let's just
> write into a strbuf so that it's impossible to get this
> wrong.

Yea this makes sense.

>
> The alt_odb caller already has a strbuf, so this makes
> things strictly simpler. The other caller, sha1_file_name(),
> uses a static PATH_MAX buffer and dies when it would
> overflow. We can convert this to a static strbuf, which
> means our allocation cost is amortized (and as a bonus, we
> no longer have to worry about PATH_MAX being too short for
> normal use).
>
> This does introduce some small overhead in fill_sha1_file(),
> as each strbuf_addchar() will check whether it needs to
> grow. However, between the optimization in fec501d
> (strbuf_addch: avoid calling strbuf_grow, 2015-04-16) and
> the fact that this is not generally called in a tight loop
> (after all, the next step is typically to access the file!)
> this probably doesn't matter. And even if it did, the right
> place to micro-optimize is inside fill_sha1_file(), by
> calling a single strbuf_grow() there.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
>  sha1_file.c | 34 ++++++++++------------------------
>  1 file changed, 10 insertions(+), 24 deletions(-)
>
> diff --git a/sha1_file.c b/sha1_file.c
> index efc8cee..80a3333 100644
> --- a/sha1_file.c
> +++ b/sha1_file.c
> @@ -172,36 +172,28 @@ enum scld_error safe_create_leading_directories_const(const char *path)
>         return result;
>  }
>
> -static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
> +static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
>  {
>         int i;
>         for (i = 0; i < 20; i++) {
>                 static char hex[] = "0123456789abcdef";
>                 unsigned int val = sha1[i];
> -               *pathbuf++ = hex[val >> 4];
> -               *pathbuf++ = hex[val & 0xf];
> +               strbuf_addch(buf, hex[val >> 4]);
> +               strbuf_addch(buf, hex[val & 0xf]);
>                 if (!i)
> -                       *pathbuf++ = '/';
> +                       strbuf_addch(buf, '/');
>         }
> -       *pathbuf = '\0';
>  }
>
>  const char *sha1_file_name(const unsigned char *sha1)
>  {
> -       static char buf[PATH_MAX];
> -       const char *objdir;
> -       int len;
> +       static struct strbuf buf = STRBUF_INIT;
>
> -       objdir = get_object_directory();
> -       len = strlen(objdir);
> +       strbuf_reset(&buf);
> +       strbuf_addf(&buf, "%s/", get_object_directory());
>
> -       /* '/' + sha1(2) + '/' + sha1(38) + '\0' */
> -       if (len + 43 > PATH_MAX)
> -               die("insanely long object directory %s", objdir);
> -       memcpy(buf, objdir, len);
> -       buf[len] = '/';
> -       fill_sha1_path(buf + len + 1, sha1);
> -       return buf;

I'm definitely a fan of seeing the magic number here go away.

> +       fill_sha1_path(&buf, sha1);
> +       return buf.buf;
>  }
>
>  struct strbuf *alt_scratch_buf(struct alternate_object_database *alt)
> @@ -213,14 +205,8 @@ struct strbuf *alt_scratch_buf(struct alternate_object_database *alt)
>  static const char *alt_sha1_path(struct alternate_object_database *alt,
>                                  const unsigned char *sha1)
>  {
> -       /* hex sha1 plus internal "/" */
> -       size_t len = GIT_SHA1_HEXSZ + 1;
>         struct strbuf *buf = alt_scratch_buf(alt);

Funny story.. While reviewing this code on my screen, my monitor has a
nice little bit of gunk just between the lines that made this one look
like it was being deleted. So I was really confused as to what strbuf
you were using and why you removed a call to alt_scratch_buf()..
Obviously this line just isn't being removed.

> -
> -       strbuf_grow(buf, len);
> -       fill_sha1_path(buf->buf + buf->len, sha1);
> -       strbuf_setlen(buf, buf->len + len);
> -
> +       fill_sha1_path(buf, sha1);
>         return buf->buf;
>  }
>
> --
> 2.10.0.618.g82cc264
>

  reply	other threads:[~2016-10-04  6:45 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-03 20:33 [PATCH 0/18] alternate object database cleanups Jeff King
2016-10-03 20:33 ` [PATCH 01/18] t5613: drop reachable_via function Jeff King
2016-10-04  5:48   ` Jacob Keller
2016-10-04 13:43     ` Jeff King
2016-10-03 20:33 ` [PATCH 02/18] t5613: drop test_valid_repo function Jeff King
2016-10-04  5:50   ` Jacob Keller
2016-10-03 20:34 ` [PATCH 03/18] t5613: use test_must_fail Jeff King
2016-10-04  5:51   ` Jacob Keller
2016-10-03 20:34 ` [PATCH 04/18] t5613: whitespace/style cleanups Jeff King
2016-10-04  5:52   ` Jacob Keller
2016-10-04 13:47     ` Jeff King
2016-10-04 20:41       ` Jacob Keller
2016-10-03 20:34 ` [PATCH 05/18] t5613: do not chdir in main process Jeff King
2016-10-04  5:54   ` Jacob Keller
2016-10-04 21:00   ` Junio C Hamano
2016-10-03 20:34 ` [PATCH 06/18] t5613: clarify "too deep" recursion tests Jeff King
2016-10-04  5:57   ` Jacob Keller
2016-10-04 13:48     ` Jeff King
2016-10-04 20:44       ` Jacob Keller
2016-10-04 20:49         ` Jeff King
2016-10-04 20:52           ` Jacob Keller
2016-10-04 20:55             ` Jeff King
2016-10-04 20:58               ` Stefan Beller
2016-10-04 21:00                 ` Jeff King
2016-10-05 13:58                 ` Jakub Narębski
2016-10-05 14:40                   ` Jeff King
2016-10-05 16:14                     ` Junio C Hamano
2016-10-05 16:47                     ` Jacob Keller
2016-10-04 21:43               ` Jacob Keller
2016-10-04 21:49                 ` Jeff King
2016-10-04 21:50                   ` Jacob Keller
2016-10-03 20:34 ` [PATCH 07/18] link_alt_odb_entry: handle normalize_path errors Jeff King
2016-10-04  6:01   ` Jacob Keller
2016-10-04 21:08   ` Junio C Hamano
2016-10-05 18:47   ` René Scharfe
2016-10-05 19:04     ` Jeff King
2016-11-07 23:42   ` Bryan Turner
2016-11-08  0:30     ` Jeff King
2016-11-08  1:12       ` Bryan Turner
2016-11-08  5:33         ` Jeff King
2016-11-08 19:27           ` Bryan Turner
2016-10-03 20:34 ` [PATCH 08/18] link_alt_odb_entry: refactor string handling Jeff King
2016-10-04  6:05   ` Jacob Keller
2016-10-04 13:53     ` Jeff King
2016-10-04 20:46       ` Jacob Keller
2016-10-04 21:18   ` Junio C Hamano
2016-10-03 20:35 ` [PATCH 09/18] alternates: provide helper for adding to alternates list Jeff King
2016-10-04  6:07   ` Jacob Keller
2016-10-03 20:35 ` [PATCH 10/18] alternates: provide helper for allocating alternate Jeff King
2016-10-04  6:09   ` Jacob Keller
2016-10-03 20:35 ` [PATCH 11/18] alternates: encapsulate alt->base munging Jeff King
2016-10-03 20:35 ` [PATCH 12/18] alternates: use a separate scratch space Jeff King
2016-10-04  6:12   ` Jacob Keller
2016-10-04 21:29   ` Junio C Hamano
2016-10-04 21:32     ` Jeff King
2016-10-04 21:49       ` Junio C Hamano
2016-10-04 21:51         ` Jeff King
2016-10-03 20:35 ` [PATCH 13/18] fill_sha1_file: write "boring" characters Jeff King
2016-10-04  6:13   ` Jacob Keller
2016-10-04 21:46     ` Junio C Hamano
2016-10-04 21:48       ` Jeff King
2016-10-04 21:49       ` Jacob Keller
2016-10-05 19:35         ` Junio C Hamano
2016-10-03 20:36 ` [PATCH 14/18] alternates: store scratch buffer as strbuf Jeff King
2016-10-03 20:36 ` [PATCH 15/18] fill_sha1_file: write into a strbuf Jeff King
2016-10-04  6:44   ` Jacob Keller [this message]
2016-10-03 20:36 ` [PATCH 16/18] count-objects: report alternates via verbose mode Jeff King
2016-10-04  6:46   ` Jacob Keller
2016-10-04 13:56     ` Jeff King
2016-10-05 14:23   ` Jakub Narębski
2016-10-05 18:47   ` René Scharfe
2016-10-03 20:36 ` [PATCH 17/18] sha1_file: always allow relative paths to alternates Jeff King
2016-10-04  6:50   ` Jacob Keller
2016-10-04 14:00     ` Jeff King
2016-10-03 20:36 ` [PATCH 18/18] alternates: use fspathcmp to detect duplicates Jeff King
2016-10-04  6:51   ` Jacob Keller
2016-10-04 14:10     ` Jeff King
2016-10-04 21:42   ` Junio C Hamano
2016-10-05  2:34   ` Aaron Schrab
2016-10-05  3:54     ` Jeff King
2016-10-04  5:47 ` [PATCH 0/18] alternate object database cleanups Jacob Keller
2016-10-04 13:41   ` Jeff King
2016-10-04 20:40     ` Jacob Keller
2016-10-05 18:47 ` René Scharfe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+P7+xrVSsNfHo_+tT2+tmXkzAiETVRDVwud-2ADGX8G42W+GQ@mail.gmail.com \
    --to=jacob.keller@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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).