⚡️ Speed up function _setup_sentry_tracing by 93%#51
Open
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Open
⚡️ Speed up function _setup_sentry_tracing by 93%#51codeflash-ai[bot] wants to merge 1 commit intomasterfrom
_setup_sentry_tracing by 93%#51codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Conversation
The optimized code achieves a **93% speedup** through two key optimizations: **1. Import Caching with Global Variable** - Replaces repeated `from pyspark import SparkContext` calls with a cached global `_spark_context_class` - Line profiler shows the import overhead drops from 6,986ns to just 2,621ns on first call, with subsequent calls using the cached reference - This eliminates Python's module lookup overhead on repeated function calls **2. Idempotent Patching Prevention** - Adds `_sentry_patched` attribute checking to prevent re-patching `SparkContext._do_init` - When already patched, `_patch_spark_context_init()` returns early, avoiding expensive decorator re-application - Line profiler shows 4 out of 6 calls now take the early return path (1,614ns vs 30,118ns for full patching) **Performance Impact by Test Case:** - **Multiple context switches**: 410% faster on subsequent calls due to cached imports and patch detection - **No active context scenarios**: 384% faster when patching is required repeatedly - **Basic setup calls**: 25% faster for typical single-call scenarios 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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_tracinginsentry_sdk/integrations/spark/spark_driver.py⏱️ Runtime :
46.5 microseconds→24.0 microseconds(best of74runs)📝 Explanation and details
The optimized code achieves a 93% speedup through two key optimizations:
1. Import Caching with Global Variable
from pyspark import SparkContextcalls with a cached global_spark_context_class2. Idempotent Patching Prevention
_sentry_patchedattribute 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-mg9oea66and push.