⚡️ Speed up function sort_chat_inputs_first
by 13%
#27
+20
−19
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.
📄 13% (0.13x) speedup for
sort_chat_inputs_first
insrc/dsa/nodes.py
⏱️ Runtime :
648 microseconds
→574 microseconds
(best of219
runs)📝 Explanation and details
Here is a high-performance rewrite of your function incorporating recommendations from the profiler and making the core logic as efficient as possible.
Bottleneck Analysis
From your profiler.
"ChatInput" in vertex_id
(done repeatedly per vertex, often for the same vertex twice)self.get_vertex(vertex_id)
and thenself.get_predecessors(...)
(done for every "ChatInput" in every layer, re-traversing graph data that may not have changed)layer.remove(vertex_id)
(removes items from lists during iteration, which is O(n) and risks skipping elements or doing slow searches)Key Optimizations
list.remove
inside loop: Build new layers without "ChatInput" vertices rather than deleting-in-place, avoiding unnecessary list traversals.Rewritten Code
Optimization Summary
layer.remove
, build new lists.Compatibility
Let me know if you want a further-parallelized or NumPy-based approach, but in this data-shape (lists of IDs, graph methods), this is likely optimal.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-sort_chat_inputs_first-mc8popkq
and push.