Audio config optimization #5408

Closed
benno1237 wants to merge 6 commits from audio_config_optimization into V3/develop
benno1237 commented 2021-10-17 17:52:17 +02:00 (Migrated from github.com)

Description of the changes

Sums up config calls in core audio by using .all()
Gets rid of guild_from_id wherever possible

Some config things I noticed while writing #5389. Currently, audio uses .all occasionally but sometimes also does multiple single config calls in a row. Sometimes even both combined in a single command. This PR fixes that

### Description of the changes Sums up config calls in core audio by using .all() Gets rid of guild_from_id wherever possible Some config things I noticed while writing #5389. Currently, audio uses .all occasionally but sometimes also does multiple single config calls in a row. Sometimes even both combined in a single command. This PR fixes that
aikaterna (Migrated from github.com) reviewed 2021-10-17 17:52:17 +02:00
PredaaA (Migrated from github.com) reviewed 2021-10-17 17:52:17 +02:00
Jackenmen (Migrated from github.com) requested changes 2021-12-26 01:49:29 +01:00
@ -40,3 +43,4 @@
)
player = lavalink.get_player(ctx.guild.id)
can_skip = await self._can_instaskip(ctx, ctx.author)
if (
Jackenmen (Migrated from github.com) commented 2021-12-26 01:33:14 +01:00

These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown).

            guild_data = await self.config.guild(ctx.guild).all()
            dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"])
            vote_enabled = guild_data["vote_enabled"]
These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown). ```suggestion guild_data = await self.config.guild(ctx.guild).all() dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"]) vote_enabled = guild_data["vote_enabled"] ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:41:02 +01:00

These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown).

        dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"])
These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown). ```suggestion dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"]) ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:36:58 +01:00

I'm pretty sure you wanted to switch to call to all() here:

        guild_data = await self.config.guild(ctx.guild).all()
        dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"])
        vote_enabled = guild_data["vote_enabled"]
I'm pretty sure you wanted to switch to call to `all()` here: ```suggestion guild_data = await self.config.guild(ctx.guild).all() dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"]) vote_enabled = guild_data["vote_enabled"] ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:37:59 +01:00

I'm pretty sure you wanted to switch to call to all() here:

        guild_data = await self.config.guild(ctx.guild).all()
        dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"])
        vote_enabled = guild_data["vote_enabled"]
I'm pretty sure you wanted to switch to call to `all()` here: ```suggestion guild_data = await self.config.guild(ctx.guild).all() dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"]) vote_enabled = guild_data["vote_enabled"] ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:39:37 +01:00

If we already know that the ID isn't in cache, we don't need to use setdefault():

                dj_enabled = self._dj_status_cache[ctx.guild.id] = (
                    await self.config.guild(ctx.guild).dj_enabled()
                )
If we already know that the ID isn't in cache, we don't need to use `setdefault()`: ```suggestion dj_enabled = self._dj_status_cache[ctx.guild.id] = ( await self.config.guild(ctx.guild).dj_enabled() ) ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:40:05 +01:00

If we already know that the ID isn't in cache, we don't need to use setdefault():

            dj_enabled = self._dj_status_cache[ctx.guild.id] = (
                await self.config.guild(ctx.guild).dj_enabled()
            )
If we already know that the ID isn't in cache, we don't need to use `setdefault()`: ```suggestion dj_enabled = self._dj_status_cache[ctx.guild.id] = ( await self.config.guild(ctx.guild).dj_enabled() ) ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:41:58 +01:00

These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown).

        dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"])
These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown). ```suggestion dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"]) ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:42:42 +01:00

If we already know that the ID isn't in cache, we don't need to use setdefault():

            dj_enabled = self._dj_status_cache[ctx.guild.id] = (
                await self.config.guild(ctx.guild).dj_enabled()
            )
If we already know that the ID isn't in cache, we don't need to use `setdefault()`: ```suggestion dj_enabled = self._dj_status_cache[ctx.guild.id] = ( await self.config.guild(ctx.guild).dj_enabled() ) ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:43:27 +01:00

These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown).

        dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"])
These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown). ```suggestion dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"]) ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:44:33 +01:00

These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown).

        dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"])
These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown). ```suggestion dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"]) ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:44:48 +01:00

typo

        max_volume = guild_data["max_volume"]
typo ```suggestion max_volume = guild_data["max_volume"] ```
Jackenmen (Migrated from github.com) commented 2021-12-26 01:45:13 +01:00

If we already know that the ID isn't in cache, we don't need to use setdefault():

            dj_enabled = self._dj_status_cache[ctx.guild.id] = (
                await self.config.guild(ctx.guild).dj_enabled()
            )
If we already know that the ID isn't in cache, we don't need to use `setdefault()`: ```suggestion dj_enabled = self._dj_status_cache[ctx.guild.id] = ( await self.config.guild(ctx.guild).dj_enabled() ) ```
@ -1477,3 +1479,4 @@
dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"])
if dj_enabled and not await self._can_instaskip(ctx, ctx.author):
ctx.command.reset_cooldown(ctx)
await self.send_embed_msg(
Jackenmen (Migrated from github.com) commented 2021-12-26 01:46:03 +01:00

These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown).

        dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"])
