Throttle spawning per chunk instead of charging nearby players - #14187
Throttle spawning per chunk instead of charging nearby players#14187RasmusKD wants to merge 1 commit into
Conversation
|
One more measurement, since the obvious question is what happens to a chunk that a region only partly covers. Worst case I could build: a 20x20 area that allows monsters, offset so it cuts through four chunks with no whole chunk anywhere inside it, surrounded by 117 small claims that deny them. Every chunk in the farm is a mixed chunk. Same method as above, mobs killed continuously so the cap never fills, three alternating runs:
The counter clears on any successful spawn, so a mixed chunk never reaches eight consecutive refusals and never throttles. There may be a small dip of about one spawn per 10s, but the spread is wider than the difference so I would not claim it. |
|
That's too big of a change to be made on all servers. |
A plugin that refuses PreCreatureSpawnEvent is charged to a per player backoff counter, which is added to the mob cap of every player within simulation distance. The refusal is a property of the location, but the counter follows the player, so denying spawns on one claim spends the cap of anyone standing near it, including players on land that allows mobs. Behind entities.spawning.throttle-refused-spawns-per-chunk, off by default, the counter instead lives on the chunk, keyed by mob category. It is charged once per call rather than once per candidate position, only for plugin refusals and never for a vanilla placement failure, and it is cleared as soon as a spawn gets through, so a chunk that a region only partly covers never throttles. A chunk over the threshold is retried on a slower interval instead of every cycle. With the option off nothing is counted and no array is allocated, so a server that does not run plugins cancelling this event is unaffected.
a674e7e to
f5707f5
Compare
|
added |
We run a survival server with 60-80 players online and we had a lot of lag. We had already cut down on plugins, so I profiled it, and the plugins were not what was expensive. Mob spawning was, at roughly a quarter of the main thread, while every plugin together came to about 2%.
We use GPFlags to turn off monsters inside claims, and WorldGuard does the same for regions. Both did it by cancelling
CreatureSpawnEvent, which happens at the very end of the spawn cycle, after the mob has been built. Because a cancelled spawn never counts toward the mob cap, the cap never fills and the server keeps building mobs and throwing them away.So we moved the check earlier, to
PreCreatureSpawnEvent. That fixed the waste but broke something else. A player with a farm on a claim that allows monsters, ringed by claims that do not, stopped getting spawns. Every cancelled pre-spawn is charged to a per-player backoff counter that is added to the mob cap of everyone within simulation distance. He had 4 to 7 real mobs and was counted as 19 to 27 against a limit of 18.The counter exists because of the claim, but it is booked against the player and follows him around. It also cannot be avoided by only denying whole chunks, because neither GriefPrevention claims nor WorldGuard regions are chunk aligned.
This moves the counter onto the chunk instead. A chunk that keeps being refused is retried on a slower interval. The counter clears the moment a spawn gets through, so a chunk that a region only partly covers never throttles. Vanilla placement failures are not counted, only plugin refusals, so a server without plugins that cancel this event behaves exactly as before and never even allocates the array.
Measured on a test server with a 65x65 area that allows monsters surrounded by 224 small claims that do not, killing mobs continuously so the cap never fills, three alternating runs:
Spawns are unchanged and about 72% of the wasted constructions are gone.
Most of my measuring was on a flat world, which exaggerates spawn attempt rates, so I would read the numbers as a direction rather than as absolutes.
The threshold and interval are constants for now. I am happy to put them behind config if you would rather, I just did not want to guess at the shape.
I used AI tooling to investigate the spawn code and to help write the patch. The problem, the measurements and the reasoning are mine, and I am happy to answer questions about any of it.