Skip to content

Commit 002f206

Browse files
peffgitster
authored andcommitted
add generic most-recently-used list
There are a few places in Git that would benefit from a fast most-recently-used cache (e.g., the list of packs, which we search linearly but would like to order based on locality). This patch introduces a generic list that can be used to store arbitrary pointers in most-recently-used order. The implementation is just a doubly-linked list, where "marking" an item as used moves it to the front of the list. Insertion and marking are O(1), and iteration is O(n). There's no lookup support provided; if you need fast lookups, you are better off with a different data structure in the first place. There is also no deletion support. This would not be hard to do, but it's not necessary for handling pack structs, which are created and never removed. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3157c88 commit 002f206

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ LIB_OBJS += merge.o
751751
LIB_OBJS += merge-blobs.o
752752
LIB_OBJS += merge-recursive.o
753753
LIB_OBJS += mergesort.o
754+
LIB_OBJS += mru.o
754755
LIB_OBJS += name-hash.o
755756
LIB_OBJS += notes.o
756757
LIB_OBJS += notes-cache.o

mru.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "cache.h"
2+
#include "mru.h"
3+
4+
void mru_append(struct mru *mru, void *item)
5+
{
6+
struct mru_entry *cur = xmalloc(sizeof(*cur));
7+
cur->item = item;
8+
cur->prev = mru->tail;
9+
cur->next = NULL;
10+
11+
if (mru->tail)
12+
mru->tail->next = cur;
13+
else
14+
mru->head = cur;
15+
mru->tail = cur;
16+
}
17+
18+
void mru_mark(struct mru *mru, struct mru_entry *entry)
19+
{
20+
/* If we're already at the front of the list, nothing to do */
21+
if (mru->head == entry)
22+
return;
23+
24+
/* Otherwise, remove us from our current slot... */
25+
if (entry->prev)
26+
entry->prev->next = entry->next;
27+
if (entry->next)
28+
entry->next->prev = entry->prev;
29+
else
30+
mru->tail = entry->prev;
31+
32+
/* And insert us at the beginning. */
33+
entry->prev = NULL;
34+
entry->next = mru->head;
35+
if (mru->head)
36+
mru->head->prev = entry;
37+
mru->head = entry;
38+
}
39+
40+
void mru_clear(struct mru *mru)
41+
{
42+
struct mru_entry *p = mru->head;
43+
44+
while (p) {
45+
struct mru_entry *to_free = p;
46+
p = p->next;
47+
free(to_free);
48+
}
49+
mru->head = mru->tail = NULL;
50+
}

mru.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef MRU_H
2+
#define MRU_H
3+
4+
/**
5+
* A simple most-recently-used cache, backed by a doubly-linked list.
6+
*
7+
* Usage is roughly:
8+
*
9+
* // Create a list. Zero-initialization is required.
10+
* static struct mru cache;
11+
* mru_append(&cache, item);
12+
* ...
13+
*
14+
* // Iterate in MRU order.
15+
* struct mru_entry *p;
16+
* for (p = cache.head; p; p = p->next) {
17+
* if (matches(p->item))
18+
* break;
19+
* }
20+
*
21+
* // Mark an item as used, moving it to the front of the list.
22+
* mru_mark(&cache, p);
23+
*
24+
* // Reset the list to empty, cleaning up all resources.
25+
* mru_clear(&cache);
26+
*
27+
* Note that you SHOULD NOT call mru_mark() and then continue traversing the
28+
* list; it reorders the marked item to the front of the list, and therefore
29+
* you will begin traversing the whole list again.
30+
*/
31+
32+
struct mru_entry {
33+
void *item;
34+
struct mru_entry *prev, *next;
35+
};
36+
37+
struct mru {
38+
struct mru_entry *head, *tail;
39+
};
40+
41+
void mru_append(struct mru *mru, void *item);
42+
void mru_mark(struct mru *mru, struct mru_entry *entry);
43+
void mru_clear(struct mru *mru);
44+
45+
#endif /* MRU_H */

0 commit comments

Comments
 (0)