Solved Performance of config files VS Java Objects

Discussion in 'Spigot Plugin Development' started by LoneDev, Jan 15, 2020.

  1. LoneDev


    Is it better to cache attributes into java objects (creating classes) or reading them from plugin config.yml file?

    I want to know if performance will change or not


    #1 LoneDev, Jan 15, 2020
    Last edited: Jan 15, 2020
  2. SteelPhoenix


    reading/writing to file and I/O operations in general take WAY longer than accessing cached memory.
    • Agree Agree x 1
    • Useful Useful x 1
  3. LoneDev


    Isn't spigot builtin files class caching yml files content?
  4. The only file that is kept in cache is the default config.yml, however, it isn't always up to date with the actual file and that's why you have to call #saveConfig and #reloadConfig which are IO operations (slow)
    • Useful Useful x 1
  5. LoneDev


    Interesting.. then I'll recode my logic to load config attributes in Java objects, since these IO operations would be called a lot as I call them on interact events and such.

  6. Configs are for the most part cached, it depends on how you store and access your configuration objects though. If you use the config object embedded in your javaplugin instance, it'll stay cached. If you have a file manager which stores your config instances, it'll stay cached. If you initialise the yamlconfiguration every time you need it, obviously that won't stay cached.

    Spigot loads all the data from the file into a configuration object (think of it as a glorified map), and it'll stay in memory so long you keep referencing the same object. It'll only perform IO when you update said configuration with the hard values in the file, or save your values in memory to file.

    Regarding the original question, while spigot's configuration's are technically objects, they're not what you should be using. Performance is negligible, but readability and maintainability will greatly improve by mapping your config's to your own data objects.
    • Informative Informative x 3
    • Useful Useful x 1
  7. LoneDev


    Thanks for the suggestion and detailed info, I'll remap everything into objects in order to gain more code reusability
  8. I generally use Maps when I have large amount of configuration. The biggest issue that arises when using maps is that order is not saved, so if you try to save you map back into config you'll find that your keys may become out of order.

    If your problem is solved make sure to mark it as solved
  9. HDD: ~150 MB/s
    SSD: ~500 MB/s (fast ones)
    RAM: ~5 GB/s
    If you'll use the data a lot, it will be Cached (L3-L1). In that case, maximum speed.
    Approximative comparison using Objects vs I/O: CPU-Speed x number of CPUs (if we assume that every object is inside the L1 Cache and everything is done by the CPU; in other words, no graphic card or other dedicated hardware is involved) for the example of 2.5 GHz 8-Core CPU:
    2.5 x 8 = 20 GHz compared to 500 MHz => 40 x faster.

    This is a very rough approximation, but you can say that (for this simple example) using actual objects can be up to 50x faster (again, not taking into account any special HW as well as parallel execution by the cores and so on)
    • Informative Informative x 1
  10. A LinkedHashMap will preserve the order, and I'm pretty sure that's the map implementation spigot's configuration system actually uses.
  11. LoneDev


    In other words:
    Performance would be the same as Spigot caches YamlConfiguration on its load, but it's better to wrap that around objects to maintain a more stable codebase and reusability
  12. This post on the LinkedHashMap may be helpful to some people.
  13. Not the same; using your own objects would be faster, but basically unnoticeable.
  14. There’s a marginal benefit to flat mapping on your own if you are using long config keys or if you’re doing lookups very often to avoid key parsing overhead.
  15. LoneDev


    Yes, will also lead to less bugs and mistakes on keys writing.

    Thanks all, I'll remap everything to objects
  16. the yml cached configs are made using nested hashmaps which well is much slower than just getting the value directly from an object
    • Informative Informative x 1