Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-10-02 Thread Jeff King
On Sat, Sep 30, 2017 at 09:09:11PM +0300, Оля Тележная wrote:

> I added "v2" after "PATCH", but it does not appeared. Actually it was
> written automatically and it was "PATCH Outreachy v2". I rearranged it
> in the middle of the phrase.

That looks fine.

> > I'm not sure what you mean about checking in mru_clear(). It may make
> > sense to just send your v2 patch and I can see there what you do.
> 
> I realized that I said something strange before. I solved the problem
> with leak by deleting INIT in prepare_packed_git and adding init as
> you suggested here:
> https://github.com/telezhnaya/git/commit/7f8995835949f83e54efdfd88445feb6d54cb3e9#commitcomment-24576103

OK, that makes sense.

> By the way, I asked to edit FAQ for Linux kernel newbies about linked
> list that confused me a week ago, and that funny picture was deleted.
> https://kernelnewbies.org/FAQ/LinkedLists
> Maybe it will help to someone else :)

Thank you. :)

Remember (and this applies to other Outreachy folks, too) to submit your
application in the Outreachy system:

   https://outreachy.gnome.org

-Peff


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-30 Thread Оля Тележная
I added "v2" after "PATCH", but it does not appeared. Actually it was
written automatically and it was "PATCH Outreachy v2". I rearranged it
in the middle of the phrase.

>> I forgot about leak. I also need to add checking in mru_clear. That's
>> not beautiful solution but it works reliably.
>
> I'm not sure what you mean about checking in mru_clear(). It may make
> sense to just send your v2 patch and I can see there what you do.

I realized that I said something strange before. I solved the problem
with leak by deleting INIT in prepare_packed_git and adding init as
you suggested here:
https://github.com/telezhnaya/git/commit/7f8995835949f83e54efdfd88445feb6d54cb3e9#commitcomment-24576103

By the way, I asked to edit FAQ for Linux kernel newbies about linked
list that confused me a week ago, and that funny picture was deleted.
https://kernelnewbies.org/FAQ/LinkedLists
Maybe it will help to someone else :)


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-29 Thread Junio C Hamano
Jeff King  writes:

> Yes, I think we could just call this "list_move_to_front()" or
> something. The fact that it's operating on a list called
> "packed_git_mru" is probably sufficient to make it clear that the
> purpose is managing recentness.

I earlier said I wasn't sure, but I fully agree with your envisioned
endgame state, my understanding of which is that we will not have an
API that is only to be used to manage a MRU list (hence there will
be no mru.[ch] in that future), as there is very small MRU-specific
operation to be offered anyway.  Namely, mru_mark() that is to be
used to "mark the fact that the item was used---do whatever necessary
to maintain the MRU list".

Instead, everybody understands how the list API works, and the fact
that one instance of list_head is called MRU is sufficient to see
that use of "move this to the front of the list" function means we
are marking the item as most-recently-used.

Thanks.


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-29 Thread Jeff King
On Fri, Sep 29, 2017 at 11:38:27PM +0300, Оля Тележная wrote:

> > About minor issues ( "tmp" vs "p2", variable scope, space indentation)
> > - fully agree, I will fix it.
> > ...
> > So finally I think that I need to fix that minor issues and that's
> > all.
> 
> I forgot about leak. I also need to add checking in mru_clear. That's
> not beautiful solution but it works reliably.

I'm not sure what you mean about checking in mru_clear(). It may make
sense to just send your v2 patch and I can see there what you do.

> Question remains the same - do you see something more to fix in this patch?

I think that's it. Think about writing a bit in the commit message about
the "why" of this. Like I said earlier, the rationale for this commit is
perhaps obvious, but it never hurts to be explicit (and it may be good
practice, since part of the point of this task is getting you familiar
with the patch submission process).

-Peff


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-29 Thread Jeff King
On Fri, Sep 29, 2017 at 07:08:28PM +0300, Оля Тележная wrote:

> Many thanks to all of you, I am interested in every opinion. Sorry
> that I wasn't in the discussion, unfortunately I got sick, that's why
> I skipped all the process.

No problem. It's often reasonable to let review comments come in and
think about them as a whole before responding or re-posting anyway.

> > An overlong line (I can locally wrap it, so the patch does not have
> > to be re-sent only to fix this alone).
> I've read only about 50 characters max in commit head (and
> highlighting repeats it), but there's nothing about max length of line
> in commit message. Sorry, next time I will make it shorter.

