title: Is there a way of rendering custom tags asynchronously (multithreaded)
body: <p>In our Grails application we use CMS to provide GSP templates for our application. The GSP templates contain some custom tags that (and I appreciate that) break the MVC paradigm - they're actually mini-MVCs, they get their data and then render some template(s). In a pseudo code it could look like:</p>
  
  <pre><code>def customTag1 = {attrs, body -&gt;
     def data = service.getData(attrs)
     out &lt;&lt; render (template: 'template1', model: data)
  }
  </code></pre>
  
  <p>Now that's all right with us and we can live with that. However we've got a lot of these tags on our pages and for some of the tags it can take several milliseconds to get their data. And as the tags are executed synchronously (one by one) the total time to render a page is the sum of all calls (plus small margin for the actual rendering). </p>
  
  <p>What we try to do is to come up with a way of executing the tags in parallel (asynchronously). </p>
  
  <p>So far we've come up with a couple of ways how to do it but none of them really work for us. </p>
  
  <p>Firstly we try to delegate the model retrieval to promises, then in a filter wait for their fulfillment and then do the rendering of the tags. But it fails on the race condition on <code>GroovyPageOutputStack</code> as the tags are accessing it on an ad-hoc basis.</p>
  
  <p>Our second attempt was to do a two phase rendering. In the first run all the custom tags are not really rendering but merely getting their data asynchronously (initiating promises). In the second round, once the promises are completed, we do the real thing and render the page. However the performance penalty of running the rendering twice seems to be too high.</p>
  
  <p>In our last attempt we would like to try to duplicate all the custom tags by our CMS (kind of pre-processing) that would allow us to collect the data asynchronously and then, when the data is ready, do the rendering. Again, in the pseudo code it could like:</p>
  
  <pre><code>&lt;a:async&gt;
      &lt;c:customTag1 attr1="1" norender="1"/&gt;
      &lt;c:customTag2 attr2="2" norender="1"/&gt;
  &lt;/async&gt;
  &lt;c:customTag1 attr1="1"/&gt;
  &lt;c:customTag2 attr1="2"/&gt;
  </code></pre>
  
  <p>This way we would do the heavy-lifting first and the rendering once all the data is available.</p>
  
  <p>Anyway, do you think there's a better way of executing certain tags asynchronously? So far it looks like no one anticipated such scenario - the GSP rendering looks, as far as I can tell, very single threaded (some threadlocals, the use of one <code>GroovyPageOutputStack</code> and so on).</p>
  
  <p>All help/ideas will be highly appreciated!</p>