From c2551776bf8e1f0cef0190613d03b54b471d8091 Mon Sep 17 00:00:00 2001 From: 0ffz Date: Sat, 20 Aug 2022 21:41:31 -0400 Subject: [PATCH] Dont use inline class for PrefabKey, string operations are slower --- .../mineinabyss/geary/prefabs/PrefabKey.kt | 14 +++++----- .../geary/papermc/store/DataStore.kt | 8 +++--- .../papermc/store/NamespacedKeyHelpers.kt | 27 +++++-------------- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/geary-prefabs/src/main/kotlin/com/mineinabyss/geary/prefabs/PrefabKey.kt b/geary-prefabs/src/main/kotlin/com/mineinabyss/geary/prefabs/PrefabKey.kt index 1a79b6169..6066c1f71 100644 --- a/geary-prefabs/src/main/kotlin/com/mineinabyss/geary/prefabs/PrefabKey.kt +++ b/geary-prefabs/src/main/kotlin/com/mineinabyss/geary/prefabs/PrefabKey.kt @@ -11,16 +11,15 @@ import org.koin.core.component.KoinComponent * by a '`:`' symbol. */ @Serializable(with = PrefabKeySerializer::class) -@JvmInline -value class PrefabKey private constructor(val full: String) : KoinComponent { - val namespace: String get() = full.substringBefore(':') - val key: String get() = full.substringAfter(':') - +// We don't make this a value class since calculating substring is pretty expensive compared to one new object instantiation +class PrefabKey private constructor(val namespace: String, val key: String) : KoinComponent { fun toEntity(): Entity = toEntityOrNull() ?: error("Requested non null prefab entity for $this, but it does not exist.") fun toEntityOrNull(): Entity? = globalContext.prefabManager[this] + val full get() = "$namespace:$key" + override fun toString(): String = full companion object { @@ -29,9 +28,10 @@ value class PrefabKey private constructor(val full: String) : KoinComponent { /** Creates a key from a string with [namespace] and [key] separated by one '`:`' character. */ fun of(stringKey: String): PrefabKey { - if (stringKey.split(':').size != 2) + val split = stringKey.split(':') + if (split.size != 2) error("Malformed prefab key: $stringKey. Must only contain one : that splits namespace and key.") - return PrefabKey(stringKey) + return PrefabKey(split[0], split[1]) } /** Creates a key from a [namespace] and [name] which must not contain any '`:`' characters. */ diff --git a/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/store/DataStore.kt b/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/store/DataStore.kt index 30f574b83..380c4e4cd 100644 --- a/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/store/DataStore.kt +++ b/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/store/DataStore.kt @@ -62,7 +62,7 @@ inline fun PersistentDataContainer.decode(): T? { inline fun PersistentDataContainer.decode( key: NamespacedKey, serializer: DeserializationStrategy? = - globalContext.serializers.getSerializerFor(key.removeComponentPrefix(), T::class) + globalContext.serializers.getSerializerFor(key, T::class) ): T? { serializer ?: return null @@ -92,6 +92,8 @@ fun PersistentDataContainer.encodeComponents( encodePrefabs(prefabs.map { it.toRelation()!!.target.toGeary().get() }.filterNotNull()) } +private val PREFABS_KEY = "geary:prefabs".toMCKey() + /** * Encodes a list of [PrefabKey]s under the key `geary:prefabs`. When decoding these will be stored in * [DecodedEntityData.type]. @@ -104,7 +106,7 @@ fun PersistentDataContainer.encodePrefabs(keys: Collection) { encode( keys.toSet(), SetSerializer(PrefabKey.serializer()), - "geary:prefabs".toMCKey() + PREFABS_KEY ) } @@ -113,7 +115,7 @@ object PrefabNamespaceMigrations { } fun PersistentDataContainer.decodePrefabs(): Set = - decode("geary:prefabs".toMCKey(), SetSerializer(PrefabKey.serializer())) + decode(PREFABS_KEY, SetSerializer(PrefabKey.serializer())) ?.map { key -> // Migrate namespace if needed val migrated = PrefabNamespaceMigrations.migrations.getOrDefault(key.namespace, key.namespace) diff --git a/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/store/NamespacedKeyHelpers.kt b/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/store/NamespacedKeyHelpers.kt index a60de4f1f..761db567d 100644 --- a/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/store/NamespacedKeyHelpers.kt +++ b/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/store/NamespacedKeyHelpers.kt @@ -5,6 +5,9 @@ import org.bukkit.NamespacedKey import org.bukkit.persistence.PersistentDataContainer import org.bukkit.plugin.Plugin +/** The prefix present for keys of component [NamespacedKey]s. (i.e. `namespace:COMPONENT_PREFIX.key`) */ +internal const val COMPONENT_PREFIX = "component." + /** Gets all the keys under this [PersistentDataContainer] whose namespace matches the [plugin]'s. */ internal fun PersistentDataContainer.keysFrom(plugin: Plugin): List { val pluginNamespace = NamespacedKey(plugin, "").namespace @@ -12,25 +15,9 @@ internal fun PersistentDataContainer.keysFrom(plugin: Plugin): List