⚡️ Speed up function send by 130%
#110
Open
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.
📄 130% (1.30x) speedup for
sendinskyvern/forge/sdk/api/email.py⏱️ Runtime :
2.12 milliseconds→920 microseconds(best of29runs)📝 Explanation and details
The optimization addresses a critical async performance issue by properly handling blocking I/O operations in the
_sendfunction.Key optimization applied:
smtplib.SMTP,starttls(),login(),send_message()) directly in the async function, which blocks the event loop and prevents other async operations from proceeding.asyncio.to_thread(): Wrapped all SMTP operations in a nested function and executed it viaasyncio.to_thread(), offloading the blocking I/O to a thread pool while keeping the async interface intact.smtp_host.quit()to ensure SMTP connections are properly closed.Why this leads to speedup:
In async Python, blocking I/O operations in the main thread freeze the entire event loop. While one email is being sent (network I/O), no other async tasks can execute. By moving SMTP operations to a thread, the event loop remains free to handle other tasks concurrently, dramatically improving throughput when multiple operations are running.
Impact on workloads:
Based on the function reference, this optimization is particularly valuable since
email.send()is called from a workflow block that also performs database operations, logging, and timing loops. The workflow context shows this happens during human interaction workflows where the system needs to send emails while simultaneously monitoring database state changes and handling timeouts. The async improvement ensures email sending doesn't block these concurrent operations.Test case performance:
The optimization shows consistent improvements across test scenarios involving email validation errors and concurrent operations, with runtime dropping from 2.12ms to 920μs (130% speedup) and throughput increasing by 3.8%. This is especially beneficial for the concurrent test cases where multiple email operations can now truly run in parallel rather than sequentially blocking each other.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-send-mir96tiuand push.