From: Michael Haggerty <mhagger@alum.mit.edu>
To: Jeff King <peff@peff.net>
Cc: "Martin Ågren" <martin.agren@gmail.com>,
"Git Mailing List" <git@vger.kernel.org>
Subject: Re: [PATCH v3 1/3] refs/files-backend: add longer-scoped copy of string to list
Date: Tue, 5 Sep 2017 11:03:36 +0200 [thread overview]
Message-ID: <CAMy9T_F5HWF=hinxdp0C+pgyH_8MnR31p=XK1rjyMiM3UwST_w@mail.gmail.com> (raw)
In-Reply-To: <20170905084507.wntm3bfzfezgimcz@sigill.intra.peff.net>
On Tue, Sep 5, 2017 at 10:45 AM, Jeff King <peff@peff.net> wrote:
> On Tue, Aug 29, 2017 at 07:18:22PM +0200, Martin Ågren wrote:
>
>> [...]
>> Rearrange the handling of `referent`, so that we don't add it directly
>> to `affected_refnames`. Instead, first just check whether `referent`
>> exists in the string list, and later add `new_update->refname`.
>
> Coincidentally[1] I came across this same leak, and my solution ended up
> slightly different. I'll share it here in case it's of interest.
>
> In your solution we end up searching the string list twice: once to see
> if we have the item, and then again to insert it. Whereas in the
> original we did both with a single search.
>
> But we can observe that either:
>
> 1. The item already existed, in which case our insert was a noop, and
> we're good.
>
> or
>
> 2. We inserted it, in which case we proceed with creating new_update.
>
> We can then in O(1) replace the pointer in the string list item
> with the storage in new_update. We know we're not violating any
> string_list invariants because the strings contain the same bytes.
>
> I.e.:
>
> diff --git a/refs/files-backend.c b/refs/files-backend.c
> index 9266f5ab9d..1d16c1b33e 100644
> --- a/refs/files-backend.c
> +++ b/refs/files-backend.c
> @@ -2178,6 +2178,11 @@ static int split_symref_update(struct files_ref_store *refs,
> update->flags |= REF_LOG_ONLY | REF_NODEREF;
> update->flags &= ~REF_HAVE_OLD;
>
> + /*
> + * Re-point at the storage provided by our ref_update, which we know
> + * will last as long as the affected_refnames list.
> + */
> + item->string = new_update->refname;
> item->util = new_update;
>
> return 0;
>
> It feels pretty dirty, though. It would certainly be a bug if we ever
> decided to switch affected_refnames to duplicate its strings.
>
> So given that your solution is only a constant-time factor worse in
> efficiency, we should probably prefer it as the more maintainable
> option.
This is clever, but I don't like that it requires outside code to
change internal `string_list` structures in a way that is not
documented to be OK.
If we cared about getting rid of the extra `O(lg N)` search (and I
agree with you that it doesn't matter in this case), I think the clean
way to do it would be for `string_list` to expose a method like
struct string_list_item *string_list_insert_at_index(struct
string_list *list, size_t index, const char *string);
and to use it, together with `string_list_find_insert_index()`, to
avoid having to search twice.
Michael
next prev parent reply other threads:[~2017-09-05 9:03 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-25 18:49 [PATCH 1/2] refs/files-backend: duplicate strings added to affected_refnames Martin Ågren
2017-08-25 18:49 ` [PATCH 2/2] refs/files-backend: fix memory leak in lock_ref_for_update Martin Ågren
2017-08-25 21:00 ` [PATCH 1/2] refs/files-backend: duplicate strings added to affected_refnames Junio C Hamano
2017-08-26 10:16 ` Martin Ågren
2017-08-28 8:06 ` Michael Haggerty
2017-08-28 10:09 ` Martin Ågren
2017-08-28 20:32 ` [PATCH v2 1/2] refs/files-backend: add longer-scoped copy of string to list Martin Ågren
2017-08-29 8:33 ` Michael Haggerty
2017-08-28 20:32 ` [PATCH v2 2/2] refs/files-backend: fix memory leak in lock_ref_for_update Martin Ågren
2017-08-29 8:39 ` Michael Haggerty
2017-08-29 10:41 ` Martin Ågren
2017-08-29 17:18 ` [PATCH v3 1/3] refs/files-backend: add longer-scoped copy of string to list Martin Ågren
2017-08-30 2:52 ` Michael Haggerty
2017-08-30 18:02 ` Martin Ågren
2017-09-05 10:02 ` Junio C Hamano
2017-09-05 17:24 ` Martin Ågren
2017-09-05 20:36 ` Jeff King
2017-09-05 21:26 ` Junio C Hamano
2017-09-06 18:12 ` Martin Ågren
2017-09-06 19:52 ` Junio C Hamano
2017-09-06 23:45 ` Jeff King
2017-09-09 6:57 ` [PATCH v4 0/4] Rerolling ma/split-symref-update-fix Martin Ågren
2017-09-09 6:57 ` [PATCH v4 1/4] refs/files-backend: add longer-scoped copy of string to list Martin Ågren
2017-09-09 6:57 ` [PATCH v4 2/4] refs/files-backend: fix memory leak in lock_ref_for_update Martin Ågren
2017-09-09 6:57 ` [PATCH v4 3/4] refs/files-backend: correct return value " Martin Ågren
2017-09-09 6:57 ` [PATCH v4 4/4] refs/files-backend: add `refname`, not "HEAD", to list Martin Ågren
2017-09-09 10:47 ` [PATCH v4 0/4] Rerolling ma/split-symref-update-fix Jeff King
2017-09-05 8:45 ` [PATCH v3 1/3] refs/files-backend: add longer-scoped copy of string to list Jeff King
2017-09-05 9:03 ` Michael Haggerty [this message]
2017-09-05 9:04 ` Jeff King
2017-08-29 17:18 ` [PATCH v3 2/3] refs/files-backend: fix memory leak in lock_ref_for_update Martin Ågren
2017-09-05 8:47 ` Jeff King
2017-09-05 17:28 ` Martin Ågren
2017-08-29 17:18 ` [PATCH v3 3/3] refs/files-backend: correct return value " Martin Ågren
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='CAMy9T_F5HWF=hinxdp0C+pgyH_8MnR31p=XK1rjyMiM3UwST_w@mail.gmail.com' \
--to=mhagger@alum.mit.edu \
--cc=git@vger.kernel.org \
--cc=martin.agren@gmail.com \
--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).