⚡️ Speed up function _setup_sentry_tracing
by 93%
#51
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.
📄 93% (0.93x) speedup for
_setup_sentry_tracing
insentry_sdk/integrations/spark/spark_driver.py
⏱️ Runtime :
46.5 microseconds
→24.0 microseconds
(best of74
runs)📝 Explanation and details
The optimized code achieves a 93% speedup through two key optimizations:
1. Import Caching with Global Variable
from pyspark import SparkContext
calls with a cached global_spark_context_class
2. Idempotent Patching Prevention
_sentry_patched
attribute checking to prevent re-patchingSparkContext._do_init
_patch_spark_context_init()
returns early, avoiding expensive decorator re-applicationPerformance Impact by Test Case:
The optimizations are particularly effective for applications that repeatedly call these functions or switch between SparkContext instances, which is common in distributed Spark environments. The changes preserve all original functionality while dramatically reducing redundant work.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_setup_sentry_tracing-mg9oea66
and push.