Skip to content

Commit

Permalink
new: Add patch to optimize mob spawning
Browse files Browse the repository at this point in the history
Retrieving the entity spawn list for a given entity category is a
rather hot activity in mob spawning. This replaces the EntityCategory->List map
with a specialized type for enum keys, which reduces a hash lookup to an array index.
  • Loading branch information
jellysquid3 committed May 9, 2020
1 parent 5aac5de commit a122476
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.jellysquid.mods.lithium.mixin.world.mob_spawning;

import com.google.common.collect.Maps;
import net.minecraft.entity.EntityCategory;
import net.minecraft.world.biome.Biome;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;
import java.util.Map;

@Mixin(Biome.class)
public class MixinBiome {
@Mutable
@Shadow
@Final
private Map<EntityCategory, List<Biome.SpawnEntry>> spawns;

/**
* Re-initialize the spawn category lists with a much faster backing collection type for enum keys. This provides
* a modest speed-up for mob spawning as {@link Biome#getEntitySpawnList(EntityCategory)} is a rather hot method.
*/
@Inject(method = "<init>", at = @At("RETURN"))
private void reinit(Biome.Settings settings, CallbackInfo ci) {
Map<EntityCategory, List<Biome.SpawnEntry>> spawns = Maps.newEnumMap(EntityCategory.class);
spawns.putAll(this.spawns);

this.spawns = spawns;
}
}
1 change: 1 addition & 0 deletions src/main/resources/lithium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"world.chunk_ticking.MixinThreadedAnvilChunkStorage",
"world.explosions.MixinExplosion",
"world.fire_checks.MixinWorld",
"world.mob_spawning.MixinBiome",
"world.tick_scheduler.MixinServerWorld"
]
}

0 comments on commit a122476

Please sign in to comment.