-
-
Notifications
You must be signed in to change notification settings - Fork 38
refactor(treewide): reorder statements to improve speed #953
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
base: main
Are you sure you want to change the base?
Conversation
Reviewer's GuideImplemented performance optimizations by caching repeated sentry initialization checks, flattening command logic for early returns, and reordering conditional flows to reduce redundant calls. Sequence diagram for optimized avatar command flowsequenceDiagram
participant User as actor User
participant Bot
participant AvatarCog
participant Discord as discord.Interaction
User->>Bot: Triggers avatar command
Bot->>AvatarCog: send_avatar(source, member)
AvatarCog->>Discord: response.send_message(files=files) (if member has avatar)
AvatarCog->>Discord: response.send_message(content=message, ephemeral, delete_after=30) (if no avatar)
Sequence diagram for optimized xkcd command flowsequenceDiagram
participant User as actor User
participant Bot
participant XKCDCog
User->>Bot: Triggers xkcd command
Bot->>XKCDCog: xkcd(ctx, comic_id)
alt comic_id is not provided
XKCDCog->>Bot: ctx.send_help("xkcd")
else comic_id is provided
XKCDCog->>XKCDCog: specific(ctx, comic_id)
end
Class diagram for CogLoader sentry optimization changesclassDiagram
class CogLoader {
+async _load_single_cog(path: Path)
+async _load_cog_group(cogs: Sequence[Path])
+async _process_single_file(path: Path)
+async _process_directory(path: Path)
+async load_cogs(path: Path)
+async load_cogs_from_folder(folder_name: str)
+static async setup(bot: commands.Bot)
-IS_INITIALIZED: bool
}
CogLoader --|> Exception : CogLoadError
class sentry_sdk {
+is_initialized()
+get_current_span()
}
CogLoader ..> sentry_sdk : uses
CogLoader ..> safe_set_name
CogLoader ..> span
CogLoader ..> start_span
CogLoader ..> transaction
Class diagram for avatar command logic refactorclassDiagram
class AvatarCog {
+async send_avatar(source, member: discord.Member)
+async create_avatar_file(avatar)
}
AvatarCog ..> discord.Interaction
AvatarCog ..> commands.Context
AvatarCog ..> discord.Member
AvatarCog ..> commands.MemberConverter
Class diagram for xkcd command logic refactorclassDiagram
class XKCDCog {
+async xkcd(ctx: commands.Context[Tux], comic_id: int | None = None)
+async specific(ctx, comic_id)
}
XKCDCog ..> commands.Context
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey @amadaluzia - I've reviewed your changes - here's some feedback:
- Caching sentry_sdk.is_initialized() in IS_INITIALIZED at import time assumes Sentry is initialized before this module loads—consider a lazy check or helper function to avoid stale values if initialization happens later.
- There’s a lot of repeated
if IS_INITIALIZED and (current_span := get_current_span())
boilerplate—extracting that into a small helper would reduce repetition and make future span calls easier to maintain. - The avatar refactor removes the fallback that converted the context author into a Member when no member was passed; double-check that you’re not losing intended behavior for context commands without explicit members.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Caching sentry_sdk.is_initialized() in IS_INITIALIZED at import time assumes Sentry is initialized before this module loads—consider a lazy check or helper function to avoid stale values if initialization happens later.
- There’s a lot of repeated `if IS_INITIALIZED and (current_span := get_current_span())` boilerplate—extracting that into a small helper would reduce repetition and make future span calls easier to maintain.
- The avatar refactor removes the fallback that converted the context author into a Member when no member was passed; double-check that you’re not losing intended behavior for context commands without explicit members.
## Individual Comments
### Comment 1
<location> `tux/cogs/fun/xkcd.py:38` </location>
<code_context>
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
</code_context>
<issue_to_address>
The logic for handling comic_id=0 may have changed unintentionally.
The updated condition causes comic_id=0 to trigger the help message instead of fetching comic 0. If 0 is valid, use 'if comic_id is not None' to avoid this regression.
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
@sourcery-ai review |
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.
Hey @amadaluzia - I've reviewed your changes - here's some feedback:
- You have a lot of repeated
if IS_INITIALIZED and get_current_span()
checks—consider extracting that pattern into a helper or decorator to reduce duplication and improve readability. - Caching
sentry_sdk.is_initialized()
at import time can become stale if Sentry is initialized later—confirm that initialization always happens before this module loads or revert to a dynamic check. - The
avatar
command now only usessource.response
for sending, so verify that non-Interaction (Context) invocations still behave as expected or explicitly handle both cases.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- You have a lot of repeated `if IS_INITIALIZED and get_current_span()` checks—consider extracting that pattern into a helper or decorator to reduce duplication and improve readability.
- Caching `sentry_sdk.is_initialized()` at import time can become stale if Sentry is initialized later—confirm that initialization always happens before this module loads or revert to a dynamic check.
- The `avatar` command now only uses `source.response` for sending, so verify that non-Interaction (Context) invocations still behave as expected or explicitly handle both cases.
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Codecov ReportAttention: Patch coverage is
✅ All tests successful. No failed tests found.
Additional details and impacted files@@ Coverage Diff @@
## main #953 +/- ##
========================================
+ Coverage 9.26% 9.39% +0.12%
========================================
Files 123 123
Lines 10390 10388 -2
Branches 1276 1274 -2
========================================
+ Hits 963 976 +13
+ Misses 9325 9298 -27
- Partials 102 114 +12
*This pull request uses carry forward flags. Click here to find out more.
☔ View full report in Codecov by Sentry. |
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.
changes look good, please resolve the ambiguous naming of the IS_INITIALIZED variable
@@ -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() |
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.
it's not immediately clear what IS_INITIALIZED is referring to exactly. consider naming the variable something like SENTRY_INITIALIZED instead for clarity.
abc5368
to
a50dbb3
Compare
Description
This refactors the tree to optimise the cog loader, the
avatar
command and thexkcd
command with reordered if statements, and storing variables to remove unnecessary function calls. I think this is clean enough at the moment for me to write this as a PR. There will of course still be some unoptimal things I have missed but I cannot seem to find any that wouldn't conflict with #853.Guidelines
My code follows the style guidelines of this project (formatted with Ruff)
I have performed a self-review of my own code
I have commented my code, particularly in hard-to-understand areas
I have made corresponding changes to the documentation if needed
My changes generate no new warnings
I have tested this change
Any dependent changes have been merged and published in downstream modules
I have added all appropriate labels to this PR
I have followed all of these guidelines.
How Has This Been Tested? (if applicable)
I started Tux and ran a few commands.
Screenshots (if applicable)
Additional Information
Please add any other information that is important to this PR.
Summary by Sourcery
Refactor various commands and the cog loader to improve performance and readability by caching sentry initialization checks, reordering conditionals, and flattening nested branches
Enhancements: