perf: memoize intelligent name tokens per index
This commit is contained in:
@@ -22,6 +22,7 @@ import android.icu.text.Transliterator
|
||||
import android.os.Build
|
||||
import java.text.CollationKey
|
||||
import java.text.Collator
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import org.oxycblt.musikr.tag.Name
|
||||
import org.oxycblt.musikr.tag.Placeholder
|
||||
import org.oxycblt.musikr.tag.Token
|
||||
@@ -37,14 +38,23 @@ abstract class Naming {
|
||||
abstract fun name(raw: String, sort: String?): Name.Known
|
||||
|
||||
companion object {
|
||||
fun intelligent(): Naming = IntelligentNaming
|
||||
fun intelligent(): Naming = IntelligentNaming()
|
||||
|
||||
fun simple(): Naming = SimpleNaming
|
||||
}
|
||||
}
|
||||
|
||||
data object IntelligentNaming : Naming() {
|
||||
override fun name(raw: String, sort: String?): Name.Known = IntelligentKnownName(raw, sort)
|
||||
class IntelligentNaming : Naming() {
|
||||
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() {
|
||||
@@ -79,9 +89,13 @@ private data class SimpleKnownName(override val raw: String, override val sort:
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
private data class IntelligentKnownName(override val raw: String, override val sort: String?) :
|
||||
Name.Known() {
|
||||
override val tokens = parseTokens(sort ?: raw)
|
||||
private data class IntelligentKnownName(
|
||||
override val raw: String,
|
||||
override val sort: String?,
|
||||
override val tokens: List<Token>,
|
||||
) : Name.Known()
|
||||
|
||||
private val TOKEN_REGEX by lazy { Regex("(\\d+)|(\\D+)") }
|
||||
|
||||
private fun parseTokens(name: String): List<Token> {
|
||||
// TODO: This routine is consuming much of the song building runtime, find a way to
|
||||
@@ -122,8 +136,7 @@ private data class IntelligentKnownName(override val raw: String, override val s
|
||||
// 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 }
|
||||
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
|
||||
@@ -134,8 +147,3 @@ private data class IntelligentKnownName(override val raw: String, override val s
|
||||
Token(collationKey, type)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TOKEN_REGEX by lazy { Regex("(\\d+)|(\\D+)") }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.oxycblt.musikr.tag.interpret
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotEquals
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Test
|
||||
import org.oxycblt.musikr.tag.Name
|
||||
import org.oxycblt.musikr.tag.Placeholder
|
||||
@@ -302,6 +303,16 @@ class NamingTest {
|
||||
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
|
||||
fun name_equals_simple() {
|
||||
val a = Naming.simple().name("The Same", "Same")
|
||||
|
||||
Reference in New Issue
Block a user