More efficient?

Discussion in 'Plugin Development' started by Chinwe, Jul 8, 2013.

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

    Chinwe

    Hey :>

    Something that I've wondered: is it more efficient to retrieve a value from a .yml everytime I need it (eg. if (config.get("someBoolean"))) or to save all values from the config to separate variables/a map in onEnable() from which I get the values?

    Or would it not make much difference, only inconveniencing server admins who have to /myplugin reload to update their changes to the config.yml?
     
  2. Offline

    mcoder

    Saving the config values in the plugin on startup is the better way to go. If you have a lot of config options, you should even consider creating a seperate class for it, somewhere along the lines of a ConfigHandler.

    To server admins it won't make a difference, since the getConfig() method doesn't reload the config from the actual file, but the object of it in java. To reload the config from file, you have to use the reloadConfig() method.

    regards, mcoder
     
  3. Offline

    Compressions

    chinwe I would recommend saving to a map, though it wouldn't make much of a difference.
     
  4. Offline

    Chinwe

  5. Offline

    desht

    It depends. If you need to access a particular config value very frequently (e.g. in a tight loop), it's a wise idea to cache it. If you only access it occasionally, there's no real benefit.

    E.g. if you're iterating over a 100x100x100 cube of blocks to find blocks with an ID from your config file, calling getConfig().getInt("wantedID") a million times is a pretty dumb thing to do. In that case, get the config item into an int before the loops start, and use that cached value instead.

    Also, for frequently-called event handlers (PlayerMoveEvent and BlockPhysicsEvent spring to mind), it's a good idea to cache config items that those event handlers might use. You need to be careful about keeping those cached values up to date, however (ensure they're refreshed whenever the config file is reloaded, or perhaps changed by a plugin command).
     
Thread Status:
Not open for further replies.

Share This Page