Skip to content
Closed
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
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 4 additions & 3 deletions src/main/kotlin/html4tree/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,19 @@ fun process_dir(curr_dir: File){
"""

val index_middle = fun():String{
var l=""
val sb = StringBuilder()

val dir_files: MutableList<File> = 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 += """ <li><a style="display:block; width:100%" href="${if (isLinkedDirectory) { "./${it.getName().urlEncodePath()}/" } else { "./${it.getName().urlEncodePath()}" }}">${if (isLinkedDirectory) { "&#128193;" } else { "&rtrif;" }} ${it.getName().escapeHtml()}</a></li>"""+"\n"
sb.append(""" <li><a style="display:block; width:100%" href="${if (isLinkedDirectory) { "./${it.getName().urlEncodePath()}/" } else { "./${it.getName().urlEncodePath()}" }}">${if (isLinkedDirectory) { "&#128193;" } else { "&rtrif;" }} ${it.getName().escapeHtml()}</a></li>""")
sb.append('\n')
}
}

return l;
return sb.toString();
}

val index_bottom="""
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/html4tree/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LinkedList {
if(l == null){
return null
} else {
l.next = null
l.next = null
return LinkedListEntry(l.data, l.level)
}
}
Expand Down