openbox/menuframe: optimize menu list construction and counting #36
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch introduces two optimizations for large menus.
1. Cache the number of menu entries
The new
num_entriesfield stores the total number of menu items.This removes repeated O(N) calls to
g_list_lengthduring geometry updates and placement calculations such asmenu_frame_move_on_screen.2. Reduce menu population from O(N²) to O(N)
The previous implementation used
g_list_appendinside the population loop, causing a linear scan on every iteration.The replacement uses
g_list_prependin the loop and a singleg_list_reverseonce the loop is complete.Overall Effect
Together, these changes dramatically reduce CPU usage when opening or navigating very large menus (such as full “All Applications” views).
Performance Impact
For a menu with N items: