Loading values directly from YAML vs. loading values from hashmap

Discussion in 'Plugin Development' started by rogerin0, Jan 28, 2013.

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

    rogerin0

    In my plugin, I'm going to be constantly grabbing values from a YAML configuration using:
    Code:
    FileConfiguration config = getConfig();
    ...then using config.getInt(), config.getBoolean() and so on.

    I was wondering if I could increase the access times to the configuration by loading all values in the YAML configuration into hashmaps (when the plugin is first enabled), then grabbing the values from the hashmap instead of directly from the YAML config.


    Do you think this would be more efficient?
     
  2. Offline

    nasonfish

    As I remember a friend saying a while ago, this isn't a good idea really. It uses up memory storing all the values, and is static and can't be changed without stopping/starting your server, while the config can be changed whenever.

    You _can_ just call getConfig(); once and store it as a field, and then call getConfig() whenever someone runs a command to reload the config if you want, but I don't think using a HashMap is a good approach.
     
  3. Offline

    LucasEmanuel

    How are values stored in a hashmap static and cannot be changed?
     
  4. Offline

    RainoBoy97

    nasonfish
    You can make a scheduler that loops through the hasmap and stores it in the config, and vise versa
     
  5. Offline

    rogerin0

    So... would it be more efficient or not?
     
  6. Offline

    raGan.

    Look at the code: https://github.com/Bukkit/Bukkit/bl...it/configuration/MemorySection.java#L197-L227

    Config also uses hashmap to store its objects, but when you do "config.getSomething()", it will do get method shown in the link and then checks if it is instance of "Something". As you can see, it does more actions than simple hashmap lookup, so it is slower, But if you don't access it very often (and I mean VERY often), it should not make that big difference. I personally use a class with fields that represent every config option, so I don't event need to use hashmap, because I always know what info I need during compile time. For example:
    Code:
    if(DataClass.debugMode) {
        // debugMode is simple static boolean field, it can be easily accessed from anywhere, and is fast
    }
     
  7. Offline

    lDucks

    I load a config file into a hashmap on startup, and then enter data into the hashmap. Every five minutes or so, I save the new or changed data to the files automatically. And on shutdown I save all the data.
     
  8. Offline

    nasonfish

    Sorry, that was a really poor choice of words and phrasing on my part. I guess it almost slipped my mind you could re-put the values in the HashMap, sorry. I was just thinking that they would be in there and wouldn't be changed on reloading the config without some extra code. Sorry again.
     
Thread Status:
Not open for further replies.

Share This Page