Chicken-Bones / NotEnoughItems

MIT License
130 stars 127 forks source link

Ender IO #289

Open K4N0 opened 8 years ago

K4N0 commented 8 years ago

Magnet mode in NEI seems to work great in survival and causes no lag. Ender IO magnet seems to cause massive Server lag and dupes xp at times.

Any way you can help? https://github.com/SleepyTrousers/EnderIO/pull/2920#issuecomment-150046234

Chicken-Bones commented 8 years ago

NEI magnet mode in SMP just applies some simple acceleration to items near players every tick. It also keeps track of the items 'picked up' by each player, It notifies the client of the items that are picked up and dropped (via their entityID) so the client can perform the same calculations

K4N0 commented 8 years ago

Thank you. I love your mods. They are always so well done. Specially Translocators.

K4N0 commented 8 years ago

HenryLoenwind commented an hour ago Now there seems to be a misconception about lag.

Lag is caused when a computation is taking too long. That can be because some item/block/entity does stupid things, like counting the amounts of lava blocks in the nether. That kind happens all the time, because many mod authors are not professional game coders. However, that is not what happens here.

It can also happen if there just is too much stuff around. So in vanilla, if you drop 1,000,000 items on the ground, the game will lag to a standstill. Because it not only has to render them all, but because it has to move all those item entities, check their collisions with players and blocks, and so on. This is generally not a coding error, but just simple overload.

Now, what does the Ender IO magnet do?

(1) It asks Minecraft for all item entities within a configured radius around the player. (2) It then changes the velocity and speed of those item entities to give the illusion that they are being pulled towards the player. (3) For item entities that are near the player, it tells them to check their collision with the player. (That is needed after every entity movement.)

That's it. (2) consist of 9 additions and 5 multiplications per item. That is about a lightweight a computation as you can get. Now, you asked to add "compare every item to every item in the player's inventory"---that is a massive piece of computing. It consists of up to 40 checks, each one comparing an itemstack (which has an item, a stack size, a stack size maximum, and for many items NBT values). We are talking about something that can takes several tens of thousand times longer to compute.

The PR above changes step (1). It limits the size of the list that will be produced by vanilla Minecraft for the magnet. Minecraft will still loop over all items in range, but it won't put them into a list. That saves a bit of time, but more importantly, a good chunk of memory. It will also limit the number of times step (2) has to run, but that nearly doesn't matter at all. That it limits the number of times step (3) is run however, does. That step already is doing the inventory check to see if an item can be picked up. Quite a good saving there.

Chicken-Bones commented 8 years ago

That looks quite similar to what NEI does, perhaps even more performant. I'd suggest doing some actual profiling

K4N0 commented 8 years ago

hmm very odd. maybe there is a hidden bug somewhere. i'm very new to coding. The only thing i know is that your nei magnet does not cause the server to lag out and their magnet does. The server gets so much lag everyone on it gets timed out after a short block and inventory lag spike. I have 16gb of ram and none of the ram or cpu spike. very odd. i just learned how to use VisualVM. I'll put that to work. is 20k classes normal to run?