git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v3] mru: Replace mru.[ch] with list.h implementation
@ 2018-01-19 23:36 Gargi Sharma
  2018-01-20  0:59 ` Eric Sunshine
  2018-01-20  1:02 ` Eric Wong
  0 siblings, 2 replies; 7+ messages in thread
From: Gargi Sharma @ 2018-01-19 23:36 UTC (permalink / raw)
  To: git; +Cc: Gargi Sharma

Replace the custom calls to mru.[ch] with calls to list.h. This patch is
the final step in removing the mru API completely and inlining the logic.
This patch leads to significant code reduction and the mru API hence, is
not a useful abstraction anymore.

Signed-off-by: Gargi Sharma <gs051095@gmail.com>
---
Changes in v3:
	- Make the commit message more descriptive.
	- Remove braces after if statement.
Changes in v2:
        - Add a move to front function to the list API.
        - Remove memory leak.
        - Remove redundant remove operations on the list.

The commit has been built on top of ot/mru-on-list branch.

A future improvement could be removing/changing the
type of nect pointer or dropping it entirely. See discussion
here:
https://public-inbox.org/git/CAOCi2DGYQr4jFf5ObY2buyhNJeaAPQKF8tbojn2W0b18Eo+Wgw@mail.gmail.com/
---
 Makefile               |  1 -
 builtin/pack-objects.c |  9 ++++-----
 cache.h                |  9 +++++----
 list.h                 |  7 +++++++
 mru.c                  | 27 ---------------------------
 mru.h                  | 40 ----------------------------------------
 packfile.c             | 18 +++++++++---------
 sha1_file.c            |  1 -
 8 files changed, 25 insertions(+), 87 deletions(-)
 delete mode 100644 mru.c
 delete mode 100644 mru.h

diff --git a/Makefile b/Makefile
index ed4ca43..4a79ec5 100644
--- a/Makefile
+++ b/Makefile
@@ -814,7 +814,6 @@ LIB_OBJS += merge.o
 LIB_OBJS += merge-blobs.o
 LIB_OBJS += merge-recursive.o
 LIB_OBJS += mergesort.o
-LIB_OBJS += mru.o
 LIB_OBJS += name-hash.o
 LIB_OBJS += notes.o
 LIB_OBJS += notes-cache.o
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index ba81234..188ba3e 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -24,7 +24,7 @@
 #include "reachable.h"
 #include "sha1-array.h"
 #include "argv-array.h"
