From d58eb8b270dbaa27dbd7a74059dbe4959d9bddcf Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Thu, 16 Jul 2026 21:11:16 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[=EC=84=B1=EB=8A=A5=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0]=20=EC=A0=95=EC=A0=81=20CSS=20=EB=B0=8F=20?= =?UTF-8?q?=ED=95=B4=EC=8B=9C=20=EA=B3=84=EC=82=B0=20=EC=BA=90=EC=8B=B1=20?= =?UTF-8?q?(=EB=94=94=EB=A0=89=ED=86=A0=EB=A6=AC=20=EB=B0=98=EB=B3=B5?= =?UTF-8?q?=EB=AC=B8=20=EC=B5=9C=EC=A0=81=ED=99=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `html4tree/main.kt`의 `process_dir` 함수가 재귀적으로 호출될 때마다 크기가 큰 CSS 문자열(`cssContent`)을 할당하고, 매번 SHA-256 해시(`styleHash`) 연산을 수행하던 병목을 제거했습니다. - 불변인 CSS 내용과 해시 값을 `private object StaticHtmlResources`로 추출하여, JVM 애플리케이션 시작 시 한 번만 계산되도록 최적화했습니다. - 이를 통해 각 디렉토리를 처리할 때마다 발생하던 불필요한 메모리 할당(GC 압력)과 CPU 연산 오버헤드를 크게 줄였습니다. --- .jules/bolt.md | 3 + src/main/kotlin/html4tree/main.kt | 153 +++++++++++++++--------------- 2 files changed, 81 insertions(+), 75 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 19b4c61..bcdce03 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`를 사용하여 필요한 모든 속성을 한 번에 가져오는 방식을 우선적으로 고려해야 합니다. +## 2024-05-18 - Extracting Static Strings and Hashes in Kotlin Loops +**Learning:** When optimizing performance in directory processing loops (like `process_dir`), extracting large constant strings (e.g., static CSS) and deterministic calculations (e.g., SHA-256 hashing) into a `private object` using `@JvmField` prevents redundant allocations and CPU overhead per directory iteration. +**Action:** Always look for static strings and hash calculations inside loops and extract them to objects or companion objects to ensure they are computed exactly once per application run. diff --git a/src/main/kotlin/html4tree/main.kt b/src/main/kotlin/html4tree/main.kt index b455862..0a8cdd9 100644 --- a/src/main/kotlin/html4tree/main.kt +++ b/src/main/kotlin/html4tree/main.kt @@ -13,6 +13,82 @@ import com.github.ajalt.clikt.parameters.options.default import com.github.ajalt.clikt.parameters.arguments.argument import com.github.ajalt.clikt.parameters.types.int +private object StaticHtmlResources { + const val CSS_CONTENT = """ + body { + font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; + line-height: 1.5; + padding: 1rem; + color: #1f2328; + } + main { + max-width: 800px; + margin: 0 auto; + } + ul { + list-style-type: none; + padding-left: 0; + } + a.dir-link { + display: flex; + align-items: flex-start; + gap: 0.5rem; + width: 100%; + overflow-wrap: anywhere; + box-sizing: border-box; + } + .icon { + flex-shrink: 0; + width: 1.25rem; + text-align: center; + } + a { + padding: 0.5rem; + text-decoration: none; + color: #0969da; + border-radius: 4px; + transition: background-color 0.2s ease, outline-color 0.2s ease; + } + a:hover, a:focus-visible { + background-color: #f6f8fa; + text-decoration: underline; + outline: 2px solid #0969da; + outline-offset: -2px; + } + @media (prefers-reduced-motion: reduce) { + a { + transition: none; + } + } + @media (prefers-color-scheme: dark) { + body { + background-color: #0d1117; + color: #c9d1d9; + } + a { + color: #58a6ff; + } + a:hover, a:focus-visible { + background-color: #161b22; + outline-color: #58a6ff; + } + } + .empty-dir { + padding: 0.5rem; + opacity: 0.7; + font-style: italic; + } + """ + + @JvmField + val STYLE_HASH = "sha256-" + Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-256").digest(CSS_CONTENT.toByteArray(Charsets.UTF_8))) + + val CSS = """ + + """ +} + class Html4tree : CliktCommand() { val maxLevel:Int by option(help="Number of levels deep for which to generate an index.html file", hidden = false).int().default(-1) val topDir: String by argument(help="Top directory to crawl") @@ -244,79 +320,6 @@ fun process_dir(curr_dir: File, excludeSet: Set? = null, dirFiles: Array val exclude: Set = excludeSet ?: process_ignore_file(curr_dir) - val cssContent = """ - body { - font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; - line-height: 1.5; - padding: 1rem; - color: #1f2328; - } - main { - max-width: 800px; - margin: 0 auto; - } - ul { - list-style-type: none; - padding-left: 0; - } - a.dir-link { - display: flex; - align-items: flex-start; - gap: 0.5rem; - width: 100%; - overflow-wrap: anywhere; - box-sizing: border-box; - } - .icon { - flex-shrink: 0; - width: 1.25rem; - text-align: center; - } - a { - padding: 0.5rem; - text-decoration: none; - color: #0969da; - border-radius: 4px; - transition: background-color 0.2s ease, outline-color 0.2s ease; - } - a:hover, a:focus-visible { - background-color: #f6f8fa; - text-decoration: underline; - outline: 2px solid #0969da; - outline-offset: -2px; - } - @media (prefers-reduced-motion: reduce) { - a { - transition: none; - } - } - @media (prefers-color-scheme: dark) { - body { - background-color: #0d1117; - color: #c9d1d9; - } - a { - color: #58a6ff; - } - a:hover, a:focus-visible { - background-color: #161b22; - outline-color: #58a6ff; - } - } - .empty-dir { - padding: 0.5rem; - opacity: 0.7; - font-style: italic; - } - """ - - val styleHash = "sha256-" + Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-256").digest(cssContent.toByteArray(Charsets.UTF_8))) - - val css = """ - - """ - val index_top = """ @@ -324,11 +327,11 @@ ${cssContent} - + ${curr_dir.getName().escapeHtml()} - ${css} + ${StaticHtmlResources.CSS}
From 987a526819e2e10b2afeebf527b68a73ce4bcd25 Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Thu, 16 Jul 2026 21:17:09 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[=EC=84=B1=EB=8A=A5=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0]=20=EC=A0=95=EC=A0=81=20CSS=20=EB=B0=8F=20?= =?UTF-8?q?=ED=95=B4=EC=8B=9C=20=EA=B3=84=EC=82=B0=20=EC=BA=90=EC=8B=B1=20?= =?UTF-8?q?(=EB=94=94=EB=A0=89=ED=86=A0=EB=A6=AC=20=EB=B0=98=EB=B3=B5?= =?UTF-8?q?=EB=AC=B8=20=EC=B5=9C=EC=A0=81=ED=99=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `html4tree/main.kt`의 `process_dir` 함수가 재귀적으로 호출될 때마다 크기가 큰 CSS 문자열(`cssContent`)을 할당하고, 매번 SHA-256 해시(`styleHash`) 연산을 수행하던 병목을 제거했습니다. - 불변인 CSS 내용과 해시 값을 `private object StaticHtmlResources`로 추출하여, JVM 애플리케이션 시작 시 한 번만 계산되도록 최적화했습니다. - 이를 통해 각 디렉토리를 처리할 때마다 발생하던 불필요한 메모리 할당(GC 압력)과 CPU 연산 오버헤드를 크게 줄였습니다.