Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve performance of getSmallestEnclosure #41

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/com/github/zly2006/enclosure/EnclosureList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import net.minecraft.world.PersistentState
class EnclosureList(world: ServerWorld, isRoot: Boolean) : PersistentState() {
private val areaMap: MutableMap<String, EnclosureArea> = HashMap()
private val boundWorld: ServerWorld?
val areas = areaMap.values
var areas: List<EnclosureArea> = listOf()
private set

constructor(nbt: NbtCompound, world: ServerWorld, isRoot: Boolean) : this(world, isRoot) {
(nbt[ENCLOSURE_LIST_KEY] as? NbtList)?.forEach {
Expand All @@ -25,6 +26,8 @@ class EnclosureList(world: ServerWorld, isRoot: Boolean) : PersistentState() {
areaMap[name] = EnclosureArea(compound, world)
}
}

updateAreasList()
}

init {
Expand Down Expand Up @@ -75,6 +78,7 @@ class EnclosureList(world: ServerWorld, isRoot: Boolean) : PersistentState() {
fun remove(name: String): Boolean {
if (areaMap.containsKey(name)) {
areaMap.remove(name)
updateAreasList()
markDirty()
return true
}
Expand All @@ -83,8 +87,14 @@ class EnclosureList(world: ServerWorld, isRoot: Boolean) : PersistentState() {

fun addArea(area: EnclosureArea) {
areaMap[area.name] = area
updateAreasList()
markDirty()
}

// 添加和删除操作一般很少,慢一点无所谓
private fun updateAreasList() {
areas = areaMap.values.toList()
}
}

const val DATA_VERSION_KEY = "data_version"
Expand Down
13 changes: 7 additions & 6 deletions src/main/kotlin/com/github/zly2006/enclosure/EnclosureArea.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ open class EnclosureArea : PersistentState, EnclosureView {
}

private var locked = false
final override var minX by lockChecker(0)
final override var minY by lockChecker(0)
final override var minZ by lockChecker(0)
final override var maxX by lockChecker(0)
final override var maxY by lockChecker(0)
final override var maxZ by lockChecker(0)
// removed lockChecker due to performance issues
final override var minX = 0
final override var minY = 0
final override var minZ = 0
final override var maxX = 0
final override var maxY = 0
final override var maxZ = 0
var world: ServerWorld
protected set
final override var name = ""
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/com/github/zly2006/enclosure/ServerMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ object ServerMain: ModInitializer {
fun getSmallestEnclosure(world: ServerWorld, pos: BlockPos?): EnclosureArea? {
return enclosures[world.registryKey]!!.areas
.firstOrNull { area: EnclosureArea -> area.isInner(pos!!) }
?.areaOf(pos!!)
}

fun checkPermission(player: ServerPlayerEntity, permission: Permission, pos: BlockPos): Boolean {
Expand Down
Loading