TileEntities and their issues.

Discussion in 'Mod Development' started by Elder Jürd, Jan 16, 2015.

  1. Elder Jürd

    Elder Jürd New Member

    Hello FTB community.

    As a modder, now i am confused, mainly because what everyone says, and is true, tileEntities are the solution and the problem. So, to be a good modder i need to avoid using tileEntities as much as i can. OK! no problem... although...

    I suppose tileEntities that do not tick cause less lag, isn't it?

    let's say i got 1024 tileEntities, how will that affect the performance of...?

    - normal gameplay?
    - chunk loading-unloading?


    - they don't tick and store a lot of variables?
    - they tick and store few variables?

    As your guys may eb experienced on this, you might help me A LOT in the way i may program my features.

    Thank you guys for your time.
  2. McJty

    McJty Over-Achiever Mod Developer

    Normally a block in the world can only store the block id that is being used (i.e. cobble, dirt, ...) and 4 bits of metadata. That metadata is often used for color (stained hardened clay) or orientation (in case of chests).

    That's all there is. If you need to store more then that then you *have* to use tile entities.

    It is not a matter of performance. It is a matter of having a place to store your stuff. Chests, furnaces, ... machines all need tile entities.

    A tile entity that doesn't 'tick' doesn't cause performance issues with regards to cpu usage. But of course tile entities do consume memory and make loading the chunk back take a little bit longer.

    But don't worry. Use a tile entity if you need to store more information then what you can store in 16+4 bits.
    Elder Jürd likes this.
  3. Elder Jürd

    Elder Jürd New Member

    Thanks for the quick answer.

    Right. Knowing this, in my mod i have developped a series of trees that generate an insane amount of tileEntities that tick and store up to 20 variables, to make trees grow in real time.

    I need feedback on this but as far as i know, because they only do things once each 2 min, i though that would reduce the performance issues. It's been working good for up to 10 or 20 trees. Afterwards, it starts to hit performance when loading-unloading chunks, although the gameplay is not affected.

    edit: if you look at the mod page in my signature you will understand XD
  4. McJty

    McJty Over-Achiever Mod Developer

    How many tile entities for a single tree?

    There may be other ways to solve this. You can for example store data per dimension which is unrelated to blocks or chunks. Of course if you do that you would have to do your own management of that data and that is a bit more complicated.
  5. Elder Jürd

    Elder Jürd New Member

    depending on the time i let him divide, goes to 1024+;

    i was thinking on a single tile entity that ticks, wich checks the path made with tileEntities that don't tick.

    i'm not affraid of programming. But i lack some java for now.
  6. ratchet freak

    ratchet freak Well-Known Member

    I don't really see anything with the trees that need TEs; your machines do if you want to store the inventory (fun fact: the vanilla crafting table isn't a TE)

    but the trees can be done using just the 4bits of meta data and a different ID for each block
  7. Elder Jürd

    Elder Jürd New Member

    These trees that are constantly updating and feature some generation changes need more than 4 bits. They are quite complicated.

    edit: and that's why the crafting table cannot store its contents xD
  8. ratchet freak

    ratchet freak Well-Known Member

    it could then all be stored in a single "root" TE in the root of the tree that is the only one ticking,
  9. Elder Jürd

    Elder Jürd New Member

    that's what i thought :D

    although i wanted to ask because i got an idea that needs tileentities that don't actually tick, but store the coordinates of the root tile so they can know who created them. Is that affordable for my mod?

    edit, also they may have some functions that will be fired by the root tile
  10. ratchet freak

    ratchet freak Well-Known Member

    if you don't need the storage then just put those methods in the block class
  11. Elder Jürd

    Elder Jürd New Member

    Ando how do i store the 3 int for the coordinates?

    edit: i was going to use tiles and NBT
    Last edited: Jan 16, 2015
  12. ratchet freak

    ratchet freak Well-Known Member

    as I said if you don't need the storage.

    otherwise just pass them as parameters from the root TE into the method
  13. GreenZombie

    GreenZombie New Member

    Of course the mere existence of tile entities effects performance.
    The normal arrangement of chunk data means that whenever minecraft needs to know what is at a particular location its a simple array lookup. Any tile entity means that a lookup now has to additionally can a list. So you are trading an O(1) operation with an O(n) operation.
    Once you start scaling to 1000's of TEs, and consider that at 100fps you are now forcing minecraft to scan those lists 100 times per second. And that each time minecraft has to render a chunk it has to perform the scan FOR EACH tile entity, the real render cost of tile entities becomes O(n²) (assuming Mojang hasn't tried to optimise this, as this situation never occurs in vanilla so would not be on their list of improvements to make. *

    And thats not counting tick issues: more than 10s of TEs per chunk does become a problem even if they are passive. Don't.

    * This was stated perhaps more strongly than I intended. I do not *know* that the TE render scan would reduce to O(n²) - I merely suspect it as the probable worse case. Empirical tests or examination of MCs code would be required to see if hashmaps or other steps are taken to attempt to optimise the rendering of large numbers of TEs per chunk)
    Last edited: Jan 16, 2015
    FyberOptic likes this.
  14. ratchet freak

    ratchet freak Well-Known Member

    MC scans rather than using a hashmap?
  15. GreenZombie

    GreenZombie New Member

    My comments were based on observations of the anvil map data structures. Certainly there are ways to optimize the scan but they would all tend to add a large overhead to what should, in the normal case (a small number of TEs) be a quick operation and would require the construction of said hashmaps as each chunk was loaded.

    So yes, O(n²) is a worst case conjecture, based on the observation thats what the published data structures support, and that Minecrafts own developers would never have had the incentive to optimise for the unusual case presented here.
  16. ratchet freak

    ratchet freak Well-Known Member

    but during loading MC could put it in a hashmap mapped to coordinates for O(1) access, it has to be loaded anyway and pushing into a hashset is cheap
  17. Elder Jürd

    Elder Jürd New Member

    That said, i remind you that the performances seems not affected for a pretty large number of entities when you are standing next to them. The problem starts at loading and unloading chunks, otherwise it's not noticeable :/
  18. GreenZombie

    GreenZombie New Member

    Thaumcraft was creating 10k tile entities in a game that had lagged down to 5fps due to high server avg tick. Opis indicated he total time spent ticking them was merely hundreds of microseconds.

    Upgrading to the latest version that replaced them with plain blocks sees 100fps again and a 5ms avg tick.

    The measurable costs were low. The bulk of the impact just seemed to be in having that many tile entities.

    But this discussion is largely moot. Simply ceate some test cases where you generate thousands of TEs and report how it scales the avg tick and FPS. Science can answer this more accurately than opinions.
  19. Elder Jürd

    Elder Jürd New Member

    Totally agreed in everything.

Share This Page