Usually we shoot for making things look good in an 80-column terminal,
including both code and commit messages. For commit message bodies, we
tend to make them a little shorter, since "git log" will indent them. 72
characters is reasonable there. And we tend to make subject lines a
little shorter than that, even since they often appear with "Subject:"
and "[PATCH]" prefixed. I usually go for about 60 characters, but will
go over if it helps make the subject more clear.

> > I had envisioned leaving mru_mark() as a wrapper for "move to the front"
> > that could operate on any list. But seeing how Olga's patch takes it
> > down to two trivial lines, I'd also be fine with an endgame that just
> > eliminates it.
> Let's add needed function to list.h directly?

Yes, I think we could just call this "list_move_to_front()" or
something. The fact that it's operating on a list called
"packed_git_mru" is probably sufficient to make it clear that the
purpose is managing recentness.

> I also wanted to add
> list_for_each_entry function to list.h as it's in Linux kernel.
> https://www.kernel.org/doc/htmldocs/kernel-api/API-list-for-each-entry.html
> It will simplify the code even more, guess that not only in MRU
> related code. Maybe we need to do that in separate patch.

It would be nice to have list_for_each_entry(), but unfortunately it's
not portable. It relies on having a typeof operator, and we build on
platforms that lack it. It was omitted in 94e99012fc (http-walker:
reduce O(n) ops with doubly-linked list, 2016-07-11) for that reason.

> About minor issues ( "tmp" vs "p2", variable scope, space indentation)
> - fully agree, I will fix it.

Thanks.

> So finally I think that I need to fix that minor issues and that's
> all. I have plans to rewrite (with --amend) my current commit (I think
> so because I will add no new features, so it's better to have single
> commit for all changes).
> As I understand, Submitgit will send an update in a new thread. And I
> need to say there [PATCH v2].
> Please correct me if I am wrong in any of the moments mentioned earlier.

Correct. Until a patch is merged to Junio's "next" branch (at which
point it is set in stone), we generally prefer to rewrite it with
"--amend" (or git-rebase) to fix anything that comes up during the
review.

> By the way, other contributors write smth like "[PATCH v6 0/3]". What
> does mean "0/3"? It's about editing separate commits in a single
> patch, am I right?

Right, it means multiple commits in a logical series that are meant to
be applied together. Your patch is small enough that it makes as a
single patch. If we wanted to do the second step of dropping mru.[ch]
entirely now, then you'd probably have at least a 2-patch series.

(I'm OK with not doing that second step for now, though if we are not
going to polish up the mru_for_each() interface, it may make sense to
make the final step sooner rather than later).

-Peff


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-29 Thread Оля Тележная
> About minor issues ( "tmp" vs "p2", variable scope, space indentation)
> - fully agree, I will fix it.
> ...
> So finally I think that I need to fix that minor issues and that's
> all.

I forgot about leak. I also need to add checking in mru_clear. That's
not beautiful solution but it works reliably.
Question remains the same - do you see something more to fix in this patch?

Thank you!
Olga


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-29 Thread Оля Тележная
Hi everyone,
Many thanks to all of you, I am interested in every opinion. Sorry
that I wasn't in the discussion, unfortunately I got sick, that's why
I skipped all the process.
I want to reply to the main moments and also ask some questions.

>> Simplify mru.c, mru.h and related code by reusing the double-linked list 
>> implementation from list.h instead of a custom one.
> An overlong line (I can locally wrap it, so the patch does not have
> to be re-sent only to fix this alone).
I've read only about 50 characters max in commit head (and
highlighting repeats it), but there's nothing about max length of line
in commit message. Sorry, next time I will make it shorter.

About many different opinions how to improve the code: I agree with
the idea that my commit is a middle step to get rid of MRU at all. If
we really need to add initializer/mru_for_each/smth_else - it's
absolutely not a problem, but as it was said, not sure that we need
it.
It really looks that using list implementation from list.h directly
won't be worse.

> I had envisioned leaving mru_mark() as a wrapper for "move to the front"
> that could operate on any list. But seeing how Olga's patch takes it
> down to two trivial lines, I'd also be fine with an endgame that just
> eliminates it.
Let's add needed function to list.h directly? I also wanted to add
list_for_each_entry function to list.h as it's in Linux kernel.
https://www.kernel.org/doc/htmldocs/kernel-api/API-list-for-each-entry.html
It will simplify the code even more, guess that not only in MRU
related code. Maybe we need to do that in separate patch.

