Move hyperlink to end of hash table when re-referenced

This commit is contained in:
Kovid Goyal 2020-08-27 13:11:36 +05:30
parent 78dc93721d
commit c8e2061e2a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 13 additions and 1 deletions

View File

@ -105,7 +105,13 @@ get_id_for_hyperlink(Screen *screen, const char *id, const char *url) {
HyperLinkEntry *s = NULL; HyperLinkEntry *s = NULL;
if (pool->hyperlinks) { if (pool->hyperlinks) {
HASH_FIND_STR(pool->hyperlinks, key, s); HASH_FIND_STR(pool->hyperlinks, key, s);
if (s) return s->id; if (s) {
// Remove and re-add s so that it is the last entry in the hash table and
// The first entry is discarded when hash table is full.
HASH_DEL(pool->hyperlinks, s);
HASH_ADD_KEYPTR(hh, pool->hyperlinks, s->key, strlen(s->key), s);
return s->id;
}
} }
hyperlink_id_type new_id = 0; hyperlink_id_type new_id = 0;
if (pool->num_of_adds_since_garbage_collection >= MAX_ADDS_BEFORE_GC) screen_garbage_collect_hyperlink_pool(screen); if (pool->num_of_adds_since_garbage_collection >= MAX_ADDS_BEFORE_GC) screen_garbage_collect_hyperlink_pool(screen);

View File

@ -585,3 +585,9 @@ class TestScreen(BaseTest):
self.ae([(':1', 1), (':3', 2)], s.hyperlinks_as_list()) self.ae([(':1', 1), (':3', 2)], s.hyperlinks_as_list())
set_link('4'), s.draw('4') set_link('4'), s.draw('4')
self.ae([(':1', 1), (':3', 2), (':4', 3)], s.hyperlinks_as_list()) self.ae([(':1', 1), (':3', 2), (':4', 3)], s.hyperlinks_as_list())
s = self.create_screen()
set_link('1'), s.draw('1')
set_link('2'), s.draw('2')
set_link('1'), s.draw('1')
self.ae([(':2', 2), (':1', 1)], s.hyperlinks_as_list())