⚡️ Speed up function sanitize_query_string by 279% in PR #11689 (aka/traces-v0)#11945
Closed
codeflash-ai[bot] wants to merge 72 commits into
Closed
⚡️ Speed up function sanitize_query_string by 279% in PR #11689 (aka/traces-v0)#11945codeflash-ai[bot] wants to merge 72 commits into
sanitize_query_string by 279% in PR #11689 (aka/traces-v0)#11945codeflash-ai[bot] wants to merge 72 commits into
Conversation
v0 for traces includes: - filters: status, token usage range and datatime - accordian rows per trace Could add: - more filter options. Ecamples: session_id, trace_id and latency range
add sidebar buttons for logs and trace remove lods canvas control
hopefully fix duplicate trace ID insertion on windows
update tests and alembic tables for uts
was flow_name - trace_id now flow_name - flow_id
address gabriel simple changes in traces.py and native.py
model name is now set using name = f"{operation} {model_name}" if model_name else operation
* feat: use uv sources for CPU-only PyTorch Configure [tool.uv.sources] with pytorch-cpu index to avoid ~6GB CUDA dependencies in Docker images. This replaces hardcoded wheel URLs with a cleaner index-based approach. - Add pytorch-cpu index with explicit = true - Add torch/torchvision to [tool.uv.sources] - Add explicit torch/torchvision deps to trigger source override - Regenerate lockfile without nvidia/cuda/triton packages - Add required-environments for multi-platform support Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: update regex to only replace name in [project] section The previous regex matched all lines starting with `name = "..."`, which incorrectly renamed the UV index `pytorch-cpu` to `langflow-nightly` during nightly builds. This caused `uv lock` to fail with: "Package torch references an undeclared index: pytorch-cpu" The new regex specifically targets the name field within the [project] section only, avoiding unintended replacements in other sections like [[tool.uv.index]]. * style: fix ruff quote style * fix: remove required-environments to fix Python 3.13 macOS x86_64 CI The required-environments setting was causing hard failures when packages like torch didn't have wheels for specific platform/Python combinations. Without this setting, uv resolves optimistically and handles missing wheels gracefully at runtime instead of failing during resolution. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* LE-270: add fix hydration issues * LE-270: fix disable field on max token on language model --------- Co-authored-by: Olayinka Adelakun <olayinkaadelakun@mac.war.can.ibm.com>
* Add wait for selector in mcp server tests * [autofix.ci] apply automated fixes * Add more awit for selectors * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
* Reduce lag in frontend by batching react events and reducing minimval visual build time * Cleanup * [autofix.ci] apply automated fixes * add tests and improve code read * [autofix.ci] apply automated fixes * Remove debug log --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: cristhianzl <cristhian.lousa@gmail.com>
* Lazy load imports for language model component Ensures that only the necessary dependencies are required. For example, if OpenAI provider is used, it will now only import langchain_openai, rather than requiring langchain_anthropic, langchain_ibm, etc. * Add backwards-compat functions * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes (attempt 2/3) * Add exception handling * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes (attempt 2/3) * comp index * docs: azure default temperature (#11829) * change-azure-openai-default-temperature-to-1.0 * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes (attempt 2/3) * [autofix.ci] apply automated fixes (attempt 3/3) * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes (attempt 2/3) * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes (attempt 2/3) * fix unit test? * add no-group dev to docker builds * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Hamza Rashid <74062092+HzaRashid@users.noreply.github.com> Co-authored-by: Mendon Kissling <59585235+mendonk@users.noreply.github.com>
address backend code rabbit comments
address code rabbit frontend comments
test_native_tracer minor fix address c1
address C2 + C3
address H1-H5
update test_native_tracer
address m2
… into aka/traces-v0
address M1
fix 422 spam and clean comments
address M12
address M3
address M4
address M5
clean up for M7, M9, M11
address L2,L4,L5 and L6 + any test
alembic + comment clean up
The optimized code achieves a **279% speedup** (from 40.7ms to 10.7ms) by replacing a character-by-character Python generator expression with a pre-compiled regular expression that operates in optimized C code. **Key Changes:** 1. **Pre-compiled regex pattern** (`_NON_PRINTABLE_RE`): Compiled once at module import, this regex matches sequences of non-printable characters (`[^\x20-\x7E]+`) and removes them in a single optimized operation. The line profiler shows this drops from **196ms** (99.7% of time) to just **12.3ms** (94.3% of time). 2. **Fast-path for empty strings**: The explicit `if value == ""` check avoids unnecessary regex work for empty inputs, returning `None` immediately. **Why This Is Faster:** - **Generator overhead eliminated**: The original `"".join(ch for ch in value if " " <= ch <= "~")` creates a generator object, iterates character-by-character in Python bytecode, performs a comparison for each character, and builds intermediate string objects. This is pure Python-level work. - **Regex operates in C**: The `re.sub()` call hands the entire string to highly optimized C code that scans and filters in a single pass with minimal Python object creation. Regular expression engines are designed for exactly this kind of bulk pattern matching. - **Reduced memory churn**: The generator/join approach creates many intermediate Python objects during iteration. The regex performs the same filtering with far fewer allocations. **Impact on Workloads:** - **Best for**: Strings with many printable characters (typical query strings, URLs, JSON) where the regex can quickly scan and copy valid ranges. Test cases like `test_large_string_within_limit` and `test_repeated_pattern_large` demonstrate consistent speedups on realistic inputs. - **Also benefits**: Strings with many non-printable characters to filter (e.g., `test_large_string_mostly_invalid`), as the regex efficiently skips invalid character runs rather than checking each one individually. - **Minimal overhead added**: The empty string fast-path and one-time regex compilation at import add negligible cost while preserving exact behavioral compatibility (all tests pass identically). This optimization is particularly valuable if `sanitize_query_string` is called frequently in request processing pipelines, as the per-call savings compound across many invocations.
Contributor
Codecov Report❌ Patch coverage is ❌ Your project status has failed because the head coverage (41.46%) is below the target coverage (60.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #11945 +/- ##
==========================================
+ Coverage 36.40% 36.53% +0.12%
==========================================
Files 1570 1580 +10
Lines 76655 77116 +461
Branches 11629 11778 +149
==========================================
+ Hits 27910 28178 +268
- Misses 47169 47325 +156
- Partials 1576 1613 +37
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Contributor
Author
|
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.
⚡️ This pull request contains optimizations for PR #11689
If you approve this dependent PR, these changes will be merged into the original PR branch
aka/traces-v0.📄 279% (2.79x) speedup for
sanitize_query_stringinsrc/backend/base/langflow/services/tracing/validation.py⏱️ Runtime :
40.7 milliseconds→10.7 milliseconds(best of76runs)📝 Explanation and details
The optimized code achieves a 279% speedup (from 40.7ms to 10.7ms) by replacing a character-by-character Python generator expression with a pre-compiled regular expression that operates in optimized C code.
Key Changes:
Pre-compiled regex pattern (
_NON_PRINTABLE_RE): Compiled once at module import, this regex matches sequences of non-printable characters ([^\x20-\x7E]+) and removes them in a single optimized operation. The line profiler shows this drops from 196ms (99.7% of time) to just 12.3ms (94.3% of time).Fast-path for empty strings: The explicit
if value == ""check avoids unnecessary regex work for empty inputs, returningNoneimmediately.Why This Is Faster:
Generator overhead eliminated: The original
"".join(ch for ch in value if " " <= ch <= "~")creates a generator object, iterates character-by-character in Python bytecode, performs a comparison for each character, and builds intermediate string objects. This is pure Python-level work.Regex operates in C: The
re.sub()call hands the entire string to highly optimized C code that scans and filters in a single pass with minimal Python object creation. Regular expression engines are designed for exactly this kind of bulk pattern matching.Reduced memory churn: The generator/join approach creates many intermediate Python objects during iteration. The regex performs the same filtering with far fewer allocations.
Impact on Workloads:
Best for: Strings with many printable characters (typical query strings, URLs, JSON) where the regex can quickly scan and copy valid ranges. Test cases like
test_large_string_within_limitandtest_repeated_pattern_largedemonstrate consistent speedups on realistic inputs.Also benefits: Strings with many non-printable characters to filter (e.g.,
test_large_string_mostly_invalid), as the regex efficiently skips invalid character runs rather than checking each one individually.Minimal overhead added: The empty string fast-path and one-time regex compilation at import add negligible cost while preserving exact behavioral compatibility (all tests pass identically).
This optimization is particularly valuable if
sanitize_query_stringis called frequently in request processing pipelines, as the per-call savings compound across many invocations.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr11689-2026-02-28T02.07.34and push.