⚡️ Speed up method SSOConfigService._db_config_to_sso_config by 12% in PR #11399 (feature/sso-implementation)
#11432
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.
⚡️ This pull request contains optimizations for PR #11399
If you approve this dependent PR, these changes will be merged into the original PR branch
feature/sso-implementation.📄 12% (0.12x) speedup for
SSOConfigService._db_config_to_sso_configinsrc/backend/base/langflow/services/auth/sso_service.py⏱️ Runtime :
1.88 milliseconds→1.68 milliseconds(best of31runs)📝 Explanation and details
The optimized code achieves a 12% speedup by eliminating repeated import overhead and reducing redundant string comparisons:
Key Optimizations:
Hoisted imports to module level (
AuthProviderandSSOProviderConfig): The original code imported these classes inside_db_config_to_sso_configon every call. Line profiler shows these imports consumed ~4.2% of runtime (231,905ns + 166,434ns out of 6,383,280ns total). Moving them to module level eliminates this repeated cost—they're now loaded once when the module imports.Cached the provider check: The original code checked
db_config.provider == "oidc"twice—once to decide whether to createOIDCConfig, and again to determine theoidcfield value inSSOProviderConfig. The optimized version stores this result inis_oidcand reuses it, avoiding a redundant string comparison (line profiler shows the second check at ~1.1% runtime).Immutable default scopes constant: The original code created a new list
["openid", "email", "profile"]every time scopes were missing. The optimized version stores these as a tuple_DEFAULT_SCOPES_TUPLEat module level and creates a fresh list vialist(_DEFAULT_SCOPES_TUPLE)only when needed. This reduces allocations in the common case where scopes are provided (no measurable change in line profiler, but cleaner for repeated executions).Why This Matters:
SSOConfigService, which handles authentication configuration. If this method is called frequently during login flows or configuration validation (e.g., in middleware, per-request validation, or batch user operations), the cumulative savings from eliminating repeated imports and checks can meaningfully reduce latency.Impact Analysis:
Without
function_references, we cannot definitively assess whether this function is in a hot path. However, SSO configuration conversion is typically invoked during:If called frequently (e.g., in a high-traffic authentication service), the 12% speedup translates to tangible latency improvements. The optimization is particularly effective when the function is called in loops or batches (as evidenced by test cases like
test_multiple_consecutive_conversionswith 100 iterations).✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr11399-2026-01-23T18.10.50and push.