Commit a9bf0c2c43fb5545d5929f63896d06d00281cdd7

Stefan Sperling 2018-06-22T10:35:09

no need to manually count elements of a set

diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index 4eb219f..fbcf7b5 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -35,7 +35,6 @@ struct got_object_cache_entry {
 struct got_object_cache {
 	enum got_object_chache_type type;
 	struct got_object_idset *set;
-	int ncached;
 	int cache_hit;
 	int cache_miss;
 };
diff --git a/lib/repository.c b/lib/repository.c
index 5900746..a688bda 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -154,8 +154,10 @@ cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
 {
 	const struct got_error *err = NULL;
 	struct got_object_cache_entry *ce;
+	int nelem;
 
-	if (cache->ncached >= GOT_OBJECT_CACHE_SIZE) {
+	nelem = got_object_idset_num_elements(cache->set);
+	if (nelem >= GOT_OBJECT_CACHE_SIZE) {
 		err = got_object_idset_remove_random((void **)&ce,
 		    cache->set);
 		if (err)
@@ -169,7 +171,6 @@ cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
 			break;
 		}
 		free(ce);
-		cache->ncached--;
 	}
 
 	ce = calloc(1, sizeof(*ce));
@@ -190,8 +191,7 @@ cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
 			free(ce);
 			err = NULL;
 		}
-	} else
-		cache->ncached++;
+	}
 
 	return err;
 }