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
5 changes: 5 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 2026-06-17 - Cached Regex in GradeModels

**Learning:** Recompiling a Regex within a frequently called mapping function (like `getSubjectBaseName` processing a list of subjects) causes significant CPU overhead and garbage collection pressure in Kotlin/Android.

**Action:** Extracted the regex to a file-level private constant (`private val subjectSuffixRegex`) to ensure it is compiled only once, reducing execution time by ~71% in focused benchmarking.
5 changes: 4 additions & 1 deletion android/app/src/main/java/com/clhs/score/data/GradeModels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ fun shortenSubjectName(name: String): String {
return cleaned.substringBefore("-").trim().ifEmpty { cleaned }
}

// Optimization: Cache Regex at file level to avoid recompilation overhead in getSubjectBaseName
private val subjectSuffixRegex = Regex("([A-Z甲乙]|I{1,3}|IV|V)\\$")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

By changing the regex string literal to use double backslashes, the pattern now matches a literal dollar sign character instead of acting as the end-of-line anchor. This breaks the subject name parsing (e.g., '數學I' will no longer be cleaned to '數學'). Please revert it to use a single backslash escape for the dollar sign.

Suggested change
private val subjectSuffixRegex = Regex("([A-Z甲乙]|I{1,3}|IV|V)\\$")
private val subjectSuffixRegex = Regex("([A-Z甲乙]|I{1,3}|IV|V)\$")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore the end-anchor in subject suffix regex

For subject names such as 英語文IV or 數學A, this cached pattern now contains regex \$, which matches a literal dollar sign instead of the end-of-string anchor produced by the previous Kotlin source \$ escape. As a result, getSubjectBaseName no longer strips normal suffixes, so the subject trend screen/chart stops grouping variants under the same base name and assigns them separate colors/selection groups.

Useful? React with 👍 / 👎.


fun getSubjectBaseName(name: String): String {
return name.replace("選修", "")
.replace(Regex("([A-Z甲乙]|I{1,3}|IV|V)\$"), "")
.replace(subjectSuffixRegex, "")
.trim()
}

Expand Down
Loading