⚡️ Speed up method bitmart.parse_deposit_withdraw_fee by 29%
#78
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.
📄 29% (0.29x) speedup for
bitmart.parse_deposit_withdraw_feeinpython/ccxt/async_support/bitmart.py⏱️ Runtime :
1.10 milliseconds→858 microseconds(best of44runs)📝 Explanation and details
The optimization focuses on improving the
safe_numbermethod in the Exchange base class, which is a critical hot path called frequently during fee parsing operations.Key Optimization: Fast-path dictionary access
The original
safe_numbermethod always calledself.safe_string(obj, key)regardless of input type, which adds method call overhead. The optimized version adds a fast path for the most common case - whenobjis a dictionary and the key exists:Why this optimization works:
obj[key]is faster than callingself.safe_string(obj, key)which has function call overhead and additional type checkingsafe_stringmethod for non-dict objects or missing keysPerformance Impact:
The line profiler shows the optimization reduces
safe_numberexecution time from 2.97ms to 2.11ms (28% faster). Test results demonstrate consistent 20-40% speedups across most test cases where the fee exists in the dictionary, with particularly strong gains (36-59% faster) for standard numeric fee values.Cases with missing keys show slight slowdowns (5-12%) because they now do an extra
isinstancecheck before falling back to the original path, but this is a reasonable trade-off given the substantial gains for the common case.This optimization is especially valuable since
safe_numberis called in hot paths during fee parsing operations across many exchange implementations in the CCXT library.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-bitmart.parse_deposit_withdraw_fee-mhyzmbq6and push.