Open
Conversation
This optimization achieves a **43% runtime improvement** (from 8.25ms to 5.73ms) by adding a fast-path for immutable built-in types before the expensive `copy.deepcopy()` fallback. **Key optimization**: A new check `if isinstance(obj, (str, bytes, int, float, bool, type(None), complex))` returns these immutable types directly, bypassing `copy.deepcopy()`. Since these types are immutable, they don't need defensive copying—returning them directly is both safe and significantly faster. **Why this matters**: - `copy.deepcopy()` involves introspection, recursion guards, memo dictionaries, and type-specific copying logic—expensive overhead for simple immutables - Test results show dramatic speedups when processing large collections of primitives: - 500-element list of integers: **73.1% faster** (528μs → 305μs) - 500-key dict with string values: **81.3% faster** (1.03ms → 569μs) - 300-element list of dicts: **47.7% faster** (1.55ms → 1.05ms) **Impact on hot paths**: Based on `function_references`, `asdict()` is called in critical paths like `push_to_hub()` and `save_to_disk()` when serializing `DatasetInfo` objects to JSON metadata. These operations happen during dataset publishing/saving workflows, where datasets often contain many primitive-typed fields (integers for sizes, strings for names, booleans for flags). The optimization directly reduces overhead in these serialization hot paths. **Trade-offs**: The optimization adds a tiny overhead (~3-5% slower) for rare edge cases involving mutable objects requiring deepcopy (like sets), but this is negligible compared to the massive gains for common primitive-heavy workloads that dominate real-world usage.
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.
📄 44% (0.44x) speedup for
asdictinsrc/datasets/utils/py_utils.py⏱️ Runtime :
8.25 milliseconds→5.73 milliseconds(best of5runs)📝 Explanation and details
This optimization achieves a 43% runtime improvement (from 8.25ms to 5.73ms) by adding a fast-path for immutable built-in types before the expensive
copy.deepcopy()fallback.Key optimization: A new check
if isinstance(obj, (str, bytes, int, float, bool, type(None), complex))returns these immutable types directly, bypassingcopy.deepcopy(). Since these types are immutable, they don't need defensive copying—returning them directly is both safe and significantly faster.Why this matters:
copy.deepcopy()involves introspection, recursion guards, memo dictionaries, and type-specific copying logic—expensive overhead for simple immutablesImpact on hot paths: Based on
function_references,asdict()is called in critical paths likepush_to_hub()andsave_to_disk()when serializingDatasetInfoobjects to JSON metadata. These operations happen during dataset publishing/saving workflows, where datasets often contain many primitive-typed fields (integers for sizes, strings for names, booleans for flags). The optimization directly reduces overhead in these serialization hot paths.Trade-offs: The optimization adds a tiny overhead (~3-5% slower) for rare edge cases involving mutable objects requiring deepcopy (like sets), but this is negligible compared to the massive gains for common primitive-heavy workloads that dominate real-world usage.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
features/test_features.py::FeaturesTest.test_class_label_feature_with_no_labelsfeatures/test_features.py::FeaturesTest.test_feature_named_self_as_kwargfeatures/test_features.py::FeaturesTest.test_feature_named_typefeatures/test_features.py::test_class_label_to_and_from_dicttest_info.py::test_dataset_info_from_dict_with_large_listtest_py_utils.py::test_asdicttest_splits.py::test_split_dict_asdict_has_dataset_name🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-asdict-mlchvwu3and push.