About minor issues ( "tmp" vs "p2", variable scope, space indentation)
- fully agree, I will fix it.

So finally I think that I need to fix that minor issues and that's
all. I have plans to rewrite (with --amend) my current commit (I think
so because I will add no new features, so it's better to have single
commit for all changes).
As I understand, Submitgit will send an update in a new thread. And I
need to say there [PATCH v2].
Please correct me if I am wrong in any of the moments mentioned earlier.

By the way, other contributors write smth like "[PATCH v6 0/3]". What
does mean "0/3"? It's about editing separate commits in a single
patch, am I right?

Thank you one more time!
Olga


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-29 Thread Christian Couder
On Fri, Sep 29, 2017 at 9:23 AM, Jeff King  wrote:
> On Fri, Sep 29, 2017 at 09:18:11AM +0200, Christian Couder wrote:
>
>> As we use the "prepare_packed_git_run_once" static, this function will
>> only be called only once when packed_git_mru has not yet been
>> initialized, so there will be no leak.
>
> Check reprepare_packed_git(). It unsets the run_once flag, and then
> calls prepare_packed_git() again.

Ah ok.


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-29 Thread Jeff King
On Fri, Sep 29, 2017 at 09:18:11AM +0200, Christian Couder wrote:

> On Fri, Sep 29, 2017 at 12:42 AM, Jeff King  wrote:
> > On Thu, Sep 28, 2017 at 08:38:39AM +, Olga Telezhnaya wrote:
> >
> >> diff --git a/packfile.c b/packfile.c
> >> index f69a5c8d607af..ae3b0b2e9c09a 100644
> >> --- a/packfile.c
> >> +++ b/packfile.c
> >> @@ -876,6 +876,7 @@ void prepare_packed_git(void)
> >>   for (alt = alt_odb_list; alt; alt = alt->next)
> >>   prepare_packed_git_one(alt->path, 0);
> >>   rearrange_packed_git();
> >> + INIT_LIST_HEAD(_git_mru.list);
> >>   prepare_packed_git_mru();
> >>   prepare_packed_git_run_once = 1;
> >>  }
> >
> > I was thinking on this hunk a bit more, and I think it's not quite
> > right.
> >
> > The prepare_packed_git_mru() function will clear the mru list and then
> > re-add each item from the packed_git list. But by calling
> > INIT_LIST_HEAD() here, we're effectively clearing the packed_git_mru
> > list, and we end up leaking whatever was on the list before.
> 
> The current code is:
> 
> static int prepare_packed_git_run_once = 0;
> void prepare_packed_git(void)
> {
> struct alternate_object_database *alt;
> 
> if (prepare_packed_git_run_once)
> return;
> prepare_packed_git_one(get_object_directory(), 1);
> prepare_alt_odb();
> for (alt = alt_odb_list; alt; alt = alt->next)
> prepare_packed_git_one(alt->path, 0);
> rearrange_packed_git();
> prepare_packed_git_mru();
> prepare_packed_git_run_once = 1;
> }
> 
> As we use the "prepare_packed_git_run_once" static, this function will
> only be called only once when packed_git_mru has not yet been
> initialized, so there will be no leak.

Check reprepare_packed_git(). It unsets the run_once flag, and then
calls prepare_packed_git() again.

-Peff


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-29 Thread Christian Couder
On Fri, Sep 29, 2017 at 12:42 AM, Jeff King  wrote:
> On Thu, Sep 28, 2017 at 08:38:39AM +, Olga Telezhnaya wrote:
>
>> diff --git a/packfile.c b/packfile.c
>> index f69a5c8d607af..ae3b0b2e9c09a 100644
>> --- a/packfile.c
>> +++ b/packfile.c
>> @@ -876,6 +876,7 @@ void prepare_packed_git(void)
>>   for (alt = alt_odb_list; alt; alt = alt->next)
>>   prepare_packed_git_one(alt->path, 0);
>>   rearrange_packed_git();
>> + INIT_LIST_HEAD(_git_mru.list);
>>   prepare_packed_git_mru();
>>   prepare_packed_git_run_once = 1;
>>  }
>
> I was thinking on this hunk a bit more, and I think it's not quite
> right.
>
> The prepare_packed_git_mru() function will clear the mru list and then
> re-add each item from the packed_git list. But by calling
> INIT_LIST_HEAD() here, we're effectively clearing the packed_git_mru
> list, and we end up leaking whatever was on the list before.

