Skip to content

⚡ Bolt: Optimize ifelse overhead with vectorized subsetting in llcont.R - #62

Open
seonghobae wants to merge 1 commit into
masterfrom
bolt-optimize-ifelse-13039554124987038705
Open

⚡ Bolt: Optimize ifelse overhead with vectorized subsetting in llcont.R#62
seonghobae wants to merge 1 commit into
masterfrom
bolt-optimize-ifelse-13039554124987038705

Conversation

@seonghobae

Copy link
Copy Markdown
Collaborator

💡 What: Replaced slow ifelse() calls in llcont.glm and llcont.hurdle inside R/llcont.R with preallocation res <- Y * 0 (which preserves attributes and length) and vectorized subsetting res[cond] <- .... Included safety checks for scalar variables using rep_len to 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 (like log(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 llcont functions on large hurdle models or GLMs. Verify correctness via R CMD check or running the testthat suite.


PR created automatically by Jules for task 13039554124987038705 started by @seonghobae

@google-labs-jules

Copy link
Copy Markdown

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings July 27, 2026 04:03
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@seonghobae, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 18 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1dfaa498-5f7f-4ec6-b2c7-78d5681d5a73

📥 Commits

Reviewing files that changed from the base of the PR and between 95cdc9f and 33420ad.

📒 Files selected for processing (2)
  • .jules/bolt.md
  • R/llcont.R
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bolt-optimize-ifelse-13039554124987038705

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.glm binomial handling by replacing ifelse() with preallocated vectors and conditional assignment (including safe handling of scalar weights via rep_len()).
  • Optimized multiple llcont.hurdle internal likelihood components by replacing ifelse() 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.

Comment thread R/llcont.R
Comment on lines +162 to +166
## 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
Comment thread .jules/bolt.md
**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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants