perf: memoize intelligent name tokens per index

This commit is contained in:
jay
2026-11-13 14:10:20 -05:00
parent e12f00c49b
commit 214054c5e4
2 changed files with 75 additions and 56 deletions
@@ -22,6 +22,7 @@ import android.icu.text.Transliterator
import android.os.Build import android.os.Build
import java.text.CollationKey import java.text.CollationKey
import java.text.Collator import java.text.Collator
import java.util.concurrent.ConcurrentHashMap
import org.oxycblt.musikr.tag.Name import org.oxycblt.musikr.tag.Name
import org.oxycblt.musikr.tag.Placeholder import org.oxycblt.musikr.tag.Placeholder
import org.oxycblt.musikr.tag.Token import org.oxycblt.musikr.tag.Token
@@ -37,14 +38,23 @@ abstract class Naming {
abstract fun name(raw: String, sort: String?): Name.Known abstract fun name(raw: String, sort: String?): Name.Known
companion object { companion object {
fun intelligent(): Naming = IntelligentNaming fun intelligent(): Naming = IntelligentNaming()
fun simple(): Naming = SimpleNaming fun simple(): Naming = SimpleNaming
} }
} }
data object IntelligentNaming : Naming() { class IntelligentNaming : Naming() {
override fun name(raw: String, sort: String?): Name.Known = IntelligentKnownName(raw, sort) private val tokenCache = ConcurrentHashMap<String, List<Token>>()
override fun name(raw: String, sort: String?): Name.Known {
val effectiveName = sort ?: raw
return IntelligentKnownName(
raw,
sort,
tokenCache.computeIfAbsent(effectiveName, ::parseTokens),
)
}
} }
data object SimpleNaming : Naming() { data object SimpleNaming : Naming() {
@@ -79,63 +89,61 @@ private data class SimpleKnownName(override val raw: String, override val sort:
* *
* @author Alexander Capehart (OxygenCobalt) * @author Alexander Capehart (OxygenCobalt)
*/ */
private data class IntelligentKnownName(override val raw: String, override val sort: String?) : private data class IntelligentKnownName(
Name.Known() { override val raw: String,
override val tokens = parseTokens(sort ?: raw) override val sort: String?,
override val tokens: List<Token>,
) : Name.Known()
private fun parseTokens(name: String): List<Token> { private val TOKEN_REGEX by lazy { Regex("(\\d+)|(\\D+)") }
// TODO: This routine is consuming much of the song building runtime, find a way to
// optimize it private fun parseTokens(name: String): List<Token> {
var stripped = // TODO: This routine is consuming much of the song building runtime, find a way to
name // optimize it
// Replace punctuation with spaces to create token boundaries, improving var stripped =
// sorting of names like "15-9" vs "15-10". name
.replace(punctRegex, " ") // Replace punctuation with spaces to create token boundaries, improving
.let { it.ifBlank { name } } // sorting of names like "15-9" vs "15-10".
.run { .replace(punctRegex, " ")
// Strip any english articles like "the" or "an" from the start, as music .let { it.ifBlank { name } }
// sorting should ignore such when possible. .run {
when { // Strip any english articles like "the" or "an" from the start, as music
length > 4 && startsWith("the ", ignoreCase = true) -> substring(4) // sorting should ignore such when possible.
length > 3 && startsWith("an ", ignoreCase = true) -> substring(3) when {
length > 2 && startsWith("a ", ignoreCase = true) -> substring(2) length > 4 && startsWith("the ", ignoreCase = true) -> substring(4)
else -> this length > 3 && startsWith("an ", ignoreCase = true) -> substring(3)
} length > 2 && startsWith("a ", ignoreCase = true) -> substring(2)
else -> this
} }
// Transliterate to latin if available
if (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
Transliterator.getAvailableIDs().toList().contains("Any-Latin")
) {
stripped = Transliterator.getInstance("Any-Latin;").transliterate(stripped)
}
// To properly compare numeric components in names, we have to split them up into
// individual lexicographic and numeric tokens and then individually compare them
// with special logic.
return TOKEN_REGEX.findAll(stripped).mapTo(mutableListOf()) { match ->
// Remove excess whitespace where possible
val token = match.value.trim().ifEmpty { match.value }
val collationKey: CollationKey
val type: Token.Type
// Separate each token into their numeric and lexicographic counterparts.
if (token.first().isDigit()) {
// The digit string comparison breaks with preceding zero digits, remove those
val digits =
token.trimStart { Character.getNumericValue(it) == 0 }.ifEmpty { token }
// Other languages have other types of digit strings, still use collation keys
collationKey = collator.getCollationKey(digits)
type = Token.Type.NUMERIC
} else {
collationKey = collator.getCollationKey(token)
type = Token.Type.LEXICOGRAPHIC
} }
Token(collationKey, type)
} // Transliterate to latin if available
if (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
Transliterator.getAvailableIDs().toList().contains("Any-Latin")
) {
stripped = Transliterator.getInstance("Any-Latin;").transliterate(stripped)
} }
companion object { // To properly compare numeric components in names, we have to split them up into
private val TOKEN_REGEX by lazy { Regex("(\\d+)|(\\D+)") } // individual lexicographic and numeric tokens and then individually compare them
// with special logic.
return TOKEN_REGEX.findAll(stripped).mapTo(mutableListOf()) { match ->
// Remove excess whitespace where possible
val token = match.value.trim().ifEmpty { match.value }
val collationKey: CollationKey
val type: Token.Type
// Separate each token into their numeric and lexicographic counterparts.
if (token.first().isDigit()) {
// The digit string comparison breaks with preceding zero digits, remove those
val digits = token.trimStart { Character.getNumericValue(it) == 0 }.ifEmpty { token }
// Other languages have other types of digit strings, still use collation keys
collationKey = collator.getCollationKey(digits)
type = Token.Type.NUMERIC
} else {
collationKey = collator.getCollationKey(token)
type = Token.Type.LEXICOGRAPHIC
}
Token(collationKey, type)
} }
} }
@@ -20,6 +20,7 @@ package org.oxycblt.musikr.tag.interpret
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertSame
import org.junit.Test import org.junit.Test
import org.oxycblt.musikr.tag.Name import org.oxycblt.musikr.tag.Name
import org.oxycblt.musikr.tag.Placeholder import org.oxycblt.musikr.tag.Placeholder
@@ -302,6 +303,16 @@ class NamingTest {
assertEquals(Token.Type.LEXICOGRAPHIC, only.type) assertEquals(Token.Type.LEXICOGRAPHIC, only.type)
} }
@Test
fun name_intelligent_sameEffectiveName_reusesTokens() {
val naming = Naming.intelligent()
val first = naming.name("The Same", "Same")
val second = naming.name("Same", "Same")
assertSame(first.tokens, second.tokens)
}
@Test @Test
fun name_equals_simple() { fun name_equals_simple() {
val a = Naming.simple().name("The Same", "Same") val a = Naming.simple().name("The Same", "Same")