From 2ef024bbabf59c85c204c87b18a9ba883af26da0 Mon Sep 17 00:00:00 2001 From: Hassan Raza Date: Sat, 26 Sep 2026 22:07:02 +0500 Subject: [PATCH] Collect stale hyperlinks before adding new links --- docs/changelog.rst | 2 ++ kitty/hyperlink.c | 5 ++--- kitty_tests/screen.py | 8 ++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index f8a4fe576..44808dc7a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -202,6 +202,8 @@ Detailed list of changes 0.49.2 [future] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- Preserve newly created hyperlinks during automatic hyperlink collection + - Reduce input latency by processing small amounts of program output, such as the echo of typed characters, immediately instead of waiting for :opt:`input_delay` (:pull:`10560`) diff --git a/kitty/hyperlink.c b/kitty/hyperlink.c index 0f5c7f04b..90442c47e 100644 --- a/kitty/hyperlink.c +++ b/kitty/hyperlink.c @@ -143,6 +143,8 @@ get_id_for_hyperlink(Screen *screen, const char *id, const char *url) { key[keylen] = 0; hyperlink_map_itr itr = vt_get(&pool->map, key); if (!vt_is_end(itr)) return itr.data->val; + // Collect before inserting the new hyperlink, which has no cell references yet. + if (++pool->adds_since_last_gc > 8192) screen_garbage_collect_hyperlink_pool(screen); if (pool->array.count >= HYPERLINK_MAX_NUMBER - 1) { screen_garbage_collect_hyperlink_pool(screen); if (pool->array.count >= HYPERLINK_MAX_NUMBER - 128) { @@ -159,9 +161,6 @@ get_id_for_hyperlink(Screen *screen, const char *id, const char *url) { hyperlink_id_type new_id = pool->array.count++; pool->array.items[new_id] = dupstr(key, keylen); if (vt_is_end(vt_insert(&pool->map, pool->array.items[new_id], new_id))) fatal("Out of memory"); - // If there have been a lot of hyperlink adds do a garbage collect so as - // not to leak too much memory over unused hyperlinks - if (++pool->adds_since_last_gc > 8192) screen_garbage_collect_hyperlink_pool(screen); return new_id; } diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index 486c9e2e9..e62183667 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -1470,6 +1470,14 @@ class TestScreen(BaseTest): self.ae('2', s.hyperlink_at(1, 3)) self.ae(s.current_url_text(), 'Z Z') + def test_automatic_hyperlink_collection(self) -> None: + s = self.create_screen() + for i in range(1, 2 * 8192 + 3): + url = f'https://example.test/{i}' + parse_bytes(s, f'\r\x1b]8;;{url}\x1b\\L\x1b]8;;\x1b\\'.encode()) + self.ae(s.hyperlink_at(0, 0), url) + self.assertLess(len(s.hyperlinks_as_set()), 8192) + def test_text_cache_garbage_collection(self): # unique multi-codepoint cell texts, single width base + combining mark def unique_text(i):