diff --git a/.jules/bolt.md b/.jules/bolt.md index 19b4c61..94802e9 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -43,3 +43,6 @@ ## 2025-01-24 - 단일 readAttributes 호출로 파일 속성 조회 최적화 **학습:** `isDirectory`, `!it.isDirectory()`, `isSymbolicLink` 3개의 개별적인 파일 시스템 I/O 호출을 수행하면 성능 저하가 큽니다. 이를 단일 `Files.readAttributes` 호출로 변경하여 메타데이터를 한 번에 조회함으로써 I/O 오버헤드를 대폭 줄일 수 있음을 확인했습니다. **조치:** 디렉토리 순회 시 파일의 여러 속성을 확인할 때는 개별적인 stat 호출보다 `Files.readAttributes`를 사용하여 필요한 모든 속성을 한 번에 가져오는 방식을 우선적으로 고려해야 합니다. +## 2026-07-24 - 루프 내부의 불변값 연산 호이스팅 +**학습:** `process_dir`와 같이 자주 호출되는 함수 내부에서 변하지 않는 정적 문자열 할당이나 무거운 연산(예: SHA-256 해시 계산)을 수행하면 매번 호출 시마다 중복된 메모리 할당 및 처리 비용이 발생합니다. +**조치:** 불변 정적 문자열과 비용이 큰 연산들은 자주 호출되는 함수 바깥(최상위 레벨)으로 호이스팅하여 `private val` 상수로 선언함으로써 불필요한 반복 오버헤드를 제거합니다. Kotlin에서 `private val`을 사용하면 기본적으로 생성되는 public getter 메서드를 방지하여 커버리지(Jacoco) 감소도 막을 수 있습니다. diff --git a/src/main/kotlin/html4tree/main.kt b/src/main/kotlin/html4tree/main.kt index b455862..70f3c90 100644 --- a/src/main/kotlin/html4tree/main.kt +++ b/src/main/kotlin/html4tree/main.kt @@ -240,11 +240,9 @@ fun write_index_file(curr_dir: File, content: String) { } } -fun process_dir(curr_dir: File, excludeSet: Set? = null, dirFiles: Array? = null){ - - val exclude: Set = excludeSet ?: process_ignore_file(curr_dir) - - val cssContent = """ +// ⚡ Bolt Performance Optimization: Hoisted static CSS strings and expensive SHA-256 computation +// to top-level `private val`s to prevent redundant allocations and processing overhead per directory. +private val cssContent = """ body { font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; line-height: 1.5; @@ -310,13 +308,17 @@ fun process_dir(curr_dir: File, excludeSet: Set? = null, dirFiles: Array } """ - val styleHash = "sha256-" + Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-256").digest(cssContent.toByteArray(Charsets.UTF_8))) +private val styleHash = "sha256-" + Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-256").digest(cssContent.toByteArray(Charsets.UTF_8))) - val css = """ +private val css = """ """ +fun process_dir(curr_dir: File, excludeSet: Set? = null, dirFiles: Array? = null){ + + val exclude: Set = excludeSet ?: process_ignore_file(curr_dir) + val index_top = """