Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 5458f6c

Browse files
authored
Merge pull request #198 from DockyardMC/misc/optimizations
miscellaneous optimizations (optimizing at 3am is addicting)
2 parents 1573605 + 0e0ba58 commit 5458f6c

34 files changed

+272
-407
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies {
4040
implementation("com.akuleshov7:ktoml-core:0.5.1")
4141
implementation("com.akuleshov7:ktoml-file:0.5.1")
4242
implementation("net.bytebuddy:byte-buddy-agent:1.14.12")
43+
implementation("org.jctools:jctools-core:4.0.5")
4344

4445
api("io.github.dockyardmc:bytesocks-client-java:1.0-SNAPSHOT") {
4546
exclude(module = "slf4j-api")

src/main/kotlin/io/github/dockyardmc/DockyardServer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class DockyardServer(configBuilder: Config.() -> Unit) {
9797
if (ConfigManager.config.implementationConfig.applyBlockPlacementRules) DefaultBlockHandlers().register()
9898
}
9999

100-
NetworkCompression.compressionThreshold = ConfigManager.config.networkCompressionThreshold
100+
NetworkCompression.COMPRESSION_THRESHOLD = ConfigManager.config.networkCompressionThreshold
101101
WorldManager.loadDefaultWorld()
102102

103103
Events.dispatch(ServerFinishLoadEvent(this))

src/main/kotlin/io/github/dockyardmc/apis/Hologram.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class Hologram(spawnLocation: Location, builder: HologramBuilder) : Entity(spawn
6363
}
6464

6565
fun addStaticLine(line: StaticContentLine) {
66-
val lineIndex = lines.size
6766
lines.add(line)
67+
if (viewers.isEmpty()) return
6868
viewers.forEach(::updateFull)
6969
}
7070

@@ -73,8 +73,8 @@ class Hologram(spawnLocation: Location, builder: HologramBuilder) : Entity(spawn
7373
}
7474

7575
fun addPlayerLine(line: PlayerContentLine) {
76-
val lineIndex = lines.size
7776
lines.add(line)
77+
if (viewers.isEmpty()) return
7878
viewers.forEach(::updateFull)
7979
}
8080

src/main/kotlin/io/github/dockyardmc/apis/sidebar/Sidebar.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ class Sidebar(initialTitle: String, initialLines: Map<Int, SidebarLine>) : Viewa
8080
}
8181

8282
fun setGlobalLine(index: Int, value: String) {
83+
if (viewers.isEmpty()) return
8384
val before = indexToLineMap[index] as SidebarLine.Static?
8485
indexToLineMap[index] = SidebarLine.Static(value)
8586
if (before?.value != value) viewers.forEach { viewer -> sendLinePacket(viewer, index) }
8687
}
8788

8889
fun setPlayerLine(index: Int, value: (Player) -> String) {
90+
if (viewers.isEmpty()) return
8991
indexToLineMap[index] = SidebarLine.Player(value)
9092
viewers.forEach { viewer -> sendLinePacket(viewer, index) }
9193
}
@@ -116,6 +118,7 @@ class Sidebar(initialTitle: String, initialLines: Map<Int, SidebarLine>) : Viewa
116118

117119
init {
118120
title.valueChanged { event ->
121+
if (viewers.isEmpty()) return@valueChanged
119122
val packet = ClientboundScoreboardObjectivePacket(objective, ScoreboardMode.EDIT_TEXT, event.newValue, ScoreboardType.INTEGER)
120123
viewers.sendPacket(packet)
121124
}

src/main/kotlin/io/github/dockyardmc/bindables/BindablePairMap.kt

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/main/kotlin/io/github/dockyardmc/data/DataComponentHasher.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main/kotlin/io/github/dockyardmc/data/DataComponentRegistry.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import io.github.dockyardmc.data.components.*
44
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
55
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
66
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap
7-
import java.util.concurrent.atomic.AtomicInteger
87
import kotlin.reflect.KClass
98

109
object DataComponentRegistry {
1110

12-
val protocolIdCounter = AtomicInteger()
11+
var protocolIdCounter = 0
1312
val dataComponentsById = Int2ObjectOpenHashMap<KClass<out DataComponent>>()
1413
val dataComponentsByIdentifier = Object2ObjectOpenHashMap<String, KClass<out DataComponent>>()
1514

@@ -113,13 +112,14 @@ object DataComponentRegistry {
113112
val SHULKER_COLOR = register("minecraft:shulker/color", ShulkerColorComponent::class)
114113

115114
fun register(identifier: String, kclass: KClass<out DataComponent>): KClass<out DataComponent> {
116-
val protocolId = protocolIdCounter.getAndIncrement()
115+
val protocolId = protocolIdCounter
117116
dataComponentsById[protocolId] = kclass
118117
dataComponentsByIdReversed[kclass] = protocolId
119118

120119
dataComponentsByIdentifier[identifier] = kclass
121120
dataComponentsByIdentifierReversed[kclass] = identifier
122121

122+
protocolIdCounter++
123123
return kclass
124124
}
125125
}

src/main/kotlin/io/github/dockyardmc/data/components/PotionContentsComponent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PotionContentsComponent(
3333
override fun write(buffer: ByteBuf) {
3434
buffer.writeOptional(potion?.getProtocolId(), ByteBuf::writeVarInt)
3535
buffer.writeOptional(customColor, CustomColor::writePackedInt)
36-
buffer.writeList(effects, ByteBuf::writeAppliedPotionEffect)
36+
buffer.writeList(effects, AppliedPotionEffect::write)
3737
buffer.writeOptional(customName, ByteBuf::writeString)
3838
}
3939

@@ -42,7 +42,7 @@ class PotionContentsComponent(
4242
return PotionContentsComponent(
4343
buffer.readOptional(ByteBuf::readVarInt)?.let { PotionTypeRegistry.getByProtocolId(it) },
4444
buffer.readOptional(ByteBuf::readInt)?.let { CustomColor.fromRGBInt(it) },
45-
buffer.readAppliedPotionEffectsList(),
45+
buffer.readList(AppliedPotionEffect::read),
4646
buffer.readOptional(ByteBuf::readString)
4747
)
4848
}

src/main/kotlin/io/github/dockyardmc/events/Event.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@ interface Event {
1717
other: Set<Any> = setOf<Any>(),
1818
val isGlobalEvent: Boolean = false
1919
) {
20-
// what the fuck
21-
val players = players + entities.filterIsInstance<Player>()
22-
var entities = entities + players
23-
val locations = locations + this.entities.map { it.location }
24-
val worlds = worlds + this.locations.map { it.world }
20+
// combining sets is expensive and is done in initialization of every event.
21+
// In most cases, either none or only one is accessed. Let's make them lazy so they are
22+
// computed only when needed
23+
val players: Set<Player> by lazy {
24+
players + entities.filterIsInstance<Player>()
25+
}
2526

26-
val other: Set<Any> = this.players + this.entities + this.worlds + this.locations + other
27+
val entities: Set<Entity> by lazy {
28+
entities + players
29+
}
30+
31+
val locations: Set<Location> by lazy {
32+
locations + this.entities.map { entity -> entity.location }
33+
}
34+
val worlds: Set<World> by lazy {
35+
worlds + this.locations.map { location -> location.world }
36+
}
37+
38+
val other: Set<Any> by lazy {
39+
this.players + this.entities + this.worlds + this.locations + other
40+
}
2741

2842
operator fun contains(element: Any) = other.contains(element)
2943

0 commit comments

Comments
 (0)