Bukkit configuration file: create cache or not?

Discussion in 'Plugin Development' started by rsod, May 12, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    rsod

    Hello.
    A big question I have, that really bothering me.
    Can I read value from config rapidly without performance loss?
    Let's say:
    Code:
    public void onMove(PlayerMoveEvent e){
      if(e.getPlayer().getLocation().getBlockX() == getConfig().getInt('RequiredX')){
        ... //do something
      }
    }
    and
    Code:
    int reqX = getConfig().getInt('RequiredX');
    public void onMove(PlayerMoveEvent e){
      if(e.getPlayer().getLocation().getBlockX() == reqX){
        ... //do something
      }
    }
    Will those fragments of code have same performance? Personally, I prefer first one. But I learned that in programming simple solution may take more resources then complicated.

    I have configuration that has more then 500 lines of different strings, integers, doubles and so. I really don't want to define so much variables on the start of the plugin...
     
  2. Offline

    theguynextdoor

    Just did some basic and pretty crude benchmarks. I did sending a message from the config to a player during the player move event and the second one gave a fractional performance increase of about 10,000-30,000 nanoseconds so about 0.00002 (if we take the median) milliseconds.

    So it is nothing anyone will notice. Obviously I don't know what the results would be if this data was extrapolated to the amount you may be using it. I would recommend doing your own benchmarks and seeing (I would but I don't have the time as it is early in the morning/late at night and I should (would rather) be in bed)
     
  3. Offline

    Sagacious_Zed Bukkit Docs

    rsod If you care for performance, you can look at the implementation of YamlConfiguration. This will give you an idea of how quickly information can be retrieved, and if you need a more specialized solution.
     
  4. Offline

    rsod

    theguynextdoor Sagacious_Zed
    thanks for information. I looked at implementation of YamlConfiguration, I didn't really liked it, but I think I can cache most used values, and directly get what I'm using not really often.
     
  5. Offline

    RawCode

    read implementation before trying to "increase" perfrormance.
     
  6. Offline

    Wolvereness Bukkit Team Member

    MemorySection does a string search to find any periods, then does string splitting between them. On Java 6, this is 1 object creation per sub-string, with 2 on Java 7/8. This is followed by a LinkedHashMap lookup for each sub-string (up to x3 when considering defaults), with various casts and checks. I rewrote this method to not use regular expressions just over two years ago.

    References:

    A preferred way to approach this issue is to use a specific object with the appropriate fields. Plugin.getConfigObject().fieldName, but whenever reloadConfig() is called, you swap out the configObject appropriately. This also allows you to make a much more user-friendly config, with a plugin-level translation layer to fit to the logic. However, for most servers, using config.get() is going to be trivial.
     
Thread Status:
Not open for further replies.

Share This Page