⚡️ Speed up method AwsEnvironment.get_partition by 86%
#84
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 86% (0.86x) speedup for
AwsEnvironment.get_partitioninwandb/sdk/launch/environment/aws_environment.py⏱️ Runtime :
2.21 seconds→1.19 seconds(best of60runs)📝 Explanation and details
The optimization reduces unnecessary thread executor overhead by eliminating redundant async wrapping for lightweight, non-blocking operations.
Key Changes:
boto3.Sessionconstructor - This is a lightweight object creation that doesn't perform network I/O, so calling it synchronously eliminates executor overheadsession.client()creation - Client creation just builds a service client object without network calls, so it can run synchronouslyclient.get_caller_identity()- This is the actual network-bound AWS API call that benefits from being run in an executor threadWhy This Is Faster:
The original code wrapped every boto3 operation in
event_loop_thread_exec, creating unnecessary async executor tasks. Each executor call adds overhead: thread pool scheduling, context switching, and future/task creation. The line profiler shows the most time was spent inclient = await event_loop_thread_exec(session.client)("sts")(75.9% of total time), which was purely overhead sincesession.client()is non-blocking.Performance Results:
The optimization maintains identical functionality while dramatically reducing async executor overhead for operations that don't need it.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-AwsEnvironment.get_partition-mhe48zznand push.