[Alias] Create caching to call config less frequently #3788
No reviewers
Labels
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 project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot!3788
Loading…
Reference in a new issue
No description provided.
Delete branch "alias-caching"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type
Description of the changes
Add a caching system to Alias to reduce frequency of calls to config. This also re-arranges some functions inside alias to improve readability and usability.
This also changes aliases to be checked on_message_without_command to avoid the potential of loading a cog with a command with the same name as an alias.
This method is used in two other places outside Alias cog:
https://github.com/Cog-Creators/Red-DiscordBot/search?q=bot.get_cog%28%22Alias%22%29&unscoped_q=bot.get_cog%28%22Alias%22%29
@ -417,3 +281,4 @@if await self._aliases.delete_alias(ctx, alias_name, global_=True):await ctx.send(_("Alias with the name `{name}` was successfully deleted.").format(name=alias_name))This doesn't really matter with cache turned on but assuming it would be possible to disable in future, result of this coro should probably be assigned to a variable to avoid calling Config twice.
This doesn't really matter with cache turned on but assuming it would be possible to disable in future, result of this coro should probably be assigned to a variable to avoid calling Config twice.
@ -32,1 +44,4 @@def get_extra_args_from_alias(self, message: discord.Message, prefix: str) -> str:"""When an alias is executed by a user in chat this function triesConsidering this returns an
AliasEntryobject, not abool, we should call thisget_aliasinstead ofis_alias.You shouldn't be calling Config on each guild again if you already have all the needed data.
Also, considering all the optimizations for big bots that we've done lately, this should probably be wrapped in
AsyncIterclass fromredbot.core.utilsto avoid blocking event loop on alias loading for bigger bots.Could we do this on cog load instead? It would allow us to skip this check in every method accessing the cache and it would assure that this won't be called several times before the cache is fully loaded (i.e. someone spams an alias). If that's not an option for some reason, the parts that load aliases would need some lock.
(this is mostly a nitpick)
We could use a dict comprehension instead here and (imo) simplify this.
It would be faster to search the alias with a key lookup in the cache dict instead of iterating through list of alias objects.
Having these as iterators as they were before and chaining them would probably be a little better however you can consider this a nitpick
I know this isn't a part of this PR so if you'll leave as is, it's fine.
Could we skip creating alias entry for each alias and just check for
alias["name"]?@ -64,0 +125,4 @@else:aliases = [AliasEntry.from_json(d) for d in await self.config.guild(guild).entries()]return aliasesThere was a reason for this
try: excepthere - we want the JSON dict to have user/guild ID, not Member/Guild object.With that said, you could probably do an
if self.has_real_datainstead oftry: except@ -64,0 +168,4 @@) -> AliasEntry:indices = findall(r"{(\d*)}", command)if indices:try:(this is mostly a nitpick)
We could use a dict comprehension instead here and (imo) simplify this.
@ -64,0 +125,4 @@else:aliases = [AliasEntry.from_json(d) for d in await self.config.guild(guild).entries()]return aliasesThis was removed because I ensured all creation of AliasEntry objects only accepted the ID not the object itself. I'll further ensure that with typehints to only accept int and not the object themselves. They served no purpose being the object and just a reference ID is good enough.
@ -32,1 +44,4 @@def get_extra_args_from_alias(self, message: discord.Message, prefix: str) -> str:"""When an alias is executed by a user in chat this function triesI was going to but that breaks our tests for the cog.
@ -64,0 +125,4 @@else:aliases = [AliasEntry.from_json(d) for d in await self.config.guild(guild).entries()]return aliasesI was basing this comment on
from_jsonmethod which could change it into Member/Guild objects if bot was passed. I know it's never passed so maybe you could just remove this all together or at least add a comment that some changes toAliasEntrywould be needed if we would start to pass the bot object@ -32,1 +44,4 @@def get_extra_args_from_alias(self, message: discord.Message, prefix: str) -> str:"""When an alias is executed by a user in chat this function triesYou can do the loading in the alias fixture here: https://github.com/TrustyJAID/Red-DiscordBot/blob/alias-caching/redbot/pytest/alias.py#L12
@ -32,1 +44,4 @@def get_extra_args_from_alias(self, message: discord.Message, prefix: str) -> str:"""When an alias is executed by a user in chat this function triesI feel like the current method is more readable and easier for others to verify it's working correctly. List and Dict comprehensions while sometimes make the code look cleaner can also make it less readable and this is one where I think it could be misinterpreted.
@ -30,34 +42,182 @@ class AliasEntry:self.uses += 1These need to be opposite order - global aliases take precedence over guild aliases.
If you care for my nitpicks, this code would avoid doing key lookups multiple times, otherwise just ignore this part, I couldn't stop myself from putting this here :P
Looks good, thanks so much for the PR <3