mojira.dev
MC-129620

Mob spawning algorithm is super slow

The bug

The mob spawning algorithm is slow and impacts the game's TPS. This is mostly noticeably in a flat world with the gamerule doMobSpawning enabled; the tps heavily decreases. When checking the source with /debug, it showed mobSpawner as source:

[00] levels - 99.62%/99.62%
[01] |   New World - 99.82%/99.45%
[02] |   |   tick - 98.85%/98.30%
[03] |   |   |   mobSpawner - 91.42%/89.87%
[03] |   |   |   entities - 6.79%/6.67%

The lag impacts all worlds, but the amount of impact depends on how many mobs are able to spawn. (normal world with high render distance will lag more than a normal world with low render distance, for example)

How to reproduce (using a flat world, since it's most noticeably there)

  1. Create a flat world (default settings)

  2. Observe that everything is really slow (slimes, animals, sun)

  3. Disable doMobSpawning:

    /gamerule doMobSpawning false
  1. Now the tps is back to normal

Related issues

Comments

migrated

It's because hundreds of slimes are spawning in less than one minute

in a flat world

(Slimes are spawning at a height between 1-40)

AlexMCool

@Maxim Rait the tps is also very low on peaceful

migrated

I think has something to do with the mob spawning code, it even happens in default worlds sometimes. It never lagged this much in 1.12

migrated

I can confirm this. This bug exists since version 18w19a.

In a flat world with 0, 1 or 2 layers the game lags greatly, if you create a flat world with let's say 10 stone layers, the lag is reduced because the spawn attempts can not start there. When MobSpawning is turned off no lag is noticeable.
So the spawn code is somehow broken.

migrated

They really need to fix this because it makes the game unplayable because of all the lag, I try and fight mobs at night and they all lag and I can't kill them and then they speed up to me and kill me.

migrated

Still an issue in 18w21b

migrated

I use honest profiler, which isn't adversely affected by blocking on system calls and is able to provide stack traces. I ran 18w21b, and it pegged the CPU on my server.

Here is a link to the full profile:
https://www.dropbox.com/s/gk1cyxsdlmsfp5q/moblag_profile.txt?dl=0

Something is making inefficient use of toLowerCase. Worst offenders by method:
(t 11.9,s 11.9) AGCT::Unknown Java[ERR=-5]
(t 20.5,s 9.5) java.lang.String::toLowerCase
(t 9.7,s 7.9) java.lang.Character::toLowerCase
(t 5.8,s 5.8) AGCT::Unknown not Java[ERR=-3]
(t 11.7,s 4.6) java.util.HashMap::putVal
(t 3.4,s 3.4) java.lang.String::hashCode
(t 3.2,s 3.2) java.util.HashMap::resize
(t 3.0,s 3.0) java.util.HashMap$Node::<init>
(t 2.5,s 2.5) java.lang.Object::hashCode
(t 2.4,s 1.9) java.lang.String::<init>
(t 14.9,s 1.6) aia::<init>

Worst offenders by line:
(t 11.9,s 11.9) AGCT::Unknown Java[ERR=-5] @ (bci=-1,line=-100)
(t 5.8,s 5.8) AGCT::Unknown not Java[ERR=-3] @ (bci=-1,line=-100)
(t 7.3,s 5.5) java.lang.Character::toLowerCase @ (bci=5,line=6338)
(t 2.8,s 2.8) java.util.HashMap$Node::<init> @ (bci=11,line=286)
(t 2.5,s 2.5) java.lang.Object::hashCode @ (bci=-3,line=-100)
(t 2.4,s 2.4) java.lang.String::hashCode @ (bci=40,line=1471)
(t 2.0,s 2.0) java.util.HashMap::resize @ (bci=144,line=704)
(t 1.8,s 1.8) java.lang.Character::toLowerCase @ (bci=1,line=6338)
(t 1.5,s 1.5) java.lang.String::<init> @ (bci=46,line=199)
(t 1.4,s 1.4) java.lang.String::toLowerCase @ (bci=108,line=2595)
(t 1.4,s 1.4) java.util.concurrent.locks.AbstractQueuedSynchronizer::compareAndSetState @ (bci=9,line=566)
(t 1.3,s 1.3) java.lang.CharacterDataLatin1::getProperties @ (bci=7,line=72)
(t 1.3,s 1.3) java.lang.String::toLowerCase @ (bci=22,line=2571)
(t 1.2,s 1.2) sk::b @ (bci=6,line=109)
(t 1.1,s 1.1) aia::<init> @ (bci=28,line=138)

To limit the spamming, I've put slices of call trees here:
https://pastebin.com/6t7YAa39

migrated

Strangely, I tried in biome 40 (minecraft:sky_island_low) and it doesn't happen at all

I'm trying with more biomes

migrated

Biome                                   Happens?

0 / minecraft:ocean                         YES

1 / minecraft:plains                         YES

2 / minecraft:desert                         YES

8 / minecraft:hell                              NO

14 / minecraft:mushroom_island     YES

40 / minecraft:sky_island_low         NO

migrated

The slow down also depends on the render distance

pokechu22

The slow down also depends on the render distance

Oh, interesting. I actually experienced a MAJOR tps drop (~1 tick per second) a few weeks ago that persisted for a while. It turned out that's because I set my render distance to 32, and while I was getting a decent FPS, it broke the TPS. Reducing it down to 10 or so solved that. The important thing, though, is that that wasn't in a superflat world, but a normal world; perhaps this isn't exclusive to superflat?

AlexMCool

Yeah the summary isn’t correct. It’s not just about flatworlds, it’a about the mob spawning algorythm being really slow. This explains why it’s present in non-superflat worlds with high render distance (more chunks to spawn mobs) and why it doesn’t happen in all biomed (nether/end spawning algorythm might be different). I’ll try to update the summary (and description) tomorrow

migrated

Please do not mark unreleased versions as affected.
You don't have access to them yet.

AlexMCool

migrated

Community Consensus

Minecraft 18w20a, Minecraft 18w20b, Minecraft 18w20c, Minecraft 18w21a, Minecraft 18w21b, Minecraft 18w22a, Minecraft 18w22b, Minecraft 18w22c, Minecraft 1.13-pre1

Minecraft 1.13-pre2

Retrieved