Meanwhile, in spare minutes (and during sessions at ApacheCon), I've been continuing to work on Cascade. It's been a great learning exercise for me, pushing my understanding of both Clojure and functional programming in general ... and especially, some pretty advanced meta-programming with macros.
I'm also using Cascade as a kind of test bed for ideas that will eventually appear in Tapestry.
Not everything turns out exactly as I've planned. For example, I've been very excited about invariants, portions of the rendered DOM that could be cached from one request to another, to speed up the rendering. Like Tapestry, Cascade views render a DOM structure which can be manipulated (in an intermediate stage) before being streamed as text. This is a useful and powerful concept in a number of ways.
My thinking has been that a typical view will contain sections of the template that are invariant: unchanging, and that there would be a benefit to building that sub-section of the DOM once and reusing it efficiently in later renderings of the view.
Clojure template forms are processed by macros to become normal Clojure code. Thus something like (template :p [ "Hello" ])
will be transformed into code, approximately (element-node :p nil (combine (text-node "Hello")))
. My approach was to tag the new Clojure code forms (the list consisting of element-node
, :p
, etc.) with meta data to identify it as invariant. Eventually this would propagate up to a higher level and code to manage a cache would be wrapped around it: (or (read-view-cache 97) (update-view-cache 97 (element-node :p ...
Fun stuff ... until I put it into practice (after a fair amount of debugging) and discovered that in the views I've created so far (for testing purposes), the number of nodes that can be cached is low; any use of a symbol or a function call mixed into the template "taints" it as variant. I wasn't set up to do performance measurements, but my gut feeling is that the overhead of managing the caches would overshadow the savings from the small islands of reused DOM nodes.
Back to Cascade as a learning experience: just because this didn't work out doesn't mean I didn't learn a lot from heading down that direction, and certainly the amount of code it took was quite small. I have it stored in a branch in case I ever want to give it another shot.
I will have all the basic features of Cascade implemented pretty soon; I'm looking forward to seeing what the larger Clojure community makes of it. In the meantime, it has served as a great way for me to dig deep into Clojure ... I'll be putting together more sessions for NoFluffJustStuff and more articles for the companion magazine based on all this.
1 comment:
Hi, in Enlive, I implemented invariant caching but kept the cache local to each node:
* first I annotate the DOM with specialized renderers (see http://github.com/cgrand/enlive/blob/master/src/net/cgrand/enlive_html.clj#L169),
* then, when serializing, I call these specialized emitters if present (see http://github.com/cgrand/enlive/blob/master/src/net/cgrand/enlive_html.clj#L127).
Post a Comment