⚡️ Speed up function _prune_literal_if_trivial by 22%
#17
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.
📄 22% (0.22x) speedup for
_prune_literal_if_trivialinsrc/uberjob/_transformations/pruning.py⏱️ Runtime :
5.62 milliseconds→4.59 milliseconds(best of34runs)📝 Explanation and details
The optimization achieves a 22% speedup by implementing three key improvements:
1. Early-exit iteration over out-edges: Instead of using
all()which must evaluate every edge even when the first non-Dependency is found, the optimized version uses a simpleforloop that returns immediately upon finding the first non-Dependency edge. The line profiler shows this reduces time spent on edge checking from 75.6% to 80.7% of total time, but with much faster execution overall.2. Early return for trivial cases: Added explicit checks for
m == 0 or n == 0before the cost comparison, avoiding unnecessary computation when there are no predecessors or successors. This optimization particularly benefits test cases with isolated nodes or nodes with only incoming/outgoing edges.3. Direct nested loops instead of itertools.product(): Replaced
itertools.product(predecessors, successors)with simple nestedforloops. For the typical small-to-moderate graph sizes seen in the tests, direct iteration is faster than the generator overhead ofitertools.product().Impact on workloads: Since
_prune_literal_if_trivialis called within a loop over all literals inprune_plan(), this 22% improvement compounds significantly during graph optimization phases. The test results show the optimization is particularly effective for:The optimization maintains identical behavior while reducing algorithmic overhead, making graph pruning operations substantially faster across diverse graph structures.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
test_pytest_teststest_plan_py_teststest_render_py_teststest_scheduler_py_teststest_unpack_py__replay_test_0.py::test_uberjob__transformations_pruning__prune_literal_if_trivialtest_pytest_teststest_registry_py_teststest_version_py_teststest_progress_py_teststest_atoms_py__replay_test_0.py::test_uberjob__transformations_pruning__prune_literal_if_trivialTo edit these changes
git checkout codeflash/optimize-_prune_literal_if_trivial-mi6qok6land push.