These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown). ```suggestion dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, guild_data["dj_enabled"]) ```
aikaterna (Migrated from github.com) reviewed 2022-01-10 18:04:57 +01:00
aikaterna (Migrated from github.com) commented 2022-01-10 18:04:57 +01:00

These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown).

More information about why those changes were made can be found at: https://discord.com/channels/133049272517001216/171665724262055936/902639810294988881

For clarity I will repeat some of what I said in the convo at that link when I partially reviewed these changes in that discussion without commenting on this issue:


Instead of

dj_enabled = self._dj_status_cache.setdefault(
            ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
        )

Change it to:

if ctx.guild.id in self._dj_status_cache:
    dj_enabled = self._dj_status_cache[ctx.guild.id]
else:
    dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled())

2 - Keep guild_from_id as is as it serves as a demo example for newcomers and that's what core cogs are supposed to do in my opinion
3 - Be considerate of .all() calls as they can be expensive, especially on larger bots
3.1 - Sometimes 2 config calls is cheaper than an .all() so if a command has early exits that's likely why .all() wasnt used on it... as making an expensive .all() call would be a waste of resources and potentially slow the bot down (like a user spamming [p]repeat when DJ mode is enabled)


Jack, if you want changes like the example here done in another PR that's understandable and I don't need to fight against it, this comment is for providing clarity on why changes like this were added to this PR.

> These are unnecessary changes to the code logic, let's just keep it as similar to the original code as possible since this PR is meant for config optimizations, not moving code around (especially when the reason for this moving is unknown). More information about why those changes were made can be found at: https://discord.com/channels/133049272517001216/171665724262055936/902639810294988881 For clarity I will repeat some of what I said in the convo at that link when I partially reviewed these changes in that discussion without commenting on this issue: ___ Instead of ``` dj_enabled = self._dj_status_cache.setdefault( ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled() ) ``` Change it to: ``` if ctx.guild.id in self._dj_status_cache: dj_enabled = self._dj_status_cache[ctx.guild.id] else: dj_enabled = self._dj_status_cache.setdefault(ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()) ``` 2 - Keep guild_from_id as is as it serves as a demo example for newcomers and that's what core cogs are supposed to do in my opinion 3 - Be considerate of .all() calls as they can be expensive, especially on larger bots 3.1 - Sometimes 2 config calls is cheaper than an .all() so if a command has early exits that's likely why .all() wasnt used on it... as making an expensive .all() call would be a waste of resources and potentially slow the bot down (like a user spamming [p]repeat when DJ mode is enabled) ___ Jack, if you want changes like the example here done in another PR that's understandable and I don't need to fight against it, this comment is for providing clarity on why changes like this were added to this PR.
aikaterna (Migrated from github.com) reviewed 2022-01-10 18:27:49 +01:00
aikaterna (Migrated from github.com) commented 2022-01-10 18:27:49 +01:00

I'm pretty sure you wanted to switch to call to all() here:

While I was talking with @Drapersniper about these changes and setdefault, he said this to me:

The idea behind dj_enabled here is to cache is in an attribute
and NEVER call config for it if its in the cache
his "improvement" here calls .all()
which means its always calling the dj_attribute
while i say this

dj_enabled = self._dj_status_cache.setdefault(
            ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled()
        )

this right now is broken in audio
because of the way setdefault works
so its not "broken" but its for behaving as intended
the idea here is self._dj_status_cache.setdefault will only call await self.config.guild(ctx.guild).dj_enabled() if the cache for this guild is empty
but thats not how setdefault works unfortunately
which means await self.config.guild(ctx.guild).dj_enabled() ALWAYS gets called

> I'm pretty sure you wanted to switch to call to `all()` here: While I was talking with @Drapersniper about these changes and setdefault, he said this to me: The idea behind dj_enabled here is to cache is in an attribute and NEVER call config for it if its in the cache his "improvement" here calls .all() which means its always calling the dj_attribute while i say this ``` dj_enabled = self._dj_status_cache.setdefault( ctx.guild.id, await self.config.guild(ctx.guild).dj_enabled() ) ``` this right now is broken in audio because of the way setdefault works so its not "broken" but its for behaving as intended the idea here is self._dj_status_cache.setdefault will only call await self.config.guild(ctx.guild).dj_enabled() if the cache for this guild is empty but thats not how setdefault works unfortunately which means await self.config.guild(ctx.guild).dj_enabled() ALWAYS gets called
Jackenmen commented 2026-05-22 07:23:49 +02:00 (Migrated from github.com)

It's been over 4 years since the last changes on this PR were made, and there are still unresolved review comments on this PR. It's time to just close this.

It's been over 4 years since the last changes on this PR were made, and there are still unresolved review comments on this PR. It's time to just close this.

Pull request closed

Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!5408
No description provided.