mirror of
https://github.com/git/git
synced 2024-08-27 19:49:21 +00:00
![Jeff King](/assets/img/avatar_default.png)
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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
46 lines
1 KiB
C
46 lines
1 KiB
C
#ifndef MRU_H
|
|
#define MRU_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;
|
|
* mru_append(&cache, item);
|
|
* ...
|
|
*
|
|
* // Iterate in MRU order.
|
|
* struct mru_entry *p;
|
|
* for (p = cache.head; p; p = p->next) {
|
|
* if (matches(p->item))
|
|
* break;
|
|
* }
|
|
*
|
|
* // Mark an item as used, moving it to the front of the list.
|
|
* mru_mark(&cache, p);
|
|
*
|
|
* // 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_entry {
|
|
void *item;
|
|
struct mru_entry *prev, *next;
|
|
};
|
|
|
|
struct mru {
|
|
struct mru_entry *head, *tail;
|
|
};
|
|
|
|
void mru_append(struct mru *mru, void *item);
|
|
void mru_mark(struct mru *mru, struct mru_entry *entry);
|
|
void mru_clear(struct mru *mru);
|
|
|
|
#endif /* MRU_H */
|