From 260eaba88ec8f54fe2bdbe391b18fcd2db70836f Mon Sep 17 00:00:00 2001 From: M Stoeckl Date: Thu, 31 Oct 2024 09:23:26 -0400 Subject: Optimize menu sorting Sorting and deduplicating elements after all items have been registered improves the time complexity of constructing the item list from O(n^2) to O(n log n). On a system with about 4000 menu items, this reduces startup time from about 0.21 seconds to 0.13 seconds. --- render.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'render.c') diff --git a/render.c b/render.c index 3813af5..070fad9 100644 --- a/render.c +++ b/render.c @@ -28,7 +28,8 @@ void calc_widths(struct menu *menu) { menu->right_arrow = text_width(cairo, menu->font, ">") + 2 * menu->padding; // Calculate item widths and input area width - for (struct item *item = menu->items; item; item = item->next) { + for (size_t i = 0; i < menu->item_count; i++) { + struct item *item = &menu->items[i]; item->width = text_width(cairo, menu->font, item->text); if (item->width > menu->inputw) { menu->inputw = item->width; -- cgit v1.2.3