From c056df223ee830067daa297aa4d6224eb67b310f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 16 Jul 2024 07:43:50 +0530 Subject: [PATCH] Nicer way to prevent defrag --- kitty/disk-cache.c | 8 ++++++-- kitty_tests/graphics.py | 7 ++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/kitty/disk-cache.c b/kitty/disk-cache.c index 456aca1cf..5cb213b98 100644 --- a/kitty/disk-cache.c +++ b/kitty/disk-cache.c @@ -77,6 +77,7 @@ typedef struct { char *cache_dir; int cache_file_fd; Py_ssize_t small_hole_threshold; + unsigned int defrag_factor; pthread_mutex_t lock; pthread_t write_thread; bool thread_started, lock_inited, loop_data_inited, shutting_down, fully_initialized; @@ -96,6 +97,7 @@ new_diskcache_object(PyTypeObject *type, PyObject UNUSED *args, PyObject UNUSED if (self) { self->cache_file_fd = -1; self->small_hole_threshold = 512; + self->defrag_factor = 2; } return (PyObject*) self; } @@ -304,7 +306,7 @@ find_hole_to_use(DiskCache *self, const off_t required_sz) { static inline bool needs_defrag(DiskCache *self) { off_t size_on_disk = size_of_cache_file(self); - return self->total_size && size_on_disk > 0 && (size_t)size_on_disk > self->total_size * 2; + return self->total_size && size_on_disk > 0 && (size_t)size_on_disk > self->total_size * self->defrag_factor; } static void @@ -453,7 +455,8 @@ write_loop(void *data) { continue; } else if (!count) { mutex(lock); - if (self->cache_file_fd > -1) { + count = vt_size(&self->map); + if (!count && self->cache_file_fd > -1) { if (ftruncate(self->cache_file_fd, 0) == 0) lseek(self->cache_file_fd, 0, SEEK_END); } mutex(unlock); @@ -916,6 +919,7 @@ static PyMethodDef methods[] = { static PyMemberDef members[] = { {"total_size", T_ULONGLONG, offsetof(DiskCache, total_size), READONLY, "total_size"}, {"small_hole_threshold", T_PYSSIZET, offsetof(DiskCache, small_hole_threshold), 0, "small_hole_threshold"}, + {"defrag_factor", T_UINT, offsetof(DiskCache, defrag_factor), 0, "defrag_factor"}, {NULL}, }; diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index 77ec1fe56..b2e797794 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -233,11 +233,12 @@ class TestGraphics(BaseTest): for key, val in data.items(): self.ae(dc.get(key_as_bytes(key)), val) - def reset(small_hole_threshold=0): + def reset(small_hole_threshold=0, defrag_factor=2): nonlocal dc, data, s s = self.create_screen() dc = s.grman.disk_cache dc.small_hole_threshold = small_hole_threshold + dc.defrag_factor = defrag_factor data = {} holes_to_create = 2, 4, 6, 8 @@ -342,8 +343,8 @@ class TestGraphics(BaseTest): self.ae(sz, dc.size_on_disk()) # test hole coalescing - reset() - for i in range(1, 8): + reset(defrag_factor=20) + for i in range(1, 6): self.assertIsNone(add(i, str(i)*i)) dc.wait_for_write() remove(2)