KyoriPowered / adventure

A user-interface library, formerly known as text, for Minecraft: Java Edition
https://docs.advntr.dev/
MIT License
724 stars 106 forks source link

Component hints #346

Open Electroid opened 3 years ago

Electroid commented 3 years ago

Since a Component is immutable, during construction, we should store some "hints" about it and its children that are accessible in O(1) time. For example, one of the most common algorithms is to traverse a Component tree and translate keys. If we provided a hint for whether the component contained a translatable in its tree, we could fail-fast during translation. This is also applicable for #114, since we can provide a hint if the component is already compacted.

Is the tradeoff of faster time complexity for larger space complexity worth it? If it's done with an EnumSet or BitSet, I think it's manageable. Components can have complex structures, and not everything has a translatable. This could considerably speed up translations.

kezz commented 3 years ago

I think this is a brilliant idea. In Minestom we are heavily bottlenecked by the networking side of things and being able to frame a packet once is a big advantage. Current solutions are to either attempt a translation and check for equality or traverse the entire tree and check for translatable components, both of which are far from perfect solutions.

I agree that this would be a much better solution, an EnumSet containing a collection of flags would be a brilliant idea. Perhaps a flag for translations could go further and be split into vanilla vs. non-vanilla translations, or maybe just "this component has translations that exist in the translator" would go even further in making this a powerful tool.