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 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,11 +89,15 @@ 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+)") }
|
||||||
|
|
||||||
|
private fun parseTokens(name: String): List<Token> {
|
||||||
// TODO: This routine is consuming much of the song building runtime, find a way to
|
// TODO: This routine is consuming much of the song building runtime, find a way to
|
||||||
// optimize it
|
// optimize it
|
||||||
var stripped =
|
var stripped =
|
||||||
@@ -122,8 +136,7 @@ private data class IntelligentKnownName(override val raw: String, override val s
|
|||||||
// Separate each token into their numeric and lexicographic counterparts.
|
// Separate each token into their numeric and lexicographic counterparts.
|
||||||
if (token.first().isDigit()) {
|
if (token.first().isDigit()) {
|
||||||
// The digit string comparison breaks with preceding zero digits, remove those
|
// The digit string comparison breaks with preceding zero digits, remove those
|
||||||
val digits =
|
val digits = token.trimStart { Character.getNumericValue(it) == 0 }.ifEmpty { token }
|
||||||
token.trimStart { Character.getNumericValue(it) == 0 }.ifEmpty { token }
|
|
||||||
// Other languages have other types of digit strings, still use collation keys
|
// Other languages have other types of digit strings, still use collation keys
|
||||||
collationKey = collator.getCollationKey(digits)
|
collationKey = collator.getCollationKey(digits)
|
||||||
type = Token.Type.NUMERIC
|
type = Token.Type.NUMERIC
|
||||||
@@ -133,9 +146,4 @@ private data class IntelligentKnownName(override val raw: String, override val s
|
|||||||
}
|
}
|
||||||
Token(collationKey, type)
|
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.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")
|
||||||
|
|||||||
Reference in New Issue
Block a user