⚡️ Speed up function _add_langfuse_trace_id_to_alert by 617,051%
#122
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.
📄 617,051% (6,170.51x) speedup for
_add_langfuse_trace_id_to_alertinlitellm/integrations/SlackAlerting/utils.py⏱️ Runtime :
208 milliseconds→33.7 microseconds(best of5runs)📝 Explanation and details
The optimization adds caching for LangFuseLogger instances to eliminate expensive object creation on every callback request.
Key optimization:
_langfuse_logger_cachedictionary that stores LangFuseLogger instances keyed by(public_key, secret, host)tuples_get_callback_object(), the code now checks the cache first before creating new LangFuseLogger instancesWhy this creates massive speedup:
The line profiler shows that
LangFuseLoggerinstantiation in the original code takes ~0.213 seconds (99.9% of total runtime). This expensive constructor was being called repeatedly for the same configuration parameters. By caching instances, subsequent calls with identical parameters reuse existing objects, reducing the operation from 200+ milliseconds to microseconds.Performance characteristics:
The optimization maintains identical behavior and API while dramatically improving performance for repeated callback operations with consistent logging configurations.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_add_langfuse_trace_id_to_alert-mhc5lcziand push.