Random eviction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
diff --git a/src/cache.c b/src/cache.c
index a0925a1..f49515e 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -32,7 +32,6 @@ size_t git_cache__max_object_size = 4096;
int git_cache_init(git_cache *cache)
{
- cache->lru_count = 0;
cache->map = git_oidmap_alloc();
git_mutex_init(&cache->lock);
return 0;
@@ -44,6 +43,24 @@ void git_cache_free(git_cache *cache)
git_mutex_free(&cache->lock);
}
+static void cache_evict_entries(git_cache *cache, size_t evict)
+{
+ uint32_t seed = rand();
+
+ /* do not infinite loop if there's not enough entries to evict */
+ if (evict > kh_size(cache->map))
+ return;
+
+ while (evict > 0) {
+ khiter_t pos = seed++ % kh_end(cache->map);
+
+ if (kh_exist(cache->map, pos)) {
+ kh_del(oid, cache->map, pos);
+ evict--;
+ }
+ }
+}
+
static bool cache_should_store(git_otype object_type, size_t object_size)
{
if (!git_cache__store_types[object_type])
diff --git a/src/cache.h b/src/cache.h
index 3461ff7..f952830 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -23,14 +23,13 @@ enum {
typedef struct {
git_oid oid;
git_atomic refcount;
- uint16_t flags;
- uint16_t lru;
+ uint32_t cache_size;
+ uint32_t flags;
} git_cached_obj;
typedef struct {
git_oidmap *map;
git_mutex lock;
- unsigned int lru_count;
} git_cache;
int git_cache_init(git_cache *cache);
diff --git a/src/object.c b/src/object.c
index 5542ebc..54ceea3 100644
--- a/src/object.c
+++ b/src/object.c
@@ -98,6 +98,7 @@ int git_object__from_odb_object(
/* Initialize parent object */
git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid);
+ object->cached.cache_size = (uint32_t)odb_obj->raw.len;
object->repo = repo;
switch (type) {