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,63 +89,61 @@ 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 fun parseTokens(name: String): List<Token> {
|
||||
// TODO: This routine is consuming much of the song building runtime, find a way to
|
||||
// optimize it
|
||||
var stripped =
|
||||
name
|
||||
// Replace punctuation with spaces to create token boundaries, improving
|
||||
// sorting of names like "15-9" vs "15-10".
|
||||
.replace(punctRegex, " ")
|
||||
.let { it.ifBlank { name } }
|
||||
.run {
|
||||
// Strip any english articles like "the" or "an" from the start, as music
|
||||
// sorting should ignore such when possible.
|
||||
when {
|
||||
length > 4 && startsWith("the ", ignoreCase = true) -> substring(4)
|
||||
length > 3 && startsWith("an ", ignoreCase = true) -> substring(3)
|
||||
length > 2 && startsWith("a ", ignoreCase = true) -> substring(2)
|
||||
else -> this
|
||||
}
|
||||
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
|
||||
// optimize it
|
||||
var stripped =
|
||||
name
|
||||
// Replace punctuation with spaces to create token boundaries, improving
|
||||
// sorting of names like "15-9" vs "15-10".
|
||||
.replace(punctRegex, " ")
|
||||
.let { it.ifBlank { name } }
|
||||
.run {
|
||||
// Strip any english articles like "the" or "an" from the start, as music
|
||||
// sorting should ignore such when possible.
|
||||
when {
|
||||
length > 4 && startsWith("the ", ignoreCase = true) -> substring(4)
|
||||
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 {
|
||||
private val TOKEN_REGEX by lazy { Regex("(\\d+)|(\\D+)") }
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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