replace some sets with ObjectOpenHashSet

(should improve performance at some points)
This commit is contained in:
Bixilon 2023-03-20 10:41:40 +01:00
parent d939f3f7b1
commit 2599a641a7
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
8 changed files with 20 additions and 13 deletions

View File

@ -21,6 +21,7 @@ import de.bixilon.minosoft.assets.util.FileAssetsUtil.toAssetName
import de.bixilon.minosoft.assets.util.FileUtil import de.bixilon.minosoft.assets.util.FileUtil
import de.bixilon.minosoft.assets.util.InputStreamUtil.readJson import de.bixilon.minosoft.assets.util.InputStreamUtil.readJson
import de.bixilon.minosoft.data.registries.identified.ResourceLocation import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.io.FileNotFoundException import java.io.FileNotFoundException
@ -38,8 +39,8 @@ class DirectoryAssetsManager(
val prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX, val prefix: String = AssetsManager.DEFAULT_ASSETS_PREFIX,
) : AssetsManager { ) : AssetsManager {
private val basePath = File(rootPath).slashPath + "/" + prefix private val basePath = File(rootPath).slashPath + "/" + prefix
override val namespaces: MutableSet<String> = mutableSetOf() override val namespaces: MutableSet<String> = ObjectOpenHashSet()
private var assets: MutableSet<ResourceLocation> = mutableSetOf() private var assets: MutableSet<ResourceLocation> = ObjectOpenHashSet()
override var loaded: Boolean = false override var loaded: Boolean = false
private set private set
override var image: ByteArray? = null override var image: ByteArray? = null

View File

@ -18,6 +18,7 @@ import de.bixilon.minosoft.assets.AssetsManager
import de.bixilon.minosoft.assets.util.FileAssetsUtil.toAssetName import de.bixilon.minosoft.assets.util.FileAssetsUtil.toAssetName
import de.bixilon.minosoft.assets.util.InputStreamUtil.readJson import de.bixilon.minosoft.assets.util.InputStreamUtil.readJson
import de.bixilon.minosoft.data.registries.identified.ResourceLocation import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.util.zip.ZipInputStream import java.util.zip.ZipInputStream
@ -38,7 +39,7 @@ class ZipAssetsManager(
override fun load(latch: CountUpAndDownLatch) { override fun load(latch: CountUpAndDownLatch) {
check(!loaded) { "Already loaded!" } check(!loaded) { "Already loaded!" }
val namespaces: MutableSet<String> = mutableSetOf() val namespaces: MutableSet<String> = ObjectOpenHashSet()
while (true) { while (true) {
val entry = inputStream.nextEntry ?: break val entry = inputStream.nextEntry ?: break
if (entry.isDirectory) { if (entry.isDirectory) {

View File

@ -37,6 +37,7 @@ import de.bixilon.minosoft.util.json.Jackson
import de.bixilon.minosoft.util.logging.Log import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType import de.bixilon.minosoft.util.logging.LogMessageType
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
@ -96,7 +97,7 @@ class IndexAssetsManager(
val worker = UnconditionalWorker() val worker = UnconditionalWorker()
val hashes: MutableSet<String> = mutableSetOf() val hashes: MutableSet<String> = ObjectOpenHashSet()
for ((path, data) in assets) { for ((path, data) in assets) {
check(data is Map<*, *>) check(data is Map<*, *>)

View File

@ -32,6 +32,7 @@ import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.data.registries.registries.Registries import de.bixilon.minosoft.data.registries.registries.Registries
import de.bixilon.minosoft.data.registries.registries.registry.MetaTypes import de.bixilon.minosoft.data.registries.registries.registry.MetaTypes
import de.bixilon.minosoft.data.registries.registries.registry.Registry import de.bixilon.minosoft.data.registries.registries.registry.Registry
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
class BlockRegistry( class BlockRegistry(
parent: Registry<Block>? = null, parent: Registry<Block>? = null,
@ -42,7 +43,7 @@ class BlockRegistry(
fun updateStates(block: Block, data: JsonObject, registries: Registries) { fun updateStates(block: Block, data: JsonObject, registries: Registries) {
val properties: MutableMap<BlockProperties, MutableSet<Any>> = mutableMapOf() val properties: MutableMap<BlockProperties, MutableSet<Any>> = mutableMapOf()
val states: MutableSet<BlockState> = mutableSetOf() val states: MutableSet<BlockState> = ObjectOpenHashSet()
for ((stateId, stateJson) in data["states"].asAnyMap()) { for ((stateId, stateJson) in data["states"].asAnyMap()) {
val settings = BlockStateSettings.of(registries, stateJson.unsafeCast()) val settings = BlockStateSettings.of(registries, stateJson.unsafeCast())
val state = if (block is BlockStateBuilder) block.buildState(settings) else AdvancedBlockState(block, settings) val state = if (block is BlockStateBuilder) block.buildState(settings) else AdvancedBlockState(block, settings)
@ -53,7 +54,7 @@ class BlockRegistry(
if (state !is PropertyBlockState) continue if (state !is PropertyBlockState) continue
for ((property, value) in state.properties) { for ((property, value) in state.properties) {
properties.getOrPut(property) { mutableSetOf() } += value properties.getOrPut(property) { ObjectOpenHashSet() } += value
} }
} }

View File

@ -34,6 +34,7 @@ import de.bixilon.minosoft.tags.MinecraftTagTypes.ITEM
import de.bixilon.minosoft.tags.Tag import de.bixilon.minosoft.tags.Tag
import de.bixilon.minosoft.tags.TagList import de.bixilon.minosoft.tags.TagList
import de.bixilon.minosoft.tags.TagManager import de.bixilon.minosoft.tags.TagManager
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
object FallbackTags { object FallbackTags {
private val tags: MutableMap<ResourceLocation, MutableMap<ResourceLocation, Set<ResourceLocation>>> = mutableMapOf() private val tags: MutableMap<ResourceLocation, MutableMap<ResourceLocation, Set<ResourceLocation>>> = mutableMapOf()
@ -41,7 +42,7 @@ object FallbackTags {
private fun read(type: ResourceLocation, name: ResourceLocation): Set<ResourceLocation> { private fun read(type: ResourceLocation, name: ResourceLocation): Set<ResourceLocation> {
val content = Minosoft.MINOSOFT_ASSETS_MANAGER[ResourceLocation(name.namespace, "tags/${type.path}/${name.path}.json")].readJsonObject()["values"].asAnyCollection() val content = Minosoft.MINOSOFT_ASSETS_MANAGER[ResourceLocation(name.namespace, "tags/${type.path}/${name.path}.json")].readJsonObject()["values"].asAnyCollection()
val set: MutableSet<ResourceLocation> = mutableSetOf() val set: MutableSet<ResourceLocation> = ObjectOpenHashSet()
for (entry in content) { for (entry in content) {
set += ResourceLocation.of(entry.toString()) set += ResourceLocation.of(entry.toString())
} }
@ -68,7 +69,7 @@ object FallbackTags {
val map: MutableMap<ResourceLocation, Tag<T>> = mutableMapOf() val map: MutableMap<ResourceLocation, Tag<T>> = mutableMapOf()
for ((name, entries) in entries) { for ((name, entries) in entries) {
val set: MutableSet<T> = mutableSetOf() val set: MutableSet<T> = ObjectOpenHashSet()
for (item in entries) { for (item in entries) {
set += registry[item] ?: continue set += registry[item] ?: continue
} }

View File

@ -40,7 +40,7 @@ abstract class AbstractVoxelShape : Iterable<AABB> {
} }
operator fun plus(offset: Vec3t<out Number>): AbstractVoxelShape { operator fun plus(offset: Vec3t<out Number>): AbstractVoxelShape {
val result: MutableSet<AABB> = mutableSetOf() val result: MutableSet<AABB> = ObjectOpenHashSet()
for (aabb in this) { for (aabb in this) {
result.add(aabb + offset) result.add(aabb + offset)
} }
@ -48,7 +48,7 @@ abstract class AbstractVoxelShape : Iterable<AABB> {
} }
fun add(other: AbstractVoxelShape): AbstractVoxelShape { fun add(other: AbstractVoxelShape): AbstractVoxelShape {
val aabbs: MutableSet<AABB> = mutableSetOf() val aabbs: MutableSet<AABB> = ObjectOpenHashSet()
aabbs += this aabbs += this
aabbs += other aabbs += other
return VoxelShape(aabbs) return VoxelShape(aabbs)

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger * Copyright (C) 2020-2023 Moritz Zwerger
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* *
@ -22,6 +22,7 @@ import de.bixilon.minosoft.modding.event.events.CancelableEvent
import de.bixilon.minosoft.modding.event.events.Event import de.bixilon.minosoft.modding.event.events.Event
import de.bixilon.minosoft.modding.event.listener.EventListener import de.bixilon.minosoft.modding.event.listener.EventListener
import de.bixilon.minosoft.modding.event.listener.OneShotListener import de.bixilon.minosoft.modding.event.listener.OneShotListener
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import java.util.* import java.util.*
open class EventMaster(vararg parents: AbstractEventMaster) : AbstractEventMaster { open class EventMaster(vararg parents: AbstractEventMaster) : AbstractEventMaster {
@ -61,7 +62,7 @@ open class EventMaster(vararg parents: AbstractEventMaster) : AbstractEventMaste
} }
parentLock.release() parentLock.release()
val toRemove: MutableSet<EventListener> = mutableSetOf() val toRemove: MutableSet<EventListener> = ObjectOpenHashSet()
eventInvokerLock.acquire() eventInvokerLock.acquire()
val worker = UnconditionalWorker() val worker = UnconditionalWorker()
for (invoker in eventListeners) { for (invoker in eventListeners) {

View File

@ -36,6 +36,7 @@ import de.bixilon.minosoft.tags.TagManager
import de.bixilon.minosoft.util.logging.Log import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType import de.bixilon.minosoft.util.logging.LogMessageType
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
@LoadPacket @LoadPacket
class TagsS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket { class TagsS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
@ -91,7 +92,7 @@ class TagsS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
} }
private fun <T : RegistryItem> PlayInByteBuffer.readTag(registry: Registry<T>): Tag<T> { private fun <T : RegistryItem> PlayInByteBuffer.readTag(registry: Registry<T>): Tag<T> {
val items: MutableSet<T> = mutableSetOf() val items: MutableSet<T> = ObjectOpenHashSet()
for (id in readVarIntArray()) { for (id in readVarIntArray()) {
items += registry.getOrNull(id) ?: continue items += registry.getOrNull(id) ?: continue
} }