diff --git a/.Rbuildignore b/.Rbuildignore index 232504f..5a76495 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -22,3 +22,5 @@ ^\.jules(/.*)?$ ^\.trivyignore\.yaml$ ^trivy\.yaml$ +^\.semgrepignore$ +^\.Jules(/.*)?$ diff --git a/.jules/bolt.md b/.jules/bolt.md index 7d3c603..b028326 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -16,3 +16,6 @@ ## 2025-02-12 - R 언어에서 반복적인 mirt 모델 생성 시 불필요한 데이터프레임 부분집합 추출 최적화 **Learning:** R에서 데이터프레임의 특정 열을 추출하는 작업(`df[cols]`)은 O(N)의 메모리 복사를 수반합니다. `autoFIPC`에서 `mirt` 모델의 파라미터를 설정하거나 호출하는 과정 중에 `newformXDataK[colnames(newFormModel@Data$data)]` 코드가 반복해서 사용되었고, 심지어 `ncol()`을 위해 단순히 개수를 구할 때도 사용되어 불필요한 메모리 할당과 오버헤드를 초래했습니다. **Action:** 조건문이나 반복문 내부에서 불필요하게 데이터프레임 부분집합 연산이 반복되지 않도록 외부에서 한 번만 `linkedFormData <- newformXDataK[colnames(newFormModel@Data$data)]`로 캐싱(caching)한 뒤, `ncol(linkedFormData)`와 `data = linkedFormData` 형태로 재사용하여 메모리 복사와 O(N) 오버헤드를 방지해야 합니다. +## 2024-08-01 - Avoid sort() overhead for finding minimum element +**Learning:** In R, using `sort(x)[1]` or `names(sort(x))[1]` to find the minimum (or maximum) element incurs unnecessary O(N log N) overhead, compared to using `which.min()` (or `which.max()`) which runs in O(N) linear time. +**Action:** When searching for the minimum or maximum value or its index/name, prefer using `which.min(x)` or `which.max(x)` instead of sorting the entire vector, especially inside iterative/looping structures like model fitting fallbacks where overhead compounds. diff --git a/R/surveyFA.R b/R/surveyFA.R index f60fffd..c1dcfa5 100644 --- a/R/surveyFA.R +++ b/R/surveyFA.R @@ -232,7 +232,8 @@ surveyFA <- function( names(p_values) <- rownames(fit_df) if (any(!is.na(p_values))) { p_values[is.na(p_values)] <- 1 - candidate <- names(sort(p_values, decreasing = FALSE))[1L] + # ⚡ Bolt: Use which.min() for O(N) linear time lookup instead of O(N log N) sort() overhead + candidate <- names(which.min(p_values)) if (!is.na(candidate) && p_values[[candidate]] < pThreshold) { return(candidate) } diff --git a/test_dummy.R b/test_dummy.R deleted file mode 100644 index e6f7019..0000000 --- a/test_dummy.R +++ /dev/null @@ -1,2 +0,0 @@ -source("R/aFIPC.R") -source("R/surveyFA.R") diff --git a/test_validation.R b/test_validation.R deleted file mode 100644 index f084116..0000000 --- a/test_validation.R +++ /dev/null @@ -1,3 +0,0 @@ -source("R/aFIPC.R") -source("R/surveyFA.R") -print("Syntax check passed") diff --git a/tests/testthat/test-surveyFA.R b/tests/testthat/test-surveyFA.R index 060ae68..e1cc146 100644 --- a/tests/testthat/test-surveyFA.R +++ b/tests/testthat/test-surveyFA.R @@ -82,3 +82,85 @@ test_that("surveyFA reports bounded recovery exhaustion when unrecoverable", { "could not estimate a valid model after bounded recovery attempts" ) }) + +test_that("surveyFA properly triggers fallback when autofix is disabled and covers item removal", { + skip_if_not_installed("mirt") + set.seed(20260702) + + # Intentionally messy data to force failure on standard methods + raw <- as.data.frame( + matrix( + c(rep(1, 20), rep(0, 20), rbinom(160, 1, 0.5)), + ncol = 5 + ) + ) + names(raw) <- paste0("item", 1:5) + raw$item6 <- 0 # Constant column + + # Force failure without autofix + expect_error( + suppressWarnings( + aFIPC::surveyFA( + data = raw, + autofix = FALSE, + forceUIRT = TRUE, + forceNormalEM = FALSE, + forceMHRM = TRUE, # Cover forceMHRM branch + unstable = FALSE, + SE = TRUE, + itemtype = "2PL", + maxItemRemovals = 2 + ) + ), + "could not estimate a valid model after bounded recovery attempts" + ) + + # Trigger unstable branch and force NormalEM=FALSE + expect_error( + suppressWarnings( + aFIPC::surveyFA( + data = raw, + autofix = TRUE, + forceUIRT = TRUE, + forceNormalEM = FALSE, + forceMHRM = FALSE, + unstable = FALSE, + SE = TRUE, + itemtype = "2PL", + maxItemRemovals = 2 + ) + ), + "could not estimate a valid model after bounded recovery attempts" + ) + + # Trigger legacy forceUIRT warning + expect_error( + aFIPC::surveyFA(data=raw, forceUIRT = FALSE), + "surveyFA requires forceUIRT=TRUE" + ) + + # Check invalid itemtype + expect_error( + aFIPC::surveyFA(data=raw, itemtype = c("2PL", "3PL")), + "surveyFA requires itemtype to be a single non-NA character value" + ) + + # Check invalid maxItemRemovals + expect_error( + aFIPC::surveyFA(data=raw, maxItemRemovals = -1), + "surveyFA requires maxItemRemovals to be a non-negative numeric scalar" + ) + + # Check invalid pThreshold + expect_error( + aFIPC::surveyFA(data=raw, pThreshold = 1.5), + "surveyFA requires pThreshold to be in \\(0, 1\\]" + ) + + # Check insufficient non-constant columns + bad_raw <- data.frame(item1 = rep(1, 10), item2 = rep(2, 10)) + expect_error( + aFIPC::surveyFA(bad_raw, forceUIRT=TRUE), + "surveyFA needs at least two non-constant response columns" + ) +})