-
Notifications
You must be signed in to change notification settings - Fork 8.2k
⚡️ Speed up method TransactionLogsResponse.serialize_inputs by 14% in PR #10820 (cz/add-logs-feature)
#11173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
⚡️ Speed up method TransactionLogsResponse.serialize_inputs by 14% in PR #10820 (cz/add-logs-feature)
#11173
Conversation
… into cz/add-logs-feature
… into cz/add-logs-feature
The optimized code achieves a **14% speedup** (from 4.58ms to 4.01ms) through strategic short-circuit optimizations in frequently-called serialization paths:
## Key Optimizations
### 1. **Fast-path for primitives in `serialize()`**
The optimized version adds an early exit for common primitive types before expensive dispatcher logic:
```python
if obj is None or isinstance(obj, (str, int, float, bool)):
return obj
```
This avoids calling `_serialize_dispatcher()` for the most common data types. Since serialization often processes nested dictionaries containing many primitive values, this check eliminates significant overhead.
### 2. **Reordered checks in `sanitize_data()`**
The original checks `if data is None` first, then `if not isinstance(data, dict)`. The optimized version reverses this:
```python
if not isinstance(data, dict):
return data
if data is None:
return None
```
Since `None` is a valid non-dict type that would be caught by the `isinstance` check anyway, checking for dict-ness first is more efficient. This also adds an early return for empty dicts (`if not data: return {}`), avoiding unnecessary calls to `_sanitize_dict({})`.
## Why This Matters
Based on the test suite, the code frequently serializes:
- **Large nested structures** with many primitive values (strings, ints, bools)
- **Lists of dictionaries** containing both sensitive and non-sensitive data
- **Mixed-type data** with primitives alongside complex objects
The primitive fast-path optimization is particularly effective here because:
- Every nested dict/list traversal hits multiple primitive values
- Tests like `test_serialize_inputs_large_list_of_sensitive_dicts` (100 items) and `test_serialize_inputs_performance_large` (500 users) show the multiplicative benefit of avoiding dispatcher overhead on each primitive
The `sanitize_data()` optimization helps in edge cases with empty dicts or `None` values, providing small but consistent gains across the test suite.
## Impact Assessment
The 14% speedup compounds when `serialize_inputs()` is called repeatedly in transaction logging workflows. Since this appears to be a database model for transaction logs, these functions likely execute in high-volume scenarios where even microsecond improvements per call translate to meaningful latency reductions at scale.
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Comment |
Codecov Report❌ Patch coverage is ❌ Your project status has failed because the head coverage (39.50%) 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 #11173 +/- ##
==========================================
+ Coverage 33.23% 33.33% +0.10%
==========================================
Files 1394 1399 +5
Lines 66068 66222 +154
Branches 9778 9785 +7
==========================================
+ Hits 21956 22076 +120
- Misses 42986 43021 +35
+ Partials 1126 1125 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
⚡️ This pull request contains optimizations for PR #10820
If you approve this dependent PR, these changes will be merged into the original PR branch
cz/add-logs-feature.📄 14% (0.14x) speedup for
TransactionLogsResponse.serialize_inputsinsrc/backend/base/langflow/services/database/models/transactions/model.py⏱️ Runtime :
4.58 milliseconds→4.01 milliseconds(best of51runs)📝 Explanation and details
The optimized code achieves a 14% speedup (from 4.58ms to 4.01ms) through strategic short-circuit optimizations in frequently-called serialization paths:
Key Optimizations
1. Fast-path for primitives in
serialize()The optimized version adds an early exit for common primitive types before expensive dispatcher logic:
This avoids calling
_serialize_dispatcher()for the most common data types. Since serialization often processes nested dictionaries containing many primitive values, this check eliminates significant overhead.2. Reordered checks in
sanitize_data()The original checks
if data is Nonefirst, thenif not isinstance(data, dict). The optimized version reverses this:Since
Noneis a valid non-dict type that would be caught by theisinstancecheck anyway, checking for dict-ness first is more efficient. This also adds an early return for empty dicts (if not data: return {}), avoiding unnecessary calls to_sanitize_dict({}).Why This Matters
Based on the test suite, the code frequently serializes:
The primitive fast-path optimization is particularly effective here because:
test_serialize_inputs_large_list_of_sensitive_dicts(100 items) andtest_serialize_inputs_performance_large(500 users) show the multiplicative benefit of avoiding dispatcher overhead on each primitiveThe
sanitize_data()optimization helps in edge cases with empty dicts orNonevalues, providing small but consistent gains across the test suite.Impact Assessment
The 14% speedup compounds when
serialize_inputs()is called repeatedly in transaction logging workflows. Since this appears to be a database model for transaction logs, these functions likely execute in high-volume scenarios where even microsecond improvements per call translate to meaningful latency reductions at scale.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr10820-2025-12-30T19.18.44and push.