The current code is:

static int prepare_packed_git_run_once = 0;
void prepare_packed_git(void)
{
struct alternate_object_database *alt;

if (prepare_packed_git_run_once)
return;
prepare_packed_git_one(get_object_directory(), 1);
prepare_alt_odb();
for (alt = alt_odb_list; alt; alt = alt->next)
prepare_packed_git_one(alt->path, 0);
rearrange_packed_git();
prepare_packed_git_mru();
prepare_packed_git_run_once = 1;
}

As we use the "prepare_packed_git_run_once" static, this function will
only be called only once when packed_git_mru has not yet been
initialized, so there will be no leak.


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-28 Thread Jeff King
On Thu, Sep 28, 2017 at 08:38:39AM +, Olga Telezhnaya wrote:

> diff --git a/packfile.c b/packfile.c
> index f69a5c8d607af..ae3b0b2e9c09a 100644
> --- a/packfile.c
> +++ b/packfile.c
> @@ -876,6 +876,7 @@ void prepare_packed_git(void)
>   for (alt = alt_odb_list; alt; alt = alt->next)
>   prepare_packed_git_one(alt->path, 0);
>   rearrange_packed_git();
> + INIT_LIST_HEAD(_git_mru.list);
>   prepare_packed_git_mru();
>   prepare_packed_git_run_once = 1;
>  }

I was thinking on this hunk a bit more, and I think it's not quite
right.

The prepare_packed_git_mru() function will clear the mru list and then
re-add each item from the packed_git list. But by calling
INIT_LIST_HEAD() here, we're effectively clearing the packed_git_mru
list, and we end up leaking whatever was on the list before.

So for the first call to prepare_packed_git, we really need this
INIT_LIST_HEAD() call. But for subsequent calls (which come from
reprepare_packed_git()), we must not call it.

There are a few ways to work around it that I can think of:

  1. Check whether packed_git_mru.list.head is NULL, and only initialize
 in that case.

  2. Use a static initializer for packed_git_mru.list, so that we don't
 have do the first-time initializing here.

  3. Teach reprepare_packed_git() to do the mru_clear() call, so that we
 know the list is empty when we get here.

One final and more invasive option is to stop regenerating the
packed_git_mru list from scratch during each prepare_packed_git(). I did
it that way so that we start with the same order that
rearrange_packed_git() will give us, but I'm not sure how much value
that has in practice (it probably had a lot more when we didn't have the
mru, and the time-sorted pack order helped find recent objects more
quickly).

The alternative would be to just teach install_packed_git() to add each
newly-added pack to the mru list, and then never clear the list (and we
wouldn't need an mru_clear() at all, then).

-Peff


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-28 Thread Jeff King
On Fri, Sep 29, 2017 at 06:56:28AM +0900, Junio C Hamano wrote:

> Jeff King  writes:
> 
> > But I also think this patch may be a stepping stone to dropping "struct
> > mru" entirely, and just pushing a "struct list_head mru" into the
> > packed_git object itself (or of course any object you like). At which
> > point we'd just directly use the list iterators anyway.
> 
> The endgame state implied by your statement would mean that we won't
> have mru_mark() and instead have these open-coded in terms of the
> two calls into the list API.  There only are two callers of it in
> the current system, so it is not the end of the world, so I guess I
> can agree that this is a good preparation step toward the longer
> term goal, which says "mru API is over-engineered and what it offers
> over the plain vanilla list API is almost never used except for a
> few callsite; let's remove it and use the bare list API instead".

Thanks, that last sentence is a good summary of my thinking (I think
what I find most silly about it is that it allocates a whole extra
struct per item, which is where most of the complication comes from).

I had envisioned leaving mru_mark() as a wrapper for "move to the front"
that could operate on any list. But seeing how Olga's patch takes it
down to two trivial lines, I'd also be fine with an endgame that just
eliminates it.

> > I'd make the same claims here as above (both that I agree your proposed
> > interface looks nicer, but also that I think we eventually do want to
> > expose that this is tightly coupled with list.h).
> 
> I agree.  I just do not yet know if I fully embrace the idea that we
> just should use bare list API, getting rid of the mru API.

Fair enough.

-Peff


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-28 Thread Junio C Hamano
Jeff King  writes:

