⚡️ Speed up function get_pandas_reader by 16%
#454
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.
📄 16% (0.16x) speedup for
get_pandas_readerinnvflare/app_opt/sklearn/data_loader.py⏱️ Runtime :
29.6 milliseconds→25.6 milliseconds(best of35runs)📝 Explanation and details
The optimization achieves a 15% speedup by eliminating expensive function call chains and import overhead.
Key optimizations:
Removed function call indirection: The original code had a 3-function chain (
get_pandas_reader→get_file_format→get_file_ext+get_ext_format). The optimized version inlines all file extension parsing logic directly inget_pandas_reader, eliminating two function calls per invocation.Direct pathlib usage: Instead of calling helper functions that wrap
pathlib.Path(input_path).suffix, the optimized code calls this directly, avoiding function call overhead. The line profiler shows the originalget_file_formattook 90.7% of its time inget_file_ext().Localized pd_readers dictionary: Moving the
pd_readersdictionary inside the function eliminates global variable lookups and allows the Python interpreter to optimize local variable access.Reduced import overhead: The original code imported from
nvflare.app_common.utils.file_utilson every call. The optimized version uses direct imports ofpathlibandpandas, which are more efficient.Performance benefits by test case type:
The optimization is most effective for workloads with frequent file format detection, where the eliminated function call overhead compounds significantly.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_pandas_reader-mhcszf46and push.