LilasCorner / jlootbox

Other
1 stars 0 forks source link

updateThreshold should use static instances of ProbAdjuster #18

Open JohnTMurphy-NIU opened 2 years ago

JohnTMurphy-NIU commented 2 years ago

The code for the updateThreshold method currently includes the following lines:

` switch(decisionStrat) { case PRICE:

            ProbAdjuster prob;

            if (priceDiff < 0) 
                if(rarityDiff < 0)  prob = new ProbAdjuster(q4); //fourth quadrant
                else                prob = new ProbAdjuster(q1); //first quadrant
            else 
                if(rarityDiff < 0)  prob = new ProbAdjuster(q3); //third quadrant
                else                prob = new ProbAdjuster(q2); //second quadrant

            deltaProbb =  prob.getAdjustmentwBoost(rarityDiff, priceDiff, (double)newLoot.getRarity());
            break;

`

The q4, q1, q3, and q2 variables are static instances that are declared and initialized when the class is loaded. You have added a constructor to the ProbAdjuster class that takes another instance of ProbAdjuster and (essentially) copies it. However, all of this adds overhead to the code. Instead of using the q4, q1, q3, and q2 variables as templates that get copied every time, you can just use those instances:

` switch(decisionStrat) { case PRICE:

            ProbAdjuster prob;

            if (priceDiff < 0) 
                if(rarityDiff < 0)  prob = q4; //fourth quadrant
                else            prob = q1; //first quadrant
            else 
                if(rarityDiff < 0)  prob = q3; //third quadrant
                else            prob = q2; //second quadrant

            deltaProbb =  prob.getAdjustmentwBoost(rarityDiff, priceDiff, (double)newLoot.getRarity());
            break;

` Because constructing and destroying objects is expensive, this should improve performance. As long as these objects are the same for all players, and as long as they do not change during the simulation, you can reuse the instances instead of creating new ones for every player, every tick. (Note that there is some Java magic going on: the variable 'prob' is basically a pointer here, and it can be set to point to the existing object. Java doesn't really allow pointers, but it acts like it does sometimes.)