diff --git a/.jules/bolt.md b/.jules/bolt.md index f658475..82a341a 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -15,3 +15,10 @@ ## 2024-05-15 - [R Performance: ifelse Overhead] **Learning:** In R, ifelse evaluates both true and false branches entirely before subsetting, which is very inefficient for vector operations. **Action:** Optimize this by preallocating with res <- Y * 0 to preserve attributes and using vectorized subsetting like if any cond res subset <- ... +## 2026-08-11 - [R Performance: ifelse vs Readability] +**Learning:** In R, replacing `ifelse()` with vectorized subsetting (and its required `NA` handling and scalar recycling) can severely degrade code readability, turning a 1-liner into 7 lines. +**Action:** Avoid this micro-optimization unless the specific `ifelse()` call is a proven, high-impact performance bottleneck. + +## 2026-08-11 - [R Performance: vapply over sapply] +**Learning:** In R codebases, using `sapply` over a list involves significant overhead to deduce and simplify the return type. +**Action:** When the return type and length are known, prefer `vapply(..., FUN.VALUE = type)` over `sapply(...)` for better performance and safety. diff --git a/R/llcont.R b/R/llcont.R index d8e496a..b74269b 100644 --- a/R/llcont.R +++ b/R/llcont.R @@ -407,7 +407,7 @@ llcont.lavaan <- function(x, ...){ if(tolower(lavInspect(x, "options")$missing) == "ml.x") stop("cannot handle lavaan models with missing='ml.x'. consider using missing='ml'.", call. = FALSE) mispatts <- lavInspect(x, "patterns") if(any(class(mispatts) == "list")){ - npatts <- max(sapply(mispatts, nrow)) + npatts <- max(vapply(mispatts, nrow, numeric(1))) } else { npatts <- nrow(mispatts) } diff --git a/benchmark_hurdle_ifelse.R b/benchmark_hurdle_ifelse.R deleted file mode 100644 index eda1283..0000000 --- a/benchmark_hurdle_ifelse.R +++ /dev/null @@ -1,48 +0,0 @@ -# Reproducible benchmark harness -# Install the optional benchmark dependency with: -# install.packages("microbenchmark") -library(microbenchmark) - -run_zeroPoisson_orig <- function(Z, parms, offsetz, weights, Y0, Y1) { - mu <- as.vector(exp(Z %*% parms + offsetz)) - loglik0 <- -mu - Y0 * weights * loglik0 + ifelse(Y1, weights * log(1 - exp(loglik0)), 0) -} - -run_zeroPoisson_opt <- function(Z, parms, offsetz, weights, Y0, Y1) { - mu <- as.vector(exp(Z %*% parms + offsetz)) - loglik0 <- -mu - res_Y1 <- Y1 * 0 - cond <- Y1; cond[is.na(cond)] <- FALSE - if (any(cond)) { - w_c <- if (length(weights) == 1) rep_len(weights, sum(cond)) else weights[cond] - res_Y1[cond] <- w_c * log(1 - exp(loglik0[cond])) - } - Y0 * weights * loglik0 + res_Y1 -} - -# Generate mostly zeros (so Y1 is mostly FALSE) -set.seed(20260811) -n <- 1000000 -Z <- matrix(rnorm(n*2), n, 2) -parms <- c(0.5, -0.5) -offsetz <- rep(0, n) -Y <- rbinom(n, 1, 0.1) -Y0 <- Y <= 0 -Y1 <- Y > 0 -weights <- 1 - -bm <- microbenchmark( - original = run_zeroPoisson_orig(Z, parms, offsetz, weights, Y0, Y1), - optimized = run_zeroPoisson_opt(Z, parms, offsetz, weights, Y0, Y1), - times = 100, - control = list(warmup = 10) -) -print(bm) - -med_orig <- median(bm$time[bm$expr == "original"]) -med_opt <- median(bm$time[bm$expr == "optimized"]) -improvement <- (med_orig - med_opt) / med_orig - -cat(sprintf("Performance improvement: %.2f%%\n", improvement * 100)) -cat("Timing is descriptive; compare thresholds only in a controlled environment.\n")