diff --git a/.jules/bolt.md b/.jules/bolt.md index 39f32f6e..14ee10e1 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -1,3 +1,7 @@ ## 2024-06-21 - Regex Compilation in Loops **Learning:** In Kotlin, compiling regular expressions (`.toRegex()`) inside a loop over files is a significant O(N * M) performance bottleneck when processing ignore files (N files * M rules). **Action:** Always map string rules to compiled `Regex` objects outside of the file iteration loop (O(M) compilation) to avoid unnecessary regex re-compilations. + +## 2024-05-24 - Kotlin String Concatenation Bottleneck +**Learning:** In Kotlin (as in Java), repeatedly using the `+=` operator for string concatenation within a loop leads to O(N^2) time complexity and massive memory reallocation overhead because strings are immutable. This becomes a major bottleneck when dynamically rendering HTML for directories containing a large number of files. +**Action:** Always prefer `StringBuilder` when concatenating strings within a loop to maintain O(N) complexity and minimize memory allocations. diff --git a/src/main/kotlin/html4tree/main.kt b/src/main/kotlin/html4tree/main.kt index 50b2680d..4a940642 100644 --- a/src/main/kotlin/html4tree/main.kt +++ b/src/main/kotlin/html4tree/main.kt @@ -124,18 +124,19 @@ fun process_dir(curr_dir: File){ """ val index_middle = fun():String{ - var l="" + val sb = StringBuilder() val dir_files: MutableList = curr_dir.listFiles()?.toMutableList() ?: mutableListOf() dir_files.sortWith(compareBy ({it.name}) ) dir_files.forEach { val isLinkedDirectory = it.isDirectory() && !java.nio.file.Files.isSymbolicLink(it.toPath()) if((it.getName() !in exclude) && (isLinkedDirectory || !it.isDirectory())) { - l += """
  • ${if (isLinkedDirectory) { "📁" } else { "▸" }} ${it.getName().escapeHtml()}
  • """+"\n" + sb.append("""
  • ${if (isLinkedDirectory) { "📁" } else { "▸" }} ${it.getName().escapeHtml()}
  • """) + sb.append('\n') } } - return l; + return sb.toString(); } val index_bottom=""" diff --git a/src/main/kotlin/html4tree/util.kt b/src/main/kotlin/html4tree/util.kt index 9a882d74..c8979c31 100644 --- a/src/main/kotlin/html4tree/util.kt +++ b/src/main/kotlin/html4tree/util.kt @@ -39,7 +39,7 @@ class LinkedList { if(l == null){ return null } else { - l.next = null + l.next = null return LinkedListEntry(l.data, l.level) } }