From 4d8ebcce297aa025b5fa43e9c066e3ce3ee3a9d0 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Thu, 29 May 2025 11:55:35 +0530 Subject: [PATCH] perf: Apply backpressure on link counts (#32712) Context: This is QoL feature to highlight most used items. But in high throughput environments where a lot of new documents are being created this becomes a bottleneck. Fix: Limit the size of counts that can be buffered before they're flushed. Statistically this will still work just as well as it did before. --- frappe/model/utils/link_count.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/frappe/model/utils/link_count.py b/frappe/model/utils/link_count.py index e2bc3f85f6..622045fb69 100644 --- a/frappe/model/utils/link_count.py +++ b/frappe/model/utils/link_count.py @@ -29,6 +29,9 @@ ignore_doctypes = { } +LINK_COUNT_BUFFER_SIZE = 256 + + def notify_link_count(doctype, name): """updates link count for given document""" @@ -50,13 +53,18 @@ def flush_local_link_count(): link_count = frappe.cache.get_value("_link_count") or {} + flush = False for key, value in new_links.items(): if key in link_count: link_count[key] += value - else: + elif len(link_count) < LINK_COUNT_BUFFER_SIZE: link_count[key] = value + else: + continue + flush = True - frappe.cache.set_value("_link_count", link_count) + if flush: + frappe.cache.set_value("_link_count", link_count) new_links.clear()