-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
perf: optimize role manager cache getter #11239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
994e2cb to
e6c026c
Compare
kyranet
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes in GuildEmojiRoleManager#cache and GuildMemberRoleManager#cache are solid and definitely will see an improvement since it's not running a nested iteration anymore, however...
Changing GuildMember#_roles to be a Set<Snowflake> rather than a Snowflake[], whilst it's true it'll see performance gains in large datasets, the average role count is more like 2-3 roles (tested in my bot, 3_296_963 GuildMember instances, 6_291_128 member roles excluding everyone), therefore making a negligible CPU performance difference.
The memory usage does increase by replacing Array with Set substantially, see below.
Data
For 1 million sets of 5 entries each (which is above average anyways), the memory usage for Set is substantially higher:
-
Array:
- Empty: 40.47 MB | 71.21 MB
- 5 values: 40.47 MB | 71.46 MB (doesn't change because capacity)
-
Set:
- Empty: 154.91 MB | 187.46 MB
- 5 values: 231.21 MB | 265.71 MB
-
The above numbers are the difference between the start and the end.
-
globalThis.gc()is called before and after each test, using--expose-gc. -
Tested on Node.js v22.14.0
While we welcome performance improvements, specially when they make internal code easier to read, sometimes such improvement comes at the cost of higher memory usage, and this is a case where the impact is just too large to accept it. We can keep the #cache improvements nevertheless, those should work as-is without changes!
e6c026c to
e4c2ba9
Compare
Co-authored-by: Almeida <github@almeidx.dev>
I believe a
Setwould be better suited for the private member_rolescollection.I think the role cache getters will perform better by iterating over the
_rolessubset instead of every guild role.Context: Trying to do stuff with the roles of every guild member in a guild with 20000 members and 200 roles.
Status and versioning classification: