From 1bc09f6e29661c1ce8b78a0482091e3fe22efafd Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Mon, 17 Aug 2026 04:03:37 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20maximum=20matrix?= =?UTF-8?q?=20rows=20calculation=20with=20vapply?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced max(sapply(mispatts, nrow)) with max(vapply(mispatts, nrow, numeric(1))) to reduce execution overhead. --- .jules/bolt.md | 4 ++++ R/llcont.R | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index f658475..41d25ed 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -15,3 +15,7 @@ ## 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 <- ... + +## 2024-08-17 - Optimize sapply overhead with vapply in R +**Learning:** 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..4ea5d74 100644 --- a/R/llcont.R +++ b/R/llcont.R @@ -407,7 +407,8 @@ 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)) + ## Bolt: replaced sapply with vapply for performance + npatts <- max(vapply(mispatts, nrow, numeric(1))) } else { npatts <- nrow(mispatts) }