r/Clojure • • Feb 20 '21

shadow-arborist: Braindump

Lately I find myself trying to think about too many topics at once and my productivity has tanked in the process. I do think these topics are worth thinking about but I can't do them all at once on my own.

The first 2 are about component reusability and async rendering and I might make a few more in the upcoming weeks. I didn't want to turn this into a whole blog series and mostly wrote this down for myself. I'm only posting them here because people previously have expressed interest in my work on shadow-arborist and shadow-grove and might be interested in this. Currently that work lives in the shadow-experiments repo.

For those that missed it: shadow-arborist is my attempt at writing a react alternative in pure CLJS with no other dependencies. shadow-grove is the higher level "component library" on top. It would be fair to compare them to reagent being built on top of react. Sorry about the names, everything is somewhat related to trees.

In those posts I just mostly talked about the stuff that already exists and decisions I made in the process. Writing about the actual unformed ideas that I haven't settled on is difficult. I hope to get everything out of my head at some point so I can get back to focusing on specific issues that actually need solving.

Edit: Wrote another post.

72 Upvotes

24 comments sorted by

4

u/lilactown Feb 22 '21

I've been thinking about these same questions for a long time too, /u/thheller - great to read your thoughts. I too am wondering about what comes after React. I'd like to try and do a brain dump too and then compare them with you :)

One thing at top of mind - w.r.t. scheduling, data loading, state, etc. there is a great amount of importance placed on the "component"; I wonder what you think of building out a graph of data similar to reagent's ratom/reaction and implementing scheduling in that graph of computations. Could we get away with allowing "components" be dumb things which subscribe to leaves of this graph and return or mutate DOM elements? Would that simplify some of our concerns?

I think what this leaves on the table are local UI state (e.g. text input, checkbox state) but it makes the "view" layer potentially easier to reason about.

FWIW I'm pretty bought into the component model but I've been thinking about experimenting more in this area. Some benefits I see are opening up other use cases, e.g. splitting computations between server and client w/o the need to serialize the DOM, and being useful for other applications of incremental computation.

1

u/thheller Feb 23 '21 edited Feb 23 '21

Components themselves just manage state and how it relates to the DOM they produce. This needs to be optimized for dealing with the DOM parts so event handling is efficient and so on. They are themselves "passive" and only react to events.

The source of the events is however dynamic. They either come from the DOM or any other hook. This design works well. Local state is just as straightforward as more complex things like the EQL.

The problem lies within with "wiring" of the events and how you limit a) the amount of events created and b) the amount of computation they do. I totally agree with your talk on EQL and the power of just putting attributes in a query and letting the engine figure out how to compute it. Instead of resolvers I opted for a simpler mechanism of just a multi-method for performance reasons. The declarative nature on the view side is wonderful. It would be lovely if the pathom model could get fast enough. It already might be if you don't obsess over performance as much as I do but I haven't tested in a while. It also is optimized for other things that make sense for those use-cases and not UI issues.

The reagent ratom/reaction model is also trivial to wire up in the components. The underlying graph however has the same issues. I do prefer the EQL model of just having one query instead of re-frame subscribe to several different things. Ultimately what should make it into the component is the signal that its data changed and that it needs to re-render. Optimizing that is the goal.

In my current implementation there are things that result in the absolute worst case performance. Unfortunately those are also things that will occur quite frequently in regular apps. Take the benchmak I made which lists items in a table and you can click to select a row. The engine will recognize that the attribute accessed the ident of the item and the ::selected key. So whenever this changes the query recomputes. With 1000 mounted rows that does a lot of work. No rendering occurs in 999 (or 998) cases but running the query alone takes too much time.

EQL isn't the source of this performance problem. The re-frame model has the exact same drawback but it is more efficient since it just does a (get db ident) for the item data and (get db ::selected) (simplified) and hands the result to the component or other sub. So it saves the select-keys step the EQL currently does basically. This might be required for performance reasons.

2

u/thheller Feb 23 '21

I tweaked the benchmark to computing the "selected" state in the actual ::select! event instead of computing it in the query dynamically.

This takes the "select row" benchmark from 1,723.6 to 79.6. A very significant speedup with very minimal code change.

The is the "push" vs "pull" difference I referred to my post. Knowing that you are going to need some data and computing it in the correct place can make all the difference.

12

u/bowmhoust Feb 20 '21

Thanks for doing this kind of pioneering work, Thomas.

1

u/den1k Feb 21 '21

u/thheller how does the << macro compare to https://github.com/lynaghk/hicada ?

1

u/thheller Feb 21 '21

No. hicada compiles to react elements and attempts to compile regular CLJS forms such as let, for, etc.

The shadow-arborist fragment macro does not attempt to do that at all. It stops "compiling" whenever it runs into anything not a static hiccup form. Thats why you need nested << calls. I opted to have it always work that way instead of just "sometimes" since there is too much ambiguity otherwise.

1

u/lonelyfroginventor Feb 21 '21

What's your opinions on crankjs's approach for async rendering and on memoized dom as alternative to virtual dom?

1

