jonathanhogg / flitter

A functional programming language and declarative system for describing 2D and 3D visuals
https://flitter.readthedocs.io
BSD 2-Clause "Simplified" License
34 stars 1 forks source link

What about just throwing away queries? #17

Closed jonathanhogg closed 5 months ago

jonathanhogg commented 8 months ago

Queries are a royal pain-in-the-arse to handle correctly in the language, with annoying side-effects and weird rules around attribute-setting and node appending.

I've not been using them much in my recent work and a glance at the places I've used them in the past (mostly stuff from Let My Country Awake) suggests that those could be replaced with template functions.

Maybe it is time to accept that it was an idea that just hasn't turned out to be as useful as I thought it would be?

jonathanhogg commented 8 months ago

Interesting fact:

There is only one place in the codebase where I use a Node's parent (other than in the tests) and that is in the VM when appending a node to the root to see if it's the result of a query, i.e., an already parented node.

So, if I ditched queries, I could just rip out all of the weak-ref logic for Node._parent.

jonathanhogg commented 8 months ago

Interesting idea:

If I no longer had queries or ._parent, I could change the node-mutating operations (tag, set-attribute, append, insert) to return a "new" Node and make all Node objects notionally immutable. These operations could then check the refcount of the Node and mutate it directly if nothing else has a reference to it or return a copy if not.

If instead of using sibling links in the nodes I used a simple array of pointers in the parent, and without parent links to worry about, this copying could be shallow. So a single literal node could be referenced from multiple places.

This would mean I could save time copying literal nodes in a for loop. For example:

for i in ..THINGS
    !transform translate=uniform(:location;i)[..2]
        !sphere size=5

could use the same !sphere size=5 node in every !transform node.

jonathanhogg commented 8 months ago

Without queries, is it still useful to have tags? They are used only for logging and in the logic for finding nodes when the window scene graph is re-structured, which is a fairly minor optimisation.