⚡️ Speed up function _lis_inner_body_jax by 2,485%
#1076
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.
📄 2,485% (24.85x) speedup for
_lis_inner_body_jaxincode_to_optimize/sample_code.py⏱️ Runtime :
822 milliseconds→31.8 milliseconds(best of42runs)📝 Explanation and details
The optimized code achieves a 2484% speedup (from 822ms to 31.8ms) by adding JAX's
@jitdecorator to enable Just-In-Time compilation of the function.Key Optimization
JIT Compilation via
@jitdecorator: The function performs array indexing, comparison operations, and conditional updates using JAX operations (jnp.where,.at[].set()). Without JIT, each of these operations is executed separately in Python with overhead for:arr[j],arr[i],dp_inner[j],dp_inner[i])<,&,>)jnp.where)dp_inner.at[i].set())With
@jit, JAX traces the function once and compiles it into optimized XLA code that:Why This Works
The function is a perfect candidate for JIT compilation because:
Test Case Analysis
The speedup is consistent across all test scenarios:
The optimization benefits any workload that calls this function repeatedly, particularly dynamic programming algorithms (like Longest Increasing Subsequence) where this inner body function would be invoked hundreds or thousands of times.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-_lis_inner_body_jax-mkgiirxuand push.