u/thheller Feb 21 '21

I have not looked at crankjs in detail. From the docs it looks like regular old VDOM style just with some new generator magic for components.

1

u/dustingetz Feb 22 '21

For others: https://crank.js.org/guides/getting-started

it appears to be using async yield with JSX to model a dataflow like rendering construct; its pretty interesting but why do they need both yield and JSX? I guess it's yielding new view values in response to state changes, and then the JSX handles optimized rendering, I wonder if yield can do both

1

u/jackdbd Feb 25 '21

Did you have a look at Marko.js?I don't know why it's virtually unknown in the JS ecosystem, but as far as I know it's the only "framework" that supports async rendering with out-of-order flushing. I wrote framework in quotation marks because Marko, like Svelte, is really a language with its own compiler.

To be precise, Marko.js performs async rendering and HTML streaming only when it renders on a Node.js server, I guess because parsing streams client-side would be too much work for the browser. I think that for now they are still using virtual DOM diffing when rendering client-side, but this is going to change soon. Apparently they want to introduce some reactive primitives whose dependencies are known at compile time. And they are writing their own runtime to manage them. As far as I understood, this will allow them to do fine-grained updates and sub-component hydration.

See here for more details: https://medium.com/swlh/fluurt-re-inventing-marko-db265f07cc45 (Ryan Carniato is the author of Solid.js).

I have tried a lot of UI frameworks last year, but it's only when I read this article about Marko that I heard about progressive HTML rendering (even if it's a feature browsers have been supporting for a long time).

In Node.js you can stream HTML to the browser. And streaming is a pre-requisite for progressive HTML rendering. The Node.js server flushes out a HTML page in multiple chunks and sends them asynchronously to the client, so the browser can start painting the chunks as soon as they arrive.

A few articles/talks I found interesting about progressive HTML rendering:

2

u/thheller Feb 25 '21

I have not looked at marko much. Most of its "magic" seems to be happening in some bastardized mix of HTML+JS which is not something I'm interested in.

I will personally also not run any CLJS/JS code server-side anytime soon, so this entire "isomorphic" area doesn't interest me. I do have some ideas how to combine server-rendering in CLJ with all of this but nothing written on that front.

1

u/rufusthedogwoof Feb 20 '21

Thank you for your contributions.

Also, I loved your elements of clojure book. For me the first chapter was one of the best & brain-bending chapters I’ve come across in recent memory on programming and systems building.

4

u/thheller Feb 20 '21

I have never written or contributed to any books so you must have me confused with someone else. :)

3

u/pihkal Feb 20 '21

Elements of Clojure is by Zach Tellman, creator of Aleph and Manifold.

4

u/rufusthedogwoof Feb 20 '21

Yup, sorry for the mistake and thanks for the correction.

1

u/pihkal Feb 20 '21

No worries!

-5

u/[deleted] Feb 20 '21

[removed] — view removed comment

4

u/pihkal Feb 20 '21

bad bot

1

u/B0tRank Feb 20 '21

Thank you, pihkal, for voting on Adventurous_Fortune8.

This bot wants to find the best and worst bots on Reddit. You can view results here.


Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!

-4

u/[deleted] Feb 20 '21

[removed] — view removed comment

4

u/pihkal Feb 20 '21

bad bot

2

u/slifin Feb 23 '21

This might be of interest:

I'm hoping there will be a CLJS framework for this but at the moment some of the work requires changes to JS bundlers, personally, I'd prefer to be using shadow-cljs as my Webpack equivalent for this feature

0

u/[deleted] Feb 20 '21

[deleted]

4

u/thheller Feb 20 '21

I have seen almost everything the react team published and lots of that definitely informed a lot of decisions in my work. You do however have to consider that a lot of what they are doing only matters at facebook-scale and is maybe overkill for regular sized sites. I'm also doing this alone so far so I have to cut some corners. ;)

solid seems to employ the nowadays common trick of cloneNode(true) to perform a deep clone of the HTML "template" element. This will often be faster since it is one call going directly into native code. Currently shadow-arborist instead creates all elements via createElement and then setting attributes and so on. I have not experimented with cloneNode yet since earlier versions of shadow-arborist wouldn't have supported it. Nowadays it could and I might try it at some point. I haven't looked at the rest of the implementation but I expect it to be somewhat similar. It'll however lack the extensibility because it isn't built on protocols. This does however make things slower overall so in the end doing the protocols may not be worth it.

I might try to integrate into this benchmark suite since I'm curious too. Things will never beat the absolute fastest JS implementation since we are still using CLJS datastructures for most stuff and they do carry overhead.

2

u/thheller Feb 21 '21 edited Feb 22 '21

I got curious and implemented the benchmark using the whole "framework". This involves a normalized DB which is queried by EQL. Results are actually better than expected but still nowhere near competitive.

I'll see about adding a lower level version that doesn't use the DB/EQL stuff. The benchmarks that don't touch the DB much look promising.

EDIT: accidentally ran the benchmarks with wrong numbers. 10,000 instead of 1,000. numbers look more reasonable now but still bad. see Screenshot in link above.