> But I also think this patch may be a stepping stone to dropping "struct
> mru" entirely, and just pushing a "struct list_head mru" into the
> packed_git object itself (or of course any object you like). At which
> point we'd just directly use the list iterators anyway.

The endgame state implied by your statement would mean that we won't
have mru_mark() and instead have these open-coded in terms of the
two calls into the list API.  There only are two callers of it in
the current system, so it is not the end of the world, so I guess I
can agree that this is a good preparation step toward the longer
term goal, which says "mru API is over-engineered and what it offers
over the plain vanilla list API is almost never used except for a
few callsite; let's remove it and use the bare list API instead".

> I'd make the same claims here as above (both that I agree your proposed
> interface looks nicer, but also that I think we eventually do want to
> expose that this is tightly coupled with list.h).

I agree.  I just do not yet know if I fully embrace the idea that we
just should use bare list API, getting rid of the mru API.



Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-28 Thread Jeff King
On Thu, Sep 28, 2017 at 08:38:39AM +, Olga Telezhnaya wrote:

> Simplify mru.c, mru.h and related code by reusing the double-linked
> list implementation from list.h instead of a custom one.

The commit message is a good reason to talk about why we want to do
this. In this case, the answer may be fairly obvious. But I sometimes
find that things that are obvious to me as the patch author are not
quite as obvious to people reading it later (either reviewing, or six
months from now when they are hunting the cause of a bug).

> -void mru_mark(struct mru *mru, struct mru_entry *entry)
> +void mru_mark(struct mru *head, struct mru *entry)
>  {
> - /* If we're already at the front of the list, nothing to do */
> - if (mru->head == entry)
> - return;
> -
> - /* Otherwise, remove us from our current slot... */
> - if (entry->prev)
> - entry->prev->next = entry->next;
> - if (entry->next)
> - entry->next->prev = entry->prev;
> - else
> - mru->tail = entry->prev;
> -
> - /* And insert us at the beginning. */
> - entry->prev = NULL;
> - entry->next = mru->head;
> - if (mru->head)
> - mru->head->prev = entry;
> - mru->head = entry;
> + /* To mark means to put at the front of the list. */
> + list_del(>list);
> + list_add(>list, >list);
>  }

Nice, this hunk is very satisfying. :)

