From fbef762f7b3fb10eec03deb7772d34460e867a1d Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Tue, 1 Sep 2026 04:07:43 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20list=20itera?= =?UTF-8?q?tion=20with=20vapply?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced `sapply` with `vapply` in `R/llcont.R` when iterating over `mispatts` to find the maximum number of rows. In R codebases, `sapply` involves significant overhead to deduce and simplify the return type dynamically. Using `vapply` with a predefined return type avoids this overhead. --- .jules/bolt.md | 3 +++ R/llcont.R | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index f658475..604ecab 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -15,3 +15,6 @@ ## 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-09-01 - Avoid sapply overhead on lists +**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..ce4ccd2 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 to avoid type deduction overhead + npatts <- max(vapply(mispatts, nrow, numeric(1))) } else { npatts <- nrow(mispatts) } From 85b95b178e920b885c68a7aa3199240c21549475 Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Tue, 1 Sep 2026 07:40:28 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20list=20itera?= =?UTF-8?q?tion=20with=20vapply?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced `sapply` with `vapply` in `R/llcont.R` when iterating over `mispatts` to find the maximum number of rows. In R codebases, `sapply` involves significant overhead to deduce and simplify the return type dynamically. Using `vapply` with a predefined return type avoids this overhead. From 7b9d3a89c14d567d86b23b26bf61e1d4d1ed64eb Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Tue, 1 Sep 2026 11:44:29 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20list=20itera?= =?UTF-8?q?tion=20with=20vapply?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced `sapply` with `vapply` in `R/llcont.R` when iterating over `mispatts` to find the maximum number of rows. In R codebases, `sapply` involves significant overhead to deduce and simplify the return type dynamically. Using `vapply` with a predefined return type avoids this overhead.