-#include "mru.h"
+#include "list.h"
 #include "packfile.h"
 
 static const char *pack_usage[] = {
@@ -1012,9 +1012,8 @@ static int want_object_in_pack(const unsigned char *sha1,
 			return want;
 	}
 
-	list_for_each(pos, &packed_git_mru.list) {
-		struct mru *entry = list_entry(pos, struct mru, list);
-		struct packed_git *p = entry->item;
+	list_for_each(pos, &packed_git_mru) {
+		struct packed_git *p = list_entry(pos, struct packed_git, mru);
 		off_t offset;
 
 		if (p == *found_pack)
@@ -1031,7 +1030,7 @@ static int want_object_in_pack(const unsigned char *sha1,
 			}
 			want = want_found_object(exclude, p);
 			if (!exclude && want > 0)
-				mru_mark(&packed_git_mru, entry);
+				list_move_to_front(&p->mru, &packed_git_mru);
 			if (want != -1)
 				return want;
 		}
diff --git a/cache.h b/cache.h
index 49b083e..1a275ae 100644
--- a/cache.h
+++ b/cache.h
@@ -4,7 +4,7 @@
 #include "git-compat-util.h"
 #include "strbuf.h"
 #include "hashmap.h"
-#include "mru.h"
+#include "list.h"
 #include "advice.h"
 #include "gettext.h"
 #include "convert.h"
@@ -1566,6 +1566,7 @@ struct pack_window {
 
 extern struct packed_git {
 	struct packed_git *next;
+	struct list_head mru;
 	struct pack_window *windows;
 	off_t pack_size;
 	const void *index_data;
@@ -1587,10 +1588,10 @@ extern struct packed_git {
 } *packed_git;
 
 /*
- * A most-recently-used ordered version of the packed_git list, which can
- * be iterated instead of packed_git (and marked via mru_mark).
+ * A most-recently-used ordered version of the packed_git list.
  */
-extern struct mru packed_git_mru;
+extern struct list_head packed_git_mru;
+
 
 struct pack_entry {
 	off_t offset;
diff --git a/list.h b/list.h
index eb60119..5129b0a 100644
--- a/list.h
+++ b/list.h
@@ -93,6 +93,13 @@ static inline void list_move(struct list_head *elem, struct list_head *head)
 	list_add(elem, head);
 }
 
+/* Move to the front of the list. */
+static inline void list_move_to_front(struct list_head *elem, struct list_head *head)
+{
+	list_del(elem);
+	list_add(elem, head);
+}
+
 /* Replace an old entry. */
 static inline void list_replace(struct list_head *old, struct list_head *newp)
 {
diff --git a/mru.c b/mru.c
deleted file mode 100644
index 8f3f34c..0000000
--- a/mru.c
+++ /dev/null
@@ -1,27 +0,0 @@
-#include "cache.h"
-#include "mru.h"
-
-void mru_append(struct mru *head, void *item)
-{
-	struct mru *cur = xmalloc(sizeof(*cur));
-	cur->item = item;
-	list_add_tail(&cur->list, &head->list);
-}
-
-void mru_mark(struct mru *head, struct mru *entry)
-{
-	/* To mark means to put at the front of the list. */
-	list_del(&entry->list);
-	list_add(&entry->list, &head->list);
-}
-
-void mru_clear(struct mru *head)
-{
-	struct list_head *pos;
-	struct list_head *tmp;
-
-	list_for_each_safe(pos, tmp, &head->list) {
-		free(list_entry(pos, struct mru, list));
-	}
-	INIT_LIST_HEAD(&head->list);
-}
diff --git a/mru.h b/mru.h
deleted file mode 100644
index 80a589e..0000000
--- a/mru.h
+++ /dev/null
@@ -1,40 +0,0 @@
-#ifndef MRU_H
-#define MRU_H
-
-#include "list.h"
-
-/**
- * A simple most-recently-used cache, backed by a doubly-linked list.
- *
- * Usage is roughly:
- *
- *   // Create a list.  Zero-initialization is required.
- *   static struct mru cache;
- *   INIT_LIST_HEAD(&cache.list);
- *
- *   // Add new item to the end of the list.
- *   void *item;
- *   ...
- *   mru_append(&cache, item);
- *
- *   // Mark an item as used, moving it to the front of the list.
- *   mru_mark(&cache, item);
- *
- *   // Reset the list to empty, cleaning up all resources.
- *   mru_clear(&cache);
- *
- * Note that you SHOULD NOT call mru_mark() and then continue traversing the
- * list; it reorders the marked item to the front of the list, and therefore
- * you will begin traversing the whole list again.
- */
-
-struct mru {
-	struct list_head list;
-	void *item;
-};
-
-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 502d915..0a25cf2 100644
--- a/packfile.c
+++ b/packfile.c
@@ -1,5 +1,5 @@
 #include "cache.h"
-#include "mru.h"
+#include "list.h"
 #include "pack.h"
 #include "dir.h"
 #include "mergesort.h"
@@ -40,7 +40,7 @@ static unsigned int pack_max_fds;
 static size_t peak_pack_mapped;
 static size_t pack_mapped;
 struct packed_git *packed_git;
-struct mru packed_git_mru = {{&packed_git_mru.list, &packed_git_mru.list}};
+LIST_HEAD(packed_git_mru);
 
 #define SZ_FMT PRIuMAX
 static inline uintmax_t sz_fmt(size_t s) { return s; }
@@ -859,9 +859,9 @@ static void prepare_packed_git_mru(void)
 {
 	struct packed_git *p;
 
-	mru_clear(&packed_git_mru);
-	for (p = packed_git; p; p = p->next)
-		mru_append(&packed_git_mru, p);
+	for (p = packed_git; p; p = p->next) {
+		list_add_tail(&p->mru, &packed_git_mru);
+	}
 }
 
 static int prepare_packed_git_run_once = 0;
@@ -1830,10 +1830,10 @@ int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
 	if (!packed_git)
 		return 0;
 
-	list_for_each(pos, &packed_git_mru.list) {
-		struct mru *p = list_entry(pos, struct mru, list);
-		if (fill_pack_entry(sha1, e, p->item)) {
-			mru_mark(&packed_git_mru, p);
+	list_for_each(pos, &packed_git_mru) {
+		struct packed_git *p = list_entry(pos, struct packed_git, mru);
+		if (fill_pack_entry(sha1, e, p)) {
+			list_move_to_front(&p->mru, &packed_git_mru);
 			return 1;
 		}
 	}
diff --git a/sha1_file.c b/sha1_file.c
index 5a20148..e664f2d 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -24,7 +24,6 @@
 #include "bulk-checkin.h"
 #include "streaming.h"
 #include "dir.h"
-#include "mru.h"
 #include "list.h"
 #include "mergesort.h"
 #include "quote.h"
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] mru: Replace mru.[ch] with list.h implementation
  2018-01-19 23:36 [PATCH v3] mru: Replace mru.[ch] with list.h implementation Gargi Sharma
@ 2018-01-20  0:59 ` Eric Sunshine
  2018-01-20 22:26   ` Gargi Sharma
  2018-01-20  1:02 ` Eric Wong
  1 sibling, 1 reply; 7+ messages in thread
From: Eric Sunshine @ 2018-01-20  0:59 UTC (permalink / raw)
  To: Gargi Sharma; +Cc: Git List, Jeff King, Christian Couder

On Fri, Jan 19, 2018 at 6:36 PM, Gargi Sharma <gs051095@gmail.com> wrote:
> Replace the custom calls to mru.[ch] with calls to list.h. This patch is
> the final step in removing the mru API completely and inlining the logic.
> This patch leads to significant code reduction and the mru API hence, is
> not a useful abstraction anymore.

A couple minor style nits below... (may not be worth a re-roll)

> Signed-off-by: Gargi Sharma <gs051095@gmail.com>
> ---
> diff --git a/cache.h b/cache.h
> @@ -1587,10 +1588,10 @@ extern struct packed_git {
>  } *packed_git;
>
>  /*
> - * A most-recently-used ordered version of the packed_git list, which can
> - * be iterated instead of packed_git (and marked via mru_mark).
> + * A most-recently-used ordered version of the packed_git list.
>   */
> -extern struct mru packed_git_mru;
> +extern struct list_head packed_git_mru;
> +

Unnecessary extra blank line.

>  struct pack_entry {
>         off_t offset;
> diff --git a/packfile.c b/packfile.c
> @@ -859,9 +859,9 @@ static void prepare_packed_git_mru(void)
>  {
>         struct packed_git *p;
>
> -       mru_clear(&packed_git_mru);
> -       for (p = packed_git; p; p = p->next)
> -               mru_append(&packed_git_mru, p);
> +       for (p = packed_git; p; p = p->next) {
> +               list_add_tail(&p->mru, &packed_git_mru);
> +       }

Unnecessary braces on for-loop.

>  }

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] mru: Replace mru.[ch] with list.h implementation
  2018-01-19 23:36 [PATCH v3] mru: Replace mru.[ch] with list.h implementation Gargi Sharma
  2018-01-20  0:59 ` Eric Sunshine
@ 2018-01-20  1:02 ` Eric Wong
  2018-01-20 22:24   ` Gargi Sharma
  1 sibling, 1 reply; 7+ messages in thread
From: Eric Wong @ 2018-01-20  1:02 UTC (permalink / raw)
  To: Gargi Sharma; +Cc: git

Gargi Sharma <gs051095@gmail.com> wrote:
> --- a/list.h
> +++ b/list.h
> @@ -93,6 +93,13 @@ static inline void list_move(struct list_head *elem, struct list_head *head)
>  	list_add(elem, head);
>  }
>  
> +/* Move to the front of the list. */
> +static inline void list_move_to_front(struct list_head *elem, struct list_head *head)
> +{
> +	list_del(elem);
> +	list_add(elem, head);
> +}
> +

Since we already have list_move and it does the same thing,
I don't think we need a new function, here.

Hackers coming from other projects (glibc/urcu/Linux kernel)
might appreciate having fewer differences from what they're used
to.

Anyways thanks for working on this!

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] mru: Replace mru.[ch] with list.h implementation
  2018-01-20  1:02 ` Eric Wong
@ 2018-01-20 22:24   ` Gargi Sharma
  2018-01-21  4:38     ` René Scharfe
  0 siblings, 1 reply; 7+ messages in thread
From: Gargi Sharma @ 2018-01-20 22:24 UTC (permalink / raw)
  To: Eric Wong, Jeff King, Christian Couder; +Cc: git

On Sat, Jan 20, 2018 at 1:02 AM, Eric Wong <e@80x24.org> wrote:
> Gargi Sharma <gs051095@gmail.com> wrote:
>> --- a/list.h
>> +++ b/list.h
>> @@ -93,6 +93,13 @@ static inline void list_move(struct list_head *elem, struct list_head *head)
>>       list_add(elem, head);
>>  }
>>
>> +/* Move to the front of the list. */
>> +static inline void list_move_to_front(struct list_head *elem, struct list_head *head)
>> +{
>> +     list_del(elem);
>> +     list_add(elem, head);
>> +}
>> +
>
> Since we already have list_move and it does the same thing,
> I don't think we need a new function, here.
>
> Hackers coming from other projects (glibc/urcu/Linux kernel)
> might appreciate having fewer differences from what they're used
> to.

I think the idea behind this function was that it is already being used in two
places in the code and might be used in other places in the future. I agree
with your stance, but a list_move_to_front is pretty self explanatory and not
too different, so it should be alright.

>
> Anyways thanks for working on this!

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] mru: Replace mru.[ch] with list.h implementation
  2018-01-20  0:59 ` Eric Sunshine
@ 2018-01-20 22:26   ` Gargi Sharma
  0 siblings, 0 replies; 7+ messages in thread
From: Gargi Sharma @ 2018-01-20 22:26 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git List, Jeff King, Christian Couder

On Sat, Jan 20, 2018 at 12:59 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Fri, Jan 19, 2018 at 6:36 PM, Gargi Sharma <gs051095@gmail.com> wrote:
>> Replace the custom calls to mru.[ch] with calls to list.h. This patch is
>> the final step in removing the mru API completely and inlining the logic.
>> This patch leads to significant code reduction and the mru API hence, is
>> not a useful abstraction anymore.
>
> A couple minor style nits below... (may not be worth a re-roll)

I can send a v4, it shouldn't be a problem. :)

>
>> Signed-off-by: Gargi Sharma <gs051095@gmail.com>
>> ---
>> diff --git a/cache.h b/cache.h
>> @@ -1587,10 +1588,10 @@ extern struct packed_git {
>>  } *packed_git;
>>
>>  /*
>> - * A most-recently-used ordered version of the packed_git list, which can
>> - * be iterated instead of packed_git (and marked via mru_mark).
>> + * A most-recently-used ordered version of the packed_git list.
>>   */
>> -extern struct mru packed_git_mru;
>> +extern struct list_head packed_git_mru;
>> +
>
> Unnecessary extra blank line.
>
>>  struct pack_entry {
>>         off_t offset;
>> diff --git a/packfile.c b/packfile.c
>> @@ -859,9 +859,9 @@ static void prepare_packed_git_mru(void)
>>  {
>>         struct packed_git *p;
>>
>> -       mru_clear(&packed_git_mru);
>> -       for (p = packed_git; p; p = p->next)
>> -               mru_append(&packed_git_mru, p);
>> +       for (p = packed_git; p; p = p->next) {
>> +               list_add_tail(&p->mru, &packed_git_mru);
>> +       }
>
> Unnecessary braces on for-loop.
>
>>  }

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] mru: Replace mru.[ch] with list.h implementation
  2018-01-20 22:24   ` Gargi Sharma
@ 2018-01-21  4:38     ` René Scharfe
  2018-01-21 22:55       ` Gargi Sharma
  0 siblings, 1 reply; 7+ messages in thread
From: René Scharfe @ 2018-01-21  4:38 UTC (permalink / raw)
  To: Gargi Sharma, Eric Wong, Jeff King, Christian Couder; +Cc: git

Am 20.01.2018 um 23:24 schrieb Gargi Sharma:
> On Sat, Jan 20, 2018 at 1:02 AM, Eric Wong <e@80x24.org> wrote:
>> Gargi Sharma <gs051095@gmail.com> wrote:
>>> --- a/list.h
>>> +++ b/list.h
>>> @@ -93,6 +93,13 @@ static inline void list_move(struct list_head *elem, struct list_head *head)
>>>        list_add(elem, head);
>>>   }
>>>
>>> +/* Move to the front of the list. */
>>> +static inline void list_move_to_front(struct list_head *elem, struct list_head *head)
>>> +{
>>> +     list_del(elem);
>>> +     list_add(elem, head);
>>> +}
>>> +
>>
>> Since we already have list_move and it does the same thing,
>> I don't think we need a new function, here.
>>
>> Hackers coming from other projects (glibc/urcu/Linux kernel)
>> might appreciate having fewer differences from what they're used
>> to.
> 
> I think the idea behind this function was that it is already being used in two
> places in the code and might be used in other places in the future. I agree
> with your stance, but a list_move_to_front is pretty self explanatory and not
> too different, so it should be alright.

Not sure I understand the point about the function being already used as
an argument for adding it, but if there is already one which has the
exact sane behavior (list_move() in this case) then that should be used
instead of adding a duplicate.

René


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] mru: Replace mru.[ch] with list.h implementation
  2018-01-21  4:38     ` René Scharfe
@ 2018-01-21 22:55       ` Gargi Sharma
  0 siblings, 0 replies; 7+ messages in thread
From: Gargi Sharma @ 2018-01-21 22:55 UTC (permalink / raw)
  To: René Scharfe; +Cc: Eric Wong, Jeff King, Christian Couder, Git List

On Sun, Jan 21, 2018 at 4:38 AM, René Scharfe <l.s.r@web.de> wrote:
>
> Am 20.01.2018 um 23:24 schrieb Gargi Sharma:
> > On Sat, Jan 20, 2018 at 1:02 AM, Eric Wong <e@80x24.org> wrote:
> >> Gargi Sharma <gs051095@gmail.com> wrote:
> >>> --- a/list.h
> >>> +++ b/list.h
> >>> @@ -93,6 +93,13 @@ static inline void list_move(struct list_head *elem, struct list_head *head)
> >>>        list_add(elem, head);
> >>>   }
> >>>
> >>> +/* Move to the front of the list. */
> >>> +static inline void list_move_to_front(struct list_head *elem, struct list_head *head)
> >>> +{
> >>> +     list_del(elem);
> >>> +     list_add(elem, head);
> >>> +}
> >>> +
> >>
> >> Since we already have list_move and it does the same thing,
> >> I don't think we need a new function, here.
> >>
> >> Hackers coming from other projects (glibc/urcu/Linux kernel)
> >> might appreciate having fewer differences from what they're used
> >> to.
> >
> > I think the idea behind this function was that it is already being used in two
> > places in the code and might be used in other places in the future. I agree
> > with your stance, but a list_move_to_front is pretty self explanatory and not
> > too different, so it should be alright.
>
> Not sure I understand the point about the function being already used as
> an argument for adding it, but if there is already one which has the
> exact sane behavior (list_move() in this case) then that should be used
> instead of adding a duplicate.

Thanks for bringing this to my attention, René. I can use list_move()
to do the exact
same thing as list_move_to_front(). Will send another version.

Thanks,
Gargi
>
> René
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-01-21 22:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-19 23:36 [PATCH v3] mru: Replace mru.[ch] with list.h implementation Gargi Sharma
2018-01-20  0:59 ` Eric Sunshine
2018-01-20 22:26   ` Gargi Sharma
2018-01-20  1:02 ` Eric Wong
2018-01-20 22:24   ` Gargi Sharma
2018-01-21  4:38     ` René Scharfe
2018-01-21 22:55       ` Gargi Sharma

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