kayak_ui icon indicating copy to clipboard operation
kayak_ui copied to clipboard

Improve Event Processing Efficiency

Open MrGVSV opened this issue 4 years ago • 0 comments

The Problem

Currently, when processing events we iterate through every node in the tree using down_iter: https://github.com/StarArawn/kayak_ui/blob/a068b4ab4f9b83ba21a4b73598559987d981c9c3/kayak_core/src/context.rs#L277

And even with #43, we still iterate through every node: https://github.com/StarArawn/kayak_ui/blob/63d62f2ad6c18ca0c3217d0e4c20a11206fd9a9f/kayak_core/src/event_dispatcher.rs#L147-L149

This could be much more efficient if we could smartly recurse down the tree, skipping nodes we don't need to process. Additionally, the only events that actually need to process individual nodes (as of now) are Cursor events. Keyboard and Focus events either happen on a single, known node (the focused node) or are invoked from a Cursor event.

The Issue with Smart Recursion

The main issue with performing a smart recursion down the tree for a Cursor event is that we don't always know if a parent's layout contains a descendant's. For example, a child node may overflow the parent's actual bounding box. For self-directed nodes, the parent could even have no width or height since self-directed nodes don't contribute to a parent's layout.

This is an issue because we can't just recursively select the child node that contains the cursor: we don't know if its descendants will contain it or if its actually within another part of the tree entirely.

A Possible Solution

One possible solution would be to manage a spatial hash of some type, such as a quad-tree, for the nodes. This would cache the layout of every node, allowing fast read-access when determining which nodes contain a specified point. However, it comes at the cost of additional memory overhead. It might also be an issue with animations where a layout could change very quickly in a short period of time, requiring lots of updates to the tree.

Other solutions/suggestions are welcome!

MrGVSV avatar Dec 27 '21 17:12 MrGVSV