Skip to content

Commit 6a9117f

Browse files
committed
cache: use iteration interface for cache eviction
To relieve us from memory pressure, we may regularly call `cache_evict_entries` to remove some entries from it. Unfortunately, our cache does not support a least-recently-used mode or something similar, which is why we evict entries completeley at random right now. Thing is, this is only possible due to the map interfaces exposing the entry indices, and we intend to completely remove those to decouple map users from map implementations. As soon as that is done, we are unable to do this random eviction anymore. Convert this to make use of an iterator for now. Obviously, there is no random eviction possible like that anymore, but we'll always start by evicting from the beginning of the map. Due to hashing, one may hope that the selected buckets will be evicted at least in some way unpredictably. But more likely than not, this will not be the case. But let's see what happens and if any users complain about degraded performance. If so, we might come up with a different scheme than random removal, e.g. by using an LRU cache.
1 parent c976b4f commit 6a9117f

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/cache.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ void git_cache_free(git_cache *cache)
115115
/* Called with lock */
116116
static void cache_evict_entries(git_cache *cache)
117117
{
118-
uint32_t seed = rand();
119-
size_t evict_count = 8;
118+
size_t evict_count = 8, i;
120119
ssize_t evicted_memory = 0;
121120

122121
/* do not infinite loop if there's not enough entries to evict */
@@ -125,18 +124,19 @@ static void cache_evict_entries(git_cache *cache)
125124
return;
126125
}
127126

127+
i = 0;
128128
while (evict_count > 0) {
129-
size_t pos = seed++ % git_oidmap_end(cache->map);
129+
git_cached_obj *evict;
130+
const git_oid *key;
130131

131-
if (git_oidmap_has_data(cache->map, pos)) {
132-
git_cached_obj *evict = git_oidmap_value_at(cache->map, pos);
132+
if (git_oidmap_iterate((void **) &evict, cache->map, &i, &key) == GIT_ITEROVER)
133+
break;
133134

134-
evict_count--;
135-
evicted_memory += evict->size;
136-
git_cached_obj_decref(evict);
135+
evict_count--;
136+
evicted_memory += evict->size;
137+
git_cached_obj_decref(evict);
137138

138-
git_oidmap_delete_at(cache->map, pos);
139-
}
139+
git_oidmap_delete(cache->map, key);
140140
}
141141

142142
cache->used_memory -= evicted_memory;

0 commit comments

Comments
 (0)