Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) 오버헤드를 방지해야 합니다.
## 2026-08-27 - R 언어에서 불필요한 sort()[1] 및 stats::na.omit() 사용을 제거하여 메모리 할당 및 N log N 연산 방지
**Learning:** R에서 최솟값을 가지는 원소의 이름을 찾을 때 `names(sort(x))[1L]`를 사용하면 불필요하게 전체 원소를 O(N log N) 시간으로 정렬하게 되며, 유니크한 원소 개수를 셀 때 `length(stats::na.omit(unique(x)))` 또는 `length(unique(stats::na.omit(x)))`를 사용하면 내부적으로 S3 메서드 디스패치 및 na.action 속성 할당으로 인한 오버헤드가 발생합니다.
**Action:** 최솟값을 찾는 경우 `which.min(x)`를 사용하여 O(N) 선형 탐색으로 최적화하고, 유니크한 NA 제외 원소 개수를 셀 때는 `sum(!is.na(unique(x)))`와 같은 논리 인덱싱의 합계를 사용하여 함수 오버헤드와 불필요한 메모리 할당을 줄여야 합니다.
4 changes: 2 additions & 2 deletions R/aFIPC.R
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,8 @@ autoFIPC <-
if (
!is.na(newFormItemName) &&
!is.na(oldFormItemName) &&
(length(stats::na.omit(unique(newFormModel@Data$data[, newFormItemName]))) ==
length(stats::na.omit(unique(oldFormModel@Data$data[, oldFormItemName]))))
(sum(!is.na(unique(newFormModel@Data$data[, newFormItemName]))) ==
sum(!is.na(unique(oldFormModel@Data$data[, oldFormItemName]))))
Comment on lines +773 to +774

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📝 Info: aFIPC unique-count refactor is numerically equivalent

The switch from length(stats::na.omit(unique(x))) to sum(!is.na(unique(x))) yields the same distinct-non-NA count feeding the common-item equality check, so the guarded numerical behavior in aFIPC is preserved.

Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

) {
message(
'applying ',
Expand Down
4 changes: 2 additions & 2 deletions R/surveyFA.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ surveyFA <- function(
response_data <- as.data.frame(data)
response_data <-
response_data[, vapply(response_data, function(column) {
nunique <- length(unique(stats::na.omit(column)))
nunique <- sum(!is.na(unique(column)))
nunique >= 2L
}, logical(1L))]

Expand Down Expand Up @@ -232,7 +232,7 @@ 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]
candidate <- names(which.min(p_values))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📝 Info: which.min preserves sort()[1L] tie-breaking

In select_bad_item, names(which.min(p_values)) matches the old names(sort(...))[1L]. NAs are pre-replaced with 1, and sort on a named vector uses stable order, so ties resolve to the first occurrence just as which.min does.

Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

if (!is.na(candidate) && p_values[[candidate]] < pThreshold) {
return(candidate)
}
Expand Down
Loading