Majrusz / MajruszLibrary

Library with common code for my Minecraft modifications.
8 stars 5 forks source link

Lag in gamemodifier context application on all entities every tick #16

Closed LemADEC closed 10 months ago

LemADEC commented 1 year ago

As observed in CottageWitch 1.13.3 with majrusz-library-1919.2-3.3.0, there's a lag issue observed with Spark profiler. This lags affects all entities when handling modifiers every tick. Specifically, handling of modifier takes 5x more CPU than just checking the enchantment level.

Upon code review, we can see objects are created every tick for every entity, which cause memory stress and lag, see: https://github.com/Majrusz/MajruszLibrary/blob/1.19.X/src/main/java/com/mlib/gamemodifiers/contexts/OnEntityTick.java#L26 => consider passing the data without creating a new object?

We can see also a stream object created but hardly used here: https://github.com/Majrusz/MajruszLibrary/blob/1.19.X/src/main/java/com/mlib/gamemodifiers/Contexts.java#L29 => consider exploiting more of that object or code without creating such object?

Profiler show also Stream API filtering is causing copy of objects. => consider removing filtering from Stream API level, or remove Stream API usage altogether?

An additional approach could be considered by "throttling": for example, apply effects only every few seconds since enchants are prone to change at most every few minutes, consequently we could throttle the update to only be done very 100 ticks (5 seconds).

katubug commented 1 year ago

After doing some more testing, it seems it is only happening when the Enchantments mod is added. I ran a profiler with Accessories and Library and the memory leak does not seem to be present.

https://i.imgur.com/gfQe1Sw.png

VallenFrostweaver commented 1 year ago

Can confirm the above.

Majrusz commented 1 year ago

Hi, I had a break from modding recently, so sorry for the late response, but thanks for the report. After I release changes made before my break, I will try to fix this as soon as possible