⚡ Bolt: Optimize ifelse overhead with vectorized subsetting in llcont.R - #62
⚡ Bolt: Optimize ifelse overhead with vectorized subsetting in llcont.R#62seonghobae wants to merge 1 commit into
Conversation
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Warning Review limit reached
Next review available in: 18 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Pull request overview
This PR optimizes per-observation log-likelihood computations in the R package by replacing ifelse()-based branching with preallocation plus vectorized logical subsetting in llcont() methods, aiming to reduce unnecessary evaluation and memory overhead during repeated likelihood evaluations.
Changes:
- Optimized
llcont.glmbinomial handling by replacingifelse()with preallocated vectors and conditional assignment (including safe handling of scalar weights viarep_len()). - Optimized multiple
llcont.hurdleinternal likelihood components by replacingifelse()with preallocation plus subsetting to avoid computing the unused branch. - Added a Bolt knowledge-base entry documenting the
ifelse()optimization pattern.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| R/llcont.R | Replaces ifelse() in llcont.glm and llcont.hurdle with preallocation + vectorized subsetting to reduce overhead and avoid evaluating unused branches. |
| .jules/bolt.md | Documents the optimization rationale/pattern for future reference. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ## Bolt: optimized ifelse with vectorized subsetting | ||
| res <- Y1 * 0 | ||
| w <- if(length(weights) == 1) rep_len(weights, length(Y1)) else weights | ||
| res[Y1] <- w[Y1] * log(1 - exp(loglik0[Y1])) | ||
| Y0 * weights * loglik0 + res |
| **Learning:** In R, matrix multiplication of the form `t(X) %*% Y` explicitly allocates memory for the transposed matrix. Using the optimized base function `crossprod(X, Y)` avoids this allocation. | ||
| **Action:** Always replace `t(X) %*% Y` with `crossprod(X, Y)` for faster and more memory-efficient cross-product calculations. | ||
|
|
||
| ## 2024-07-27 - Optimized ifelse with vectorized subsetting in R |
💡 What: Replaced slow
ifelse()calls inllcont.glmandllcont.hurdleinsideR/llcont.Rwith preallocationres <- Y * 0(which preserves attributes and length) and vectorized subsettingres[cond] <- .... Included safety checks for scalar variables usingrep_lento avoid vector recycling bugs.🎯 Why:
ifelse()evaluates both true and false branches entirely before subsetting, causing inefficient memory allocation and evaluation overhead for mathematical operations (likelog(1 - exp(...))), which can also produce warnings/NaNs on the false branch.📊 Impact: Significantly reduces function call overhead and execution time during log-likelihood calculations, especially for large datasets or during optimizations involving repeated calls.
🔬 Measurement: Observe reduction in execution time for
llcontfunctions on large hurdle models or GLMs. Verify correctness viaR CMD checkor running thetestthatsuite.PR created automatically by Jules for task 13039554124987038705 started by @seonghobae