Add dedicated Cache for Members with Role Index
#2965
lajczik
started this conversation in
Feedback & Suggestions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
📝 Motivation
Currently, retrieving members associated with a specific role (e.g., using$\mathbf{O(N)}$ , where $N$ is the total number of members in the guild.
Role#getMembers()or internally viaMemberCacheView#getElementsWithRoles) requires JDA to iterate over the entire guild member cache. This operation isFor bots serving large guilds (e.g.,$>10,000$ members) that frequently perform role-based operations (like admin list generation, moderation tasks, or role-specific statistics), this continuous full cache scanning leads to significant CPU overhead and slow execution times. Developers often resort to building custom, external role-to-member maps, which adds complexity and potential for data inconsistency.
💡 Proposed Solution
Implement an optional, dedicated reverse index cache that maps Role IDs to a collection of Member IDs:
This index would be:
CacheFlag.Proposed Implementation Details
New
CacheFlag: Introduce a new flag to theJDABuilder:Usage: Users would enable it like so:
Performance Impact:
Role#getMembers()would becomeGuildMemberRoleAddEvent,GuildMemberRoleRemoveEvent), member joins/removes, and cache updates. This is a necessary trade-off for the performance gain.✅ Benefits
Beta Was this translation helpful? Give feedback.
All reactions