Optimizing pieces of code

Discussion in 'Spigot Plugin Development' started by MinecraftMart, Sep 4, 2016.

  1. So I have 2 pieces of code. This is the first one:

    Code (Text):
        @EventHandler
        public void onBlockPhysics(BlockPhysicsEvent event) {
            Block block = event.getBlock();
            if (plugin.getConfig().getStringList("Enabled Worlds").contains(event.getBlock().getWorld().getName())) {
                if (block.getType() == Material.SPONGE) {
                    MadeSpongeAPI.removeSuckables(block);
                } else if (MadeSpongeAPI.isSuckable(block) && MadeSpongeAPI.spongeInRange(block)) {
                    block.setTypeIdAndData(0, 0, false);
                }
            }
        }
    And this one:
    Code (Text):
        public static boolean spongeInRange(Block block) {
            int x = - plugin.getConfig().getInt("Radius");
            while (x <= plugin.getConfig().getInt("Radius")) {
                int y = - plugin.getConfig().getInt("Radius");
                while (y <= plugin.getConfig().getInt("Radius")) {
                    int z = - plugin.getConfig().getInt("Radius");
                    while (z <= plugin.getConfig().getInt("Radius")) {
                        if (block.getRelative(x, y, z).getType() == Material.SPONGE) {
                            return true;
                        }
                        ++z;
                    }
                    ++y;
                }
                ++x;
            }
            return false;
        }
    These pieces of code take up 14% of my server. With reference to spongeInRange();

    Does somebody know if this can be optimized better? Thanks in advance.

    (I did not make this plugin. The developer is inactive and I can not reach him. I am a dev myself so I do understand what it does and I will be able to edit the plugin. I just don't know if this can be optimized right now. and if so how..)
     
  2. Firslty dont try ang get the vaue each time, Create a class to load all the variabled from the config when the server loads, then store it in that method then use that to get the radius,
     
  3. On the second part I would use for-loops instead of while loops.
     
    • Agree Agree x 1
  4. Omnivion

    Patron

    cache your config values
     
    • Agree Agree x 1
  5. Just toyed around a bit and found that you can get a whopping 32% performance boost by doing this
    Code (Text):

    for(int x = -50, z, y; x <= 50; x++) {
        for(y = 10; y <= 50; y++) {
            for(z = -50; z <= 50; z++) {
                ChunkSection section = world.getChunkIfLoaded(x >> 4, z >> 4).getSections()[y >> 4];
                if(section != null) {
                    i += Block.getId(section.getType(x & 15, y & 15, z & 15).getBlock());//counting id sum to compare to bukkit way (validating math)
                }
            }
        }
    }
     
    instead of getTypeId() (which is better, even, than getType())

    Note that I've moved variable declarations into the first loop and redefined them in each nested loop.
    I'm directly accessing the chunk sections to bypass some checks that would otherwise be done and an object creation that passes 3 ******* integers to the next method. Going to a lower level than I have requires reflection, and the timings go back up.
    Obviously what is above is not a plug'n'play solution, so if there's a part of it which you don't understand, let me know.

    Hope I helped!
     
  6. You sure helped me all! Thanks!