From a50dbb3bc2a18c577bc1798f81780cd68964b187 Mon Sep 17 00:00:00 2001 From: Artur Manuel Date: Wed, 16 Jul 2025 22:09:31 +0100 Subject: [PATCH] refactor(treewide): reorder statements to improve speed --- tux/cog_loader.py | 46 +++++++++++++++++++++-------------------- tux/cogs/fun/xkcd.py | 7 ++++--- tux/cogs/info/avatar.py | 12 +++-------- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/tux/cog_loader.py b/tux/cog_loader.py index b54e4195..82cb6229 100644 --- a/tux/cog_loader.py +++ b/tux/cog_loader.py @@ -14,6 +14,8 @@ from tux.utils.config import CONFIG from tux.utils.sentry import safe_set_name, span, start_span, transaction +IS_INITIALIZED: bool = sentry_sdk.is_initialized() + class CogLoadError(Exception): """Raised when a cog fails to load.""" @@ -90,7 +92,7 @@ async def _load_single_cog(self, path: Path) -> None: cog_name = path.stem # Add span tags for the current cog - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_tag("cog.name", cog_name) current_span.set_tag("cog.path", str(path)) @@ -101,7 +103,7 @@ async def _load_single_cog(self, path: Path) -> None: # Convert path to module format (e.g., tux.cogs.admin.dev) module = f"tux.{str(relative_path).replace('/', '.').replace('\\', '.')[:-3]}" - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_tag("cog.module", module) # Check if this module or any parent module is already loaded @@ -112,7 +114,7 @@ async def _load_single_cog(self, path: Path) -> None: check_module = ".".join(module_parts[:i]) if check_module in self.bot.extensions: logger.warning(f"Skipping {module} as {check_module} is already loaded") - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_tag("cog.status", "skipped") current_span.set_tag("cog.skip_reason", "already_loaded") current_span.set_data("already_loaded_module", check_module) @@ -124,7 +126,7 @@ async def _load_single_cog(self, path: Path) -> None: self.load_times[module] = load_time # Add telemetry data to span - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_tag("cog.status", "loaded") current_span.set_data("load_time_ms", load_time * 1000) current_span.set_data("load_time_s", load_time) @@ -132,7 +134,7 @@ async def _load_single_cog(self, path: Path) -> None: logger.debug(f"Successfully loaded cog {module} in {load_time * 1000:.0f}ms") except Exception as e: - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_status("internal_error") current_span.set_tag("cog.status", "failed") current_span.set_data("error", str(e)) @@ -173,7 +175,7 @@ async def _load_cog_group(self, cogs: Sequence[Path]) -> None: return # Add basic info for the group - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_data("cog_count", len(cogs)) if categories := {cog.parent.name for cog in cogs if cog.parent}: @@ -188,7 +190,7 @@ async def _load_cog_group(self, cogs: Sequence[Path]) -> None: success_count = len([r for r in results if not isinstance(r, Exception)]) failure_count = len(results) - success_count - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_data("load_time_s", end_time - start_time) current_span.set_data("success_count", success_count) current_span.set_data("failure_count", failure_count) @@ -200,14 +202,14 @@ async def _load_cog_group(self, cogs: Sequence[Path]) -> None: async def _process_single_file(self, path: Path) -> None: """Process a single file path.""" - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_tag("path.is_dir", False) if await self.is_cog_eligible(path): await self._load_single_cog(path) async def _process_directory(self, path: Path) -> None: """Process a directory of cogs.""" - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_tag("path.is_dir", True) # Collect and sort eligible cogs by priority @@ -216,7 +218,7 @@ async def _process_directory(self, path: Path) -> None: ] cog_paths.sort(key=lambda x: x[0], reverse=True) - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_data("eligible_cog_count", len(cog_paths)) # Priority groups info for observability @@ -254,7 +256,7 @@ async def load_cogs(self, path: Path) -> None: The path to the directory containing cogs. """ # Add span context - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_tag("cog.path", str(path)) try: @@ -268,7 +270,7 @@ async def load_cogs(self, path: Path) -> None: path_str = path.as_posix() logger.error(f"An error occurred while processing {path_str}: {e}") - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_status("internal_error") current_span.set_data("error", str(e)) current_span.set_data("traceback", traceback.format_exc()) @@ -286,7 +288,7 @@ async def load_cogs_from_folder(self, folder_name: str) -> None: The name of the folder containing the cogs. """ # Add span info - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_tag("cog.folder", folder_name) # Use safe_set_name instead of direct set_name call safe_set_name(current_span, f"Load Cogs: {folder_name}") @@ -294,14 +296,14 @@ async def load_cogs_from_folder(self, folder_name: str) -> None: start_time = time.perf_counter() cog_path: Path = Path(__file__).parent / folder_name - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_data("full_path", str(cog_path)) try: await self.load_cogs(path=cog_path) load_time = time.perf_counter() - start_time - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_data("load_time_s", load_time) current_span.set_data("load_time_ms", load_time * 1000) @@ -311,12 +313,12 @@ async def load_cogs_from_folder(self, folder_name: str) -> None: # Log individual cog load times for performance monitoring slow_threshold = 1.0 # seconds if slow_cogs := {k: v for k, v in self.load_times.items() if v > slow_threshold}: - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_data("slow_cogs", slow_cogs) logger.warning(f"Slow loading cogs (>{slow_threshold * 1000:.0f}ms): {slow_cogs}") except Exception as e: - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_status("internal_error") current_span.set_data("error", str(e)) current_span.set_data("traceback", traceback.format_exc()) @@ -335,12 +337,12 @@ async def setup(cls, bot: commands.Bot) -> None: bot : commands.Bot The bot instance. """ - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): - current_span.set_tag("bot.id", bot.user.id if bot.user else "unknown") - start_time = time.perf_counter() cog_loader = cls(bot) + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): + current_span.set_tag("bot.id", bot.user.id if bot.user else "unknown") + try: # Load handlers first (they have highest priority) with start_span("cog.load_handlers", "Load handler cogs"): @@ -356,7 +358,7 @@ async def setup(cls, bot: commands.Bot) -> None: total_time = time.perf_counter() - start_time - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_data("total_load_time_s", total_time) current_span.set_data("total_load_time_ms", total_time * 1000) @@ -367,7 +369,7 @@ async def setup(cls, bot: commands.Bot) -> None: logger.info(f"Total cog loading time: {total_time * 1000:.0f}ms") except Exception as e: - if sentry_sdk.is_initialized() and (current_span := sentry_sdk.get_current_span()): + if IS_INITIALIZED and (current_span := sentry_sdk.get_current_span()): current_span.set_status("internal_error") current_span.set_data("error", str(e)) current_span.set_data("traceback", traceback.format_exc()) diff --git a/tux/cogs/fun/xkcd.py b/tux/cogs/fun/xkcd.py index f70d9037..a0695fd0 100644 --- a/tux/cogs/fun/xkcd.py +++ b/tux/cogs/fun/xkcd.py @@ -35,10 +35,11 @@ async def xkcd(self, ctx: commands.Context[Tux], comic_id: int | None = None) -> The ID of the xkcd comic to search for. """ - if comic_id: - await self.specific(ctx, comic_id) - else: + if not comic_id: await ctx.send_help("xkcd") + return + + await self.specific(ctx, comic_id) @xkcd.command( name="latest", diff --git a/tux/cogs/info/avatar.py b/tux/cogs/info/avatar.py index 1e226767..dee1c8e9 100644 --- a/tux/cogs/info/avatar.py +++ b/tux/cogs/info/avatar.py @@ -75,22 +75,16 @@ async def send_avatar( member : discord.Member The member to get the avatar of. """ - if member is not None: + if isinstance(source, discord.Interaction) and member is not None: guild_avatar = member.guild_avatar.url if member.guild_avatar else None global_avatar = member.avatar.url if member.avatar else None files = [await self.create_avatar_file(avatar) for avatar in [guild_avatar, global_avatar] if avatar] if files: - if isinstance(source, discord.Interaction): - await source.response.send_message(files=files) - else: - await source.reply(files=files) + await source.response.send_message(files=files) else: message = "Member has no avatar." - if isinstance(source, discord.Interaction): - await source.response.send_message(content=message, ephemeral=True, delete_after=30) - else: - await source.reply(content=message, ephemeral=True, delete_after=30) + await source.response.send_message(content=message, ephemeral=True, delete_after=30) elif isinstance(source, commands.Context): member = await commands.MemberConverter().convert(source, str(source.author.id))