Skip to content

Commit a348cab

Browse files
Copilotkovidgoyal
authored andcommitted
Skip generation increment in background_images() when paths unchanged
1 parent 12a8792 commit a348cab

3 files changed

Lines changed: 30 additions & 13 deletions

File tree

kitty/options/to-c.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,25 @@ free_background_images(Options *opts) {
178178

179179
static inline void
180180
background_images(PyObject *src, Options *opts) {
181-
free_background_images(opts);
182181
static unsigned generation = 0;
182+
size_t new_count = (PyTuple_Check(src) && PyTuple_GET_SIZE(src) > 0) ? (size_t)PyTuple_GET_SIZE(src) : 0;
183+
if (new_count == opts->background_images.count && opts->background_images.paths) {
184+
bool changed = false;
185+
for (size_t i = 0; i < new_count; i++) {
186+
const char *p = PyUnicode_AsUTF8(PyTuple_GET_ITEM(src, i));
187+
if (!p || !opts->background_images.paths[i] || strcmp(p, opts->background_images.paths[i]) != 0) { changed = true; break; }
188+
}
189+
if (!changed) return;
190+
}
191+
free_background_images(opts);
183192
opts->background_images.generation = ++generation;
184-
if (!PyTuple_Check(src) || PyTuple_GET_SIZE(src) == 0) return;
185-
opts->background_images.paths = calloc(PyTuple_GET_SIZE(src), sizeof(opts->background_images.paths[0]));
193+
if (!new_count) return;
194+
opts->background_images.paths = calloc(new_count, sizeof(opts->background_images.paths[0]));
186195
if (opts->background_images.paths) {
187-
opts->background_images.count = PyTuple_GET_SIZE(src);
188-
for (size_t i = 0; i < opts->background_images.count; i++) opts->background_images.paths[i] = strdup(
189-
PyUnicode_AsUTF8(PyTuple_GET_ITEM(src, i)));
196+
opts->background_images.count = new_count;
197+
for (size_t i = 0; i < opts->background_images.count; i++) {
198+
opts->background_images.paths[i] = strdup(PyUnicode_AsUTF8(PyTuple_GET_ITEM(src, i)));
199+
}
190200
}
191201
}
192202

kitty/state.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ ensure_background_images_generation(bool release_texture) {
226226
if (global_state.background_images.generation == OPT(background_images).generation) return;
227227
free_global_background_images(release_texture);
228228
global_state.background_images.generation = OPT(background_images).generation;
229+
global_state.background_images.entries_attempted = 0;
229230
if (OPT(background_images).count) {
230231
global_state.background_images.images = calloc(
231232
OPT(background_images).count, sizeof(global_state.background_images.images[0]));
@@ -238,12 +239,11 @@ static unsigned bg_image_id_counter = 0;
238239
static BackgroundImage*
239240
global_background_image(size_t idx) {
240241
ensure_background_images_generation(true);
241-
while (global_state.background_images.count <= idx && OPT(background_images).count) {
242-
RAII_ALLOC(char, path, OPT(background_images).paths[0]); // transfer ownership
243-
remove_i_from_array(OPT(background_images.paths), 0, OPT(background_images).count);
242+
while (global_state.background_images.count <= idx && global_state.background_images.entries_attempted < OPT(background_images).count) {
243+
size_t j = global_state.background_images.entries_attempted++;
244244
BackgroundImage *img = calloc(1, sizeof(BackgroundImage));
245245
if (!img) fatal("Out of memory");
246-
if (image_path_to_bitmap(path, &img->bitmap, &img->width, &img->height, &img->mmap_size)) {
246+
if (image_path_to_bitmap(OPT(background_images).paths[j], &img->bitmap, &img->width, &img->height, &img->mmap_size)) {
247247
if (send_bgimage_to_gpu(OPT(background_image_layout), img)) {
248248
img->refcnt++;
249249
img->id = ++bg_image_id_counter;
@@ -273,7 +273,7 @@ increment_bg_image_idx(size_t idx, int delta) {
273273
}
274274
if ((unsigned)abs(delta) <= idx) return idx + delta;
275275
// wrap to last image, which means we need to load all
276-
global_background_image(global_state.background_images.count + OPT(background_images).count + 1);
276+
if (OPT(background_images).count > 0) global_background_image(OPT(background_images).count - 1);
277277
return global_state.background_images.count ? global_state.background_images.count - 1 : 0;
278278
}
279279

@@ -1417,6 +1417,12 @@ pyset_background_image(PyObject *self UNUSED, PyObject *args, PyObject *kw) {
14171417
}
14181418
global_state.background_images.images[0] = bgimage;
14191419
bgimage->refcnt++;
1420+
if (OPT(background_images).count > 0) {
1421+
free(OPT(background_images).paths[0]);
1422+
char *new_path = strdup(path);
1423+
if (!new_path) fatal("Out of memory");
1424+
OPT(background_images).paths[0] = new_path;
1425+
}
14201426
} else free_global_background_images(true);
14211427
OPT(background_image_layout) = layout;
14221428
if (pylinear && pylinear != Py_None) convert_from_python_background_image_linear(pylinear, &global_state.opts);

kitty/state.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ typedef struct Options {
9595

9696
char *default_window_logo;
9797
struct {
98-
char **paths; size_t count;
98+
char **paths;
99+
size_t count;
99100
unsigned generation;
100101
} background_images;
101102
BackgroundImageLayout background_image_layout;
@@ -470,7 +471,7 @@ typedef struct GlobalState {
470471
PyObject *boss;
471472
struct {
472473
BackgroundImage **images;
473-
size_t count;
474+
size_t count, entries_attempted;
474475
unsigned generation;
475476
} background_images;
476477
OSWindow *os_windows;

0 commit comments

Comments
 (0)