{"thread":{"id":"47660","subject":"[PATCH v5] mru: Replace mru.[ch] with list.h implementation","startedAt":"2018-01-21T23:14:25Z","lastAt":"2018-01-23T00:15:06Z","messageCount":3,"participants":["Gargi Sharma","Eric Wong","Jeff King"],"isPatch":true,"patchVersion":5,"patchTotal":null},"messages":[{"id":"337038","messageId":"1516497964-6041-1-git-send-email-gs051095@gmail.com","threadId":"47660","inReplyTo":null,"subject":"[PATCH v5] mru: Replace mru.[ch] with list.h implementation","fromName":"Gargi Sharma","fromEmail":"gs051095@gmail.com","sentAt":"2018-01-21T01:26:04Z","receivedAt":"2018-01-21T23:14:25Z","isPatch":true,"sender":{"key":"gs051095@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8718814?v=4"},"body":"Replace the custom calls to mru.[ch] with calls to list.h. This patch is\nthe final step in removing the mru API completely and inlining the logic.\nThis patch leads to significant code reduction and the mru API hence, is\nnot a useful abstraction anymore.\n\nSigned-off-by: Gargi Sharma <gs051095@gmail.com>\n---\nThe commit has been built on top of ot/mru-on-list branch.\n\nChanges in v5:\n\t- Remove list_move_to_front() as it is redundant.\nChanges in v4:\n        - Fixing minor style issues.\nChanges in v3:\n        - Make the commit message more descriptive.\n        - Remove braces after if statement.\nChanges in v2:\n        - Add a move to front function to the list API.\n        - Remove memory leak.\n        - Remove redundant remove operations on the list.\n\nA future improvement could be removing/changing the\ntype of nect pointer or dropping it entirely. See discussion\nhere:\nhttps://public-inbox.org/git/CAOCi2DGYQr4jFf5ObY2buyhNJeaAPQKF8tbojn2W0b18Eo+Wgw@mail.gmail.com/\n---\n Makefile               |  1 -\n builtin/pack-objects.c |  9 ++++-----\n cache.h                |  8 ++++----\n mru.c                  | 27 ---------------------------\n mru.h                  | 40 ----------------------------------------\n packfile.c             | 15 +++++++--------\n sha1_file.c            |  1 -\n 7 files changed, 15 insertions(+), 86 deletions(-)\n delete mode 100644 mru.c\n delete mode 100644 mru.h\n\ndiff --git a/Makefile b/Makefile\nindex ed4ca43..4a79ec5 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -814,7 +814,6 @@ LIB_OBJS += merge.o\n LIB_OBJS += merge-blobs.o\n LIB_OBJS += merge-recursive.o\n LIB_OBJS += mergesort.o\n-LIB_OBJS += mru.o\n LIB_OBJS += name-hash.o\n LIB_OBJS += notes.o\n LIB_OBJS += notes-cache.o\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex ba81234..6a0b8e8 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -24,7 +24,7 @@\n #include \"reachable.h\"\n #include \"sha1-array.h\"\n #include \"argv-array.h\"\n-#include \"mru.h\"\n+#include \"list.h\"\n #include \"packfile.h\"\n \n static const char *pack_usage[] = {\n@@ -1012,9 +1012,8 @@ static int want_object_in_pack(const unsigned char *sha1,\n \t\t\treturn want;\n \t}\n \n-\tlist_for_each(pos, &packed_git_mru.list) {\n-\t\tstruct mru *entry = list_entry(pos, struct mru, list);\n-\t\tstruct packed_git *p = entry->item;\n+\tlist_for_each(pos, &packed_git_mru) {\n+\t\tstruct packed_git *p = list_entry(pos, struct packed_git, mru);\n \t\toff_t offset;\n \n \t\tif (p == *found_pack)\n@@ -1031,7 +1030,7 @@ static int want_object_in_pack(const unsigned char *sha1,\n \t\t\t}\n \t\t\twant = want_found_object(exclude, p);\n \t\t\tif (!exclude && want > 0)\n-\t\t\t\tmru_mark(&packed_git_mru, entry);\n+\t\t\t\tlist_move(&p->mru, &packed_git_mru);\n \t\t\tif (want != -1)\n \t\t\t\treturn want;\n \t\t}\ndiff --git a/cache.h b/cache.h\nindex 49b083e..cc09e3b 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -4,7 +4,7 @@\n #include \"git-compat-util.h\"\n #include \"strbuf.h\"\n #include \"hashmap.h\"\n-#include \"mru.h\"\n+#include \"list.h\"\n #include \"advice.h\"\n #include \"gettext.h\"\n #include \"convert.h\"\n@@ -1566,6 +1566,7 @@ struct pack_window {\n \n extern struct packed_git {\n \tstruct packed_git *next;\n+\tstruct list_head mru;\n \tstruct pack_window *windows;\n \toff_t pack_size;\n \tconst void *index_data;\n@@ -1587,10 +1588,9 @@ extern struct packed_git {\n } *packed_git;\n \n /*\n- * A most-recently-used ordered version of the packed_git list, which can\n- * be iterated instead of packed_git (and marked via mru_mark).\n+ * A most-recently-used ordered version of the packed_git list.\n  */\n-extern struct mru packed_git_mru;\n+extern struct list_head packed_git_mru;\n \n struct pack_entry {\n \toff_t offset;\ndiff --git a/mru.c b/mru.c\ndeleted file mode 100644\nindex 8f3f34c..0000000\n--- a/mru.c\n+++ /dev/null\n@@ -1,27 +0,0 @@\n-#include \"cache.h\"\n-#include \"mru.h\"\n-\n-void mru_append(struct mru *head, void *item)\n-{\n-\tstruct mru *cur = xmalloc(sizeof(*cur));\n-\tcur->item = item;\n-\tlist_add_tail(&cur->list, &head->list);\n-}\n-\n-void mru_mark(struct mru *head, struct mru *entry)\n-{\n-\t/* To mark means to put at the front of the list. */\n-\tlist_del(&entry->list);\n-\tlist_add(&entry->list, &head->list);\n-}\n-\n-void mru_clear(struct mru *head)\n-{\n-\tstruct list_head *pos;\n-\tstruct list_head *tmp;\n-\n-\tlist_for_each_safe(pos, tmp, &head->list) {\n-\t\tfree(list_entry(pos, struct mru, list));\n-\t}\n-\tINIT_LIST_HEAD(&head->list);\n-}\ndiff --git a/mru.h b/mru.h\ndeleted file mode 100644\nindex 80a589e..0000000\n--- a/mru.h\n+++ /dev/null\n@@ -1,40 +0,0 @@\n-#ifndef MRU_H\n-#define MRU_H\n-\n-#include \"list.h\"\n-\n-/**\n- * A simple most-recently-used cache, backed by a doubly-linked list.\n- *\n- * Usage is roughly:\n- *\n- *   // Create a list.  Zero-initialization is required.\n- *   static struct mru cache;\n- *   INIT_LIST_HEAD(&cache.list);\n- *\n- *   // Add new item to the end of the list.\n- *   void *item;\n- *   ...\n- *   mru_append(&cache, item);\n- *\n- *   // Mark an item as used, moving it to the front of the list.\n- *   mru_mark(&cache, item);\n- *\n- *   // Reset the list to empty, cleaning up all resources.\n- *   mru_clear(&cache);\n- *\n- * Note that you SHOULD NOT call mru_mark() and then continue traversing the\n- * list; it reorders the marked item to the front of the list, and therefore\n- * you will begin traversing the whole list again.\n- */\n-\n-struct mru {\n-\tstruct list_head list;\n-\tvoid *item;\n-};\n-\n-void mru_append(struct mru *head, void *item);\n-void mru_mark(struct mru *head, struct mru *entry);\n-void mru_clear(struct mru *head);\n-\n-#endif /* MRU_H */\ndiff --git a/packfile.c b/packfile.c\nindex 502d915..2388efc 100644\n--- a/packfile.c\n+++ b/packfile.c\n@@ -1,5 +1,5 @@\n #include \"cache.h\"\n-#include \"mru.h\"\n+#include \"list.h\"\n #include \"pack.h\"\n #include \"dir.h\"\n #include \"mergesort.h\"\n@@ -40,7 +40,7 @@ static unsigned int pack_max_fds;\n static size_t peak_pack_mapped;\n static size_t pack_mapped;\n struct packed_git *packed_git;\n-struct mru packed_git_mru = {{&packed_git_mru.list, &packed_git_mru.list}};\n+LIST_HEAD(packed_git_mru);\n \n #define SZ_FMT PRIuMAX\n static inline uintmax_t sz_fmt(size_t s) { return s; }\n@@ -859,9 +859,8 @@ static void prepare_packed_git_mru(void)\n {\n \tstruct packed_git *p;\n \n-\tmru_clear(&packed_git_mru);\n \tfor (p = packed_git; p; p = p->next)\n-\t\tmru_append(&packed_git_mru, p);\n+\t\tlist_add_tail(&p->mru, &packed_git_mru);\n }\n \n static int prepare_packed_git_run_once = 0;\n@@ -1830,10 +1829,10 @@ int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)\n \tif (!packed_git)\n \t\treturn 0;\n \n-\tlist_for_each(pos, &packed_git_mru.list) {\n-\t\tstruct mru *p = list_entry(pos, struct mru, list);\n-\t\tif (fill_pack_entry(sha1, e, p->item)) {\n-\t\t\tmru_mark(&packed_git_mru, p);\n+\tlist_for_each(pos, &packed_git_mru) {\n+\t\tstruct packed_git *p = list_entry(pos, struct packed_git, mru);\n+\t\tif (fill_pack_entry(sha1, e, p)) {\n+\t\t\tlist_move(&p->mru, &packed_git_mru);\n \t\t\treturn 1;\n \t\t}\n \t}\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 5a20148..e664f2d 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -24,7 +24,6 @@\n #include \"bulk-checkin.h\"\n #include \"streaming.h\"\n #include \"dir.h\"\n-#include \"mru.h\"\n #include \"list.h\"\n #include \"mergesort.h\"\n #include \"quote.h\"\n-- \n2.7.4\n\n"},{"id":"337051","messageId":"20180122073701.h7fh7xrkmnzndzj7@untitled","threadId":"47660","inReplyTo":"1516497964-6041-1-git-send-email-gs051095@gmail.com","subject":"Re: [PATCH v5] mru: Replace mru.[ch] with list.h implementation","fromName":"Eric Wong","fromEmail":"e@80x24.org","sentAt":"2018-01-22T07:37:01Z","receivedAt":"2018-01-22T07:37:06Z","isPatch":true,"sender":{"key":"e@80x24.org","avatar":null},"body":"Gargi Sharma <gs051095@gmail.com> wrote:\n>  7 files changed, 15 insertions(+), 86 deletions(-)\n\nThanks! I like the code reduction and increased use of list.h\n\nWere you able to finish running the test suite?  I wasn't :<\n\n> -void mru_clear(struct mru *head)\n> -{\n> -\tstruct list_head *pos;\n> -\tstruct list_head *tmp;\n> -\n> -\tlist_for_each_safe(pos, tmp, &head->list) {\n> -\t\tfree(list_entry(pos, struct mru, list));\n> -\t}\n> -\tINIT_LIST_HEAD(&head->list);\n> -}\n\nOK, the free() calls are no longer necessary...\n\n<snip>\n\n> --- a/packfile.c\n> +++ b/packfile.c\n> @@ -859,9 +859,8 @@ static void prepare_packed_git_mru(void)\n>  {\n>  \tstruct packed_git *p;\n>  \n> -\tmru_clear(&packed_git_mru);\n\nBut the removed mru_clear needs to be replaced with:\n\n+\tINIT_LIST_HEAD(&packed_git_mru);\n\nOtherwise, t3050 never finishes for me.\n\n>  \tfor (p = packed_git; p; p = p->next)\n> -\t\tmru_append(&packed_git_mru, p);\n> +\t\tlist_add_tail(&p->mru, &packed_git_mru);\n>  }\n\nEverything else looks good to me, I'm still waiting for the test\nsuite to run but I don't expect further problems.\n"},{"id":"337133","messageId":"20180123001500.GD26357@sigill.intra.peff.net","threadId":"47660","inReplyTo":"20180122073701.h7fh7xrkmnzndzj7@untitled","subject":"Re: [PATCH v5] mru: Replace mru.[ch] with list.h implementation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2018-01-23T00:15:00Z","receivedAt":"2018-01-23T00:15:06Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Jan 22, 2018 at 07:37:01AM +0000, Eric Wong wrote:\n\n> > --- a/packfile.c\n> > +++ b/packfile.c\n> > @@ -859,9 +859,8 @@ static void prepare_packed_git_mru(void)\n> >  {\n> >  \tstruct packed_git *p;\n> >  \n> > -\tmru_clear(&packed_git_mru);\n> \n> But the removed mru_clear needs to be replaced with:\n> \n> +\tINIT_LIST_HEAD(&packed_git_mru);\n> \n> Otherwise, t3050 never finishes for me.\n\nGood catch. One alternative is to just add any new packs to the end (or\nbeginning) of the mru list, instead of \"resetting\" it here. We can do\nthat because we know that prepare_packed_git() never drops list entries,\nbut only adds new ones.\n\nI'm OK with it either way, though.\n\n-Peff\n"}]}