Skip to content

Commit

Permalink
perf(help.py): cache prefixes to reduce redundant fetch operations
Browse files Browse the repository at this point in the history
Cache prefixes for each guild to improve performance by minimizing
repeated fetch operations. This change reduces the overhead of
dynamically fetching prefixes, especially in environments with
multiple guilds.
  • Loading branch information
kzndotsh committed Nov 20, 2024
1 parent 303ea38 commit 94f6a81
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions tux/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ def __init__(self):
"usage": "$help <command> or <sub-command>",
},
)
self._prefix_cache: dict[int | None, str] = {}

# Helpers for Prefix and Embeds
async def _get_prefix(self) -> str:
"""Dynamically fetches the prefix from the context or uses a default prefix."""
return self.context.clean_prefix or CONFIG.DEFAULT_PREFIX
"""Fetches and caches the prefix for each guild."""
guild_id = self.context.guild.id if self.context.guild else None

if guild_id not in self._prefix_cache:
# Fetch and cache the prefix specific to the guild
self._prefix_cache[guild_id] = self.context.clean_prefix or CONFIG.DEFAULT_PREFIX

return self._prefix_cache[guild_id]

def _embed_base(self, title: str, description: str | None = None) -> discord.Embed:
"""Creates a base embed with uniform styling."""
Expand Down

0 comments on commit 94f6a81

Please sign in to comment.