> -void mru_clear(struct mru *mru)
> +void mru_clear(struct mru *head)
>  {
> - struct mru_entry *p = mru->head;
> -
> - while (p) {
> - struct mru_entry *to_free = p;
> - p = p->next;
> + struct list_head *p1;
> + struct list_head *p2;
> + struct mru *to_free;
> + 
> + list_for_each_safe(p1, p2, >list) {
> + to_free = list_entry(p1, struct mru, list);
>   free(to_free);
>   }
> - mru->head = mru->tail = NULL;
> + INIT_LIST_HEAD(>list);

Two minor style comments here:

  - Perhaps "tmp" is a better name than "p2" for the second argument of
a list_for_each_safe, as it makes it less likely to confuse p1 and
p2 (though admittedly the whole function is short enough that it
probably doesn't matter much either way).

  - It's a good practice to declare variables in the smallest scope
possible. So I think the declaration of to_free could go inside the
loop.

You could actually get rid of it entirely with:

  free(list_entry(p1, struct mru, list));

but I certainly don't mind using a variable for better readability.

> @@ -29,17 +28,13 @@
>   * you will begin traversing the whole list again.
>   */
>  
> -struct mru_entry {
> - void *item;
> - struct mru_entry *prev, *next;
> -};
> -
>  struct mru {
> - struct mru_entry *head, *tail;
> + struct list_head list;
> +void *item;
>  };

The decision to get rid of the "mru versus mru_entry" distinction
surprised me a little. In the original, a "struct mru" represented the
whole list. In the list.h implementation, a "struct list_head" serves
that purpose, as a sentinel value. But that sentinel doesn't need to
have an "item", right? I.e., we could have:

  struct mru {
  struct list_head head;
  };

  struct mru_entry {
  void *item;
  struct list_head list;
  };

As I said in my response to Junio (and as we discussed a little
off-list), I think we can eventually move to having no structs at all
(just list_heads embedded inside the existing packfile objects). At
which point the user of the API would just declare:

  LIST_HEAD(packed_git_mru);

themselves. So I'm actually fine with this direction if we're using it
as the "middle step" that I mentioned there.

>  struct mru {
> - struct mru_entry *head, *tail;
> + struct list_head list;
> +void *item;
>  };

The funny indentation in this diff shows that "void *item" is indented
with spaces, not a tab.

> [...]

I pointed out a few minor bits, but overall this is looking very strong.
Great work!

-Peff


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-28 Thread Jeff King
On Thu, Sep 28, 2017 at 08:03:00PM +0900, Junio C Hamano wrote:

> > -   for (entry = packed_git_mru.head; entry; entry = entry->next) {
> > +   list_for_each(pos, _git_mru.list) {
> > +   struct mru *entry = list_entry(pos, struct mru, list);
> > struct packed_git *p = entry->item;
> > off_t offset;
> 
> I was a bit surprised to see a change outside mru.[ch] like this
> one.  The reason why I was surprised was because I expected mru.[ch]
> would offer its own API that encapsulates enumeration like this one
> and this patch would just be reimplementing that API using the list
> API, instead of rewriting the users of mru API to directly access
> the list API.
> 
> Alas, there is no such mru API that lets a mru user to iterate over
> elements, so the original of the above code were using mru's
> implementation detail directly.
> 
> We probably want to invent mru_for_each() that hides the fact that
> mru is implemented in terms of list_head from the users of mru API
> and use it here.

I agree that the caller would be a little shorter with an mru-specific
iterator (e.g., we could probably do the list_entry() part
automatically).

But I also think this patch may be a stepping stone to dropping "struct
mru" entirely, and just pushing a "struct list_head mru" into the
packed_git object itself (or of course any object you like). At which
point we'd just directly use the list iterators anyway.

(One could argue that if that's our end goal, we could go straight
there. But I think this middle state has value, because the individual
steps are easier to verify).

> > @@ -8,18 +10,15 @@
> >   *
> >   *   // Create a list.  Zero-initialization is required.
> >   *   static struct mru cache;
> > - *   mru_append(, item);
> > - *   ...
> > + *   INIT_LIST_HEAD();
> 
> "Zero-initialization is required." is no longer true, it seems, and
> the comment above needs to be updated, right?
> 
> More importantly, this leaks to the user of the API the fact that
> mru is internally implemented in terms of the list API, which is
> not necessary (when we want to update the implementation later, we'd
> need to update all the users again).  Perhaps you'd want
> 
>   INIT_MRU(cache);
> 
> which is #define'd in this file, perhaps like so:
> 
>   #define INIT_MRU(mru)   INIT_LIST_HEAD(&((mru).list))

I'd make the same claims here as above (both that I agree your proposed
interface looks nicer, but also that I think we eventually do want to
expose that this is tightly coupled with list.h).

-Peff


Re: [PATCH Outreachy] mru: use double-linked list from list.h

2017-09-28 Thread Junio C Hamano
Olga Telezhnaya  writes:

> Simplify mru.c, mru.h and related code by reusing the double-linked list 
> implementation from list.h instead of a custom one.

An overlong line (I can locally wrap it, so the patch does not have
to be re-sent only to fix this alone).

> Signed-off-by: Olga Telezhnaia 

Thanks for making your "From:" name match the "Signed-off-by:" name;
anglicising like you did is probably more friendly to the readers
than writing both in Cyrillic, which is another valid way to make
them match.

> Mentored-by: Christian Couder 
> Mentored by: Jeff King 
> ---
>  builtin/pack-objects.c |  5 +++--
>  mru.c  | 51 
> +++---
>  mru.h  | 31 +-
>  packfile.c |  6 --
>  4 files changed, 35 insertions(+), 58 deletions(-)

As Christian mentioned earlier, nice line reduction ;-)

> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
> index f721137eaf881..ba812349e0aab 100644
> --- a/builtin/pack-objects.c
%> +++ b/builtin/pack-objects.c
> @@ -995,8 +995,8 @@ static int want_object_in_pack(const unsigned char *sha1,
>  struct packed_git **found_pack,
>  off_t *found_offset)
>  {
> - struct mru_entry *entry;
>   int want;
> + struct list_head *pos;
>  
>   if (!exclude && local && has_loose_object_nonlocal(sha1))
>   return 0;
> @@ -1012,7 +1012,8 @@ static int want_object_in_pack(const unsigned char 
> *sha1,
>   return want;
>   }
>  
> - for (entry = packed_git_mru.head; entry; entry = entry->next) {
> + list_for_each(pos, _git_mru.list) {
> + struct mru *entry = list_entry(pos, struct mru, list);
>   struct packed_git *p = entry->item;
>   off_t offset;

I was a bit surprised to see a change outside mru.[ch] like this
one.  The reason why I was surprised was because I expected mru.[ch]
would offer its own API that encapsulates enumeration like this one
and this patch would just be reimplementing that API using the list
API, instead of rewriting the users of mru API to directly access
the list API.

Alas, there is no such mru API that lets a mru user to iterate over
elements, so the original of the above code were using mru's
implementation detail directly.

We probably want to invent mru_for_each() that hides the fact that
mru is implemented in terms of list_head from the users of mru API
and use it here.

> diff --git a/mru.c b/mru.c
> index 9dedae0287ed2..8b6ba3d9b7fad 100644
> --- a/mru.c
> +++ b/mru.c
> @@ -1,50 +1,29 @@
>  #include "cache.h"
>  #include "mru.h"
>  
> -void mru_append(struct mru *mru, void *item)
> +void mru_append(struct mru *head, void *item)
>  {
> - struct mru_entry *cur = xmalloc(sizeof(*cur));
> + struct mru *cur = xmalloc(sizeof(*cur));
>   cur->item = item;
> - cur->prev = mru->tail;
> - cur->next = NULL;
> -
> - if (mru->tail)
> - mru->tail->next = cur;
> - else
> - mru->head = cur;
> - mru->tail = cur;
> + list_add_tail(>list, >list);
>  }
>  
> -void mru_mark(struct mru *mru, struct mru_entry *entry)
> +void mru_mark(struct mru *head, struct mru *entry)
>  {
> - /* If we're already at the front of the list, nothing to do */
> - if (mru->head == entry)
> - return;
> -
> - /* Otherwise, remove us from our current slot... */
> - if (entry->prev)
> - entry->prev->next = entry->next;
> - if (entry->next)
> - entry->next->prev = entry->prev;
> - else
> - mru->tail = entry->prev;
> -
> - /* And insert us at the beginning. */
> - entry->prev = NULL;
> - entry->next = mru->head;
> - if (mru->head)
> - mru->head->prev = entry;
> - mru->head = entry;
> + /* To mark means to put at the front of the list. */
> + list_del(>list);
> + list_add(>list, >list);
>  }
>  
> -void mru_clear(struct mru *mru)
> +void mru_clear(struct mru *head)
>  {
> - struct mru_entry *p = mru->head;
> -
> - while (p) {
> - struct mru_entry *to_free = p;
> - p = p->next;
> + struct list_head *p1;
> + struct list_head *p2;
> + struct mru *to_free;
> + 
> + list_for_each_safe(p1, p2, >list) {
> + to_free = list_entry(p1, struct mru, list);
>   free(to_free);
>   }
> - mru->head = mru->tail = NULL;
> + INIT_LIST_HEAD(>list);
>  }
> diff --git a/mru.h b/mru.h
> index 42e4aeaa1098a..36a332af0bf88 100644
> --- a/mru.h
> +++ b/mru.h
> @@ -1,6 +1,8 @@
>  #ifndef MRU_H
>  #define MRU_H
>  
> +#include "list.h"
> +
>  /**
>   * A simple most-recently-used cache, backed by a doubly-linked list.
>   *
> @@ -8,18 +10,15 @@
>   *
>   *   // Create a list.  Zero-initialization is required.
>   

[PATCH Outreachy] mru: use double-linked list from list.h

2017-09-28 Thread Olga Telezhnaya
Simplify mru.c, mru.h and related code by reusing the double-linked list 
implementation from list.h instead of a custom one.

Signed-off-by: Olga Telezhnaia 
Mentored-by: Christian Couder 
Mentored by: Jeff King 
---
 builtin/pack-objects.c |  5 +++--
 mru.c  | 51 +++---
 mru.h  | 31 +-
 packfile.c |  6 --
 4 files changed, 35 insertions(+), 58 deletions(-)

diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index f721137eaf881..ba812349e0aab 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -995,8 +995,8 @@ static int want_object_in_pack(const unsigned char *sha1,
   struct packed_git **found_pack,
   off_t *found_offset)
 {
-   struct mru_entry *entry;
int want;
+   struct list_head *pos;
 
if (!exclude && local && has_loose_object_nonlocal(sha1))
return 0;
@@ -1012,7 +1012,8 @@ static int want_object_in_pack(const unsigned char *sha1,
return want;
}
 
-   for (entry = packed_git_mru.head; entry; entry = entry->next) {
+   list_for_each(pos, _git_mru.list) {
+   struct mru *entry = list_entry(pos, struct mru, list);
struct packed_git *p = entry->item;
off_t offset;
 
diff --git a/mru.c b/mru.c
index 9dedae0287ed2..8b6ba3d9b7fad 100644
--- a/mru.c
+++ b/mru.c
@@ -1,50 +1,29 @@
 #include "cache.h"
 #include "mru.h"
 
-void mru_append(struct mru *mru, void *item)
+void mru_append(struct mru *head, void *item)
 {
-   struct mru_entry *cur = xmalloc(sizeof(*cur));
+   struct mru *cur = xmalloc(sizeof(*cur));
cur->item = item;
-   cur->prev = mru->tail;
-   cur->next = NULL;
-
-   if (mru->tail)
-   mru->tail->next = cur;
-   else
-   mru->head = cur;
-   mru->tail = cur;
+   list_add_tail(>list, >list);
 }
 
-void mru_mark(struct mru *mru, struct mru_entry *entry)
+void mru_mark(struct mru *head, struct mru *entry)
 {
-   /* If we're already at the front of the list, nothing to do */
-   if (mru->head == entry)
-   return;
-
-   /* Otherwise, remove us from our current slot... */
-   if (entry->prev)
-   entry->prev->next = entry->next;
-   if (entry->next)
-   entry->next->prev = entry->prev;
-   else
-   mru->tail = entry->prev;
-
-   /* And insert us at the beginning. */
-   entry->prev = NULL;
-   entry->next = mru->head;
-   if (mru->head)
-   mru->head->prev = entry;
-   mru->head = entry;
+   /* To mark means to put at the front of the list. */
+   list_del(>list);
+   list_add(>list, >list);
 }
 
-void mru_clear(struct mru *mru)
+void mru_clear(struct mru *head)
 {
-   struct mru_entry *p = mru->head;
-
-   while (p) {
-   struct mru_entry *to_free = p;
-   p = p->next;
+   struct list_head *p1;
+   struct list_head *p2;
+   struct mru *to_free;
+   
+   list_for_each_safe(p1, p2, >list) {
+   to_free = list_entry(p1, struct mru, list);
free(to_free);
}
-   mru->head = mru->tail = NULL;
+   INIT_LIST_HEAD(>list);
 }
diff --git a/mru.h b/mru.h
index 42e4aeaa1098a..36a332af0bf88 100644
--- a/mru.h
+++ b/mru.h
@@ -1,6 +1,8 @@
 #ifndef MRU_H
 #define MRU_H
 
+#include "list.h"
+
 /**
  * A simple most-recently-used cache, backed by a doubly-linked list.
  *
@@ -8,18 +10,15 @@
  *
  *   // Create a list.  Zero-initialization is required.
  *   static struct mru cache;
- *   mru_append(, item);
- *   ...
+ *   INIT_LIST_HEAD();
  *
- *   // Iterate in MRU order.
- *   struct mru_entry *p;
- *   for (p = cache.head; p; p = p->next) {
- * if (matches(p->item))
- * break;
- *   }
+ *   // Add new item to the end of the list.
+ *   void *item;
+ *   ...
+ *   mru_append(, item);
  *
  *   // Mark an item as used, moving it to the front of the list.
- *   mru_mark(, p);
+ *   mru_mark(, item);
  *
  *   // Reset the list to empty, cleaning up all resources.
  *   mru_clear();
@@ -29,17 +28,13 @@
  * you will begin traversing the whole list again.
  */
 
-struct mru_entry {
-   void *item;
-   struct mru_entry *prev, *next;
-};
-
 struct mru {
-   struct mru_entry *head, *tail;
+   struct list_head list;
+void *item;
 };
 
-void mru_append(struct mru *mru, void *item);
-void mru_mark(struct mru *mru, struct mru_entry *entry);
-void mru_clear(struct mru *mru);
+void mru_append(struct mru *head, void *item);
+void mru_mark(struct mru *head, struct mru *entry);
+void mru_clear(struct mru *head);
 
 #endif /* MRU_H */
diff --git a/packfile.c b/packfile.c
index f69a5c8d607af..ae3b0b2e9c09a 100644
--- a/packfile.c
+++