Skip to content
Merged
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 @@ -13,3 +13,6 @@
## 2024-07-12 - R 언어에서 데이터프레임 서브셋팅 시 불필요한 which() 및 반복 평가 제거
**Learning:** 데이터 프레임의 특정 로우(row)를 변경할 때 `df[which(df$col == "val"), ]`와 같이 `which()`를 사용하면 내부적으로 추가 함수 호출 및 논리 벡터 평가 오버헤드가 발생합니다. 또한, 여러 값을 업데이트하기 위해 동일한 조건식을 연속으로 사용하면 매번 동일한 O(N) 논리 벡터 평가가 중복해서 일어납니다. 불필요한 `paste0("GROUP")` 호출도 오버헤드를 더합니다.
**Action:** `which()`를 생략하고 직접 논리 인덱싱(`df$col == "val"`)을 사용하며, 동일한 조건식을 두 번 이상 연속으로 사용할 경우 해당 논리 벡터를 변수에 캐싱(`idx <- df$col == "val"`)하여 여러 번 재사용함으로써 중복된 O(N) 선형 스캔을 피하고 성능을 최적화해야 합니다. 또한 불필요한 문자열 연산을 제거합니다.
## 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) 오버헤드를 방지해야 합니다.
1 change: 1 addition & 0 deletions .semgrepignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packrat/**
20 changes: 12 additions & 8 deletions R/aFIPC.R
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,10 @@ autoFIPC <-
}

message('\nestimating Linked Form Eq(X) parameters')

# ⚡ Bolt: Cache subsetted dataframe to avoid repeated O(N) memory copies during mirt model setup
linkedFormData <- newformXDataK[colnames(newFormModel@Data$data)]

if (forceNormalZeroOne) {
freeMEAN <- F

Expand All @@ -866,7 +870,7 @@ autoFIPC <-
LinkedModelSyntax <-
mirt::mirt.model(paste0(
'F1 = 1-',
ncol(newformXDataK[colnames(newFormModel@Data$data)]),
ncol(linkedFormData),
'\n',
'MEAN = F1'
))
Expand All @@ -877,7 +881,7 @@ autoFIPC <-
LinkedModelSyntax <-
mirt::mirt.model(paste0(
'F1 = 1-',
ncol(newformXDataK[colnames(newFormModel@Data$data)]),
ncol(linkedFormData),
'\n'
))
}
Expand All @@ -897,7 +901,7 @@ autoFIPC <-

LinkedModel <-
mirt::mirt(
data = newformXDataK[colnames(newFormModel@Data$data)],
data = linkedFormData,
LinkedModelSyntax,
itemtype = newFormModel@Model$itemtype,
method = 'EM',
Expand All @@ -917,7 +921,7 @@ autoFIPC <-
} else {
LinkedModel <-
mirt::mirt(
data = newformXDataK[colnames(newFormModel@Data$data)],
data = linkedFormData,
LinkedModelSyntax,
itemtype = newFormModel@Model$itemtype,
method = 'EM',
Expand All @@ -944,7 +948,7 @@ autoFIPC <-
# LinkedModel <- oldFormModel
LinkedModel <-
mirt::mirt(
data = newformXDataK[colnames(newFormModel@Data$data)],
data = linkedFormData,
LinkedModelSyntax,
itemtype = newFormModel@Model$itemtype,
method = 'MHRM',
Expand All @@ -964,7 +968,7 @@ autoFIPC <-
} else {
LinkedModel <-
mirt::mirt(
data = newformXDataK[colnames(newFormModel@Data$data)],
data = linkedFormData,
LinkedModelSyntax,
itemtype = newFormModel@Model$itemtype,
method = 'MHRM',
Expand All @@ -988,15 +992,15 @@ autoFIPC <-
# message('Estimation failed. estimating new parameters with no prior distribution using quasi-Monte Carlo EM estimation. please be patient.')
#
# rm(LinkedModel)
# try(LinkedModel <- mirt::mirt(data = newformXDataK[colnames(newFormModel@Data$data)], LinkedModelSyntax, itemtype = newFormModel@Model$itemtype, SE = T, method = 'QMCEM', accelerate = 'squarem', technical = list(NCYCLES = 1e+5), pars = NewScaleParms, GenRandomPars = F))
# try(LinkedModel <- mirt::mirt(data = linkedFormData, LinkedModelSyntax, itemtype = newFormModel@Model$itemtype, SE = T, method = 'QMCEM', accelerate = 'squarem', technical = list(NCYCLES = 1e+5), pars = NewScaleParms, GenRandomPars = F))
# }
#
# if(!LinkedModel@OptimInfo$secondordertest){
# message('Estimation failed. estimating new parameters with no prior distribution using Cai\'s (2010) Metropolis-Hastings Robbins-Monro (MHRM) algorithm. please be patient.')
#
# try(rm(LinkedModel), silent = TRUE)
# for (attempt in seq_len(3)) {
# try(LinkedModel <- mirt::mirt(data = newformXDataK[colnames(newFormModel@Data$data)], LinkedModelSyntax, itemtype = newFormModel@Model$itemtype, SE = T, method = 'MHRM', accelerate = 'squarem', technical = list(NCYCLES = 1e+5, MHRM_SE_draws = 200000), pars = NewScaleParms, GenRandomPars = T))
# try(LinkedModel <- mirt::mirt(data = linkedFormData, LinkedModelSyntax, itemtype = newFormModel@Model$itemtype, SE = T, method = 'MHRM', accelerate = 'squarem', technical = list(NCYCLES = 1e+5, MHRM_SE_draws = 200000), pars = NewScaleParms, GenRandomPars = T))
# if (exists('LinkedModel')) break
# }
# if (!exists('LinkedModel')) stop('Failed to estimate LinkedModel with MHRM after 3 attempts')
Expand Down
Loading