performance-view.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <sect1 id="performance.view">
  2. <title>View Rendering</title>
  3. <para>
  4. When using Zend Framework's MVC layer, chances are you will be using
  5. <code>Zend_View</code>. <code>Zend_View</code> is relatively performant
  6. when compared to other view or templating engines; since view scripts
  7. are written in PHP, you do not incur the overhead of compiling custom
  8. markup to PHP, nor do you need to worry that the compiled PHP is
  9. unoptimized. However, <code>Zend_View</code> presents its own issues:
  10. extension is done via overloading (view helpers), and a number of view
  11. helpers, while carrying out key functionality do so with a performance
  12. cost.
  13. </para>
  14. <sect2 id="performance.view.pluginloader">
  15. <title>How can I speed up resolution of view helpers?</title>
  16. <para>
  17. Most <code>Zend_View</code> "methods" are actually provided via
  18. overloading to the helper system. This provides important
  19. flexibility to Zend_View; instead of needing to extend Zend_View and
  20. provide all the helper methods you may utilize in your application,
  21. you can define your helper methods in separate classes and consume
  22. them at will as if they were direct methods of Zend_View. This keeps
  23. the view object itself relatively thin, and ensures that objects are
  24. created only when needed.
  25. </para>
  26. <para>
  27. Internally, <code>Zend_View</code> uses the <link
  28. linkend="zend.loader.pluginloader">PluginLoader</link> to look
  29. up helper classes. This means that for each helper you call,
  30. <code>Zend_View</code> needs to pass the helper name to the
  31. PluginLoader, which then needs to determine the class name, load the
  32. class file if necessary, and then return the class name so it may be
  33. instantiated. Subsequent uses of the helper are much faster, as
  34. <code>Zend_View</code> keeps an internal registry of loaded helpers,
  35. but if you use many helpers, the calls add up.
  36. </para>
  37. <para>
  38. The question, then, is: how can you speed up helper resolution?
  39. </para>
  40. <sect3 id="performance.view.pluginloader.cache">
  41. <title>Use the PluginLoader include file cache</title>
  42. <para>
  43. The simplest, cheapest solution is the same as for <link
  44. linkend="performance.classloading.pluginloader">general
  45. PluginLoader performance</link>: <link
  46. linkend="zend.loader.pluginloader.performance.example">use
  47. the PluginLoader include file cache</link>. Anecdotal
  48. evidence has shown this technique to provide a 25-30%
  49. performance gain on systems without an opcode cache, and a
  50. 40-65% gain on systems with an opcode cache.
  51. </para>
  52. </sect3>
  53. <sect3 id="performance.view.pluginloader.extend">
  54. <title>Extend Zend_View to provide often used helper methods</title>
  55. <para>
  56. Another solution for those seeking to tune performance even
  57. further is to extend <code>Zend_View</code> to manually add the
  58. helper methods they most use in their application. Such helper
  59. methods may simply manually instantiate the appropriate helper
  60. class and proxy to it, or stuff the full helper implementation
  61. into the method.
  62. </para>
  63. <programlisting role="php"><![CDATA[
  64. class My_View extends Zend_View
  65. {
  66. /**
  67. * @var array Registry of helper classes used
  68. */
  69. protected $_localHelperObjects = array();
  70. /**
  71. * Proxy to url view helper
  72. *
  73. * @param array $urlOptions Options passed to the assemble method of the Route object.
  74. * @param mixed $name The name of a Route to use. If null it will use the current Route
  75. * @param bool $reset Whether or not to reset the route defaults with those provided
  76. * @return string Url for the link href attribute.
  77. */
  78. public function url(array $urlOptions = array(), $name = null,
  79. $reset = false, $encode = true
  80. ) {
  81. if (!array_key_exists('url', $this->_localHelperObjects)) {
  82. $this->_localHelperObjects['url'] = new Zend_View_Helper_Url();
  83. $this->_localHelperObjects['url']->setView($view);
  84. }
  85. $helper = $this->_localHelperObjects['url'];
  86. return $helper->url($urlOptions, $name, $reset, $encode);
  87. }
  88. /**
  89. * Echo a message
  90. *
  91. * Direct implementation.
  92. *
  93. * @param string $string
  94. * @return string
  95. */
  96. public function message($string)
  97. {
  98. return "<h1>" . $this->escape($message) . "</h1>\n";
  99. }
  100. }
  101. ]]></programlisting>
  102. <para>
  103. Either way, this technique will substantially reduce the
  104. overhead of the helper system by avoiding calls to the
  105. PluginLoader entirely, and either benefiting from autoloading or
  106. bypassing it altogether.
  107. </para>
  108. </sect3>
  109. </sect2>
  110. <sect2 id="performance.view.partial">
  111. <title>How can I speed up view partials?</title>
  112. <para>
  113. Those who use partials heavily and who profile their applications
  114. will often immediately notice that the <code>partial()</code> view
  115. helper incurs a lot of overhead, due to the need to clone the view
  116. object. Is it possible to speed this up?
  117. </para>
  118. <sect3 id="performance.view.partial.render">
  119. <title>Use partial() only when really necessary</title>
  120. <para>
  121. The <code>partial()</code> view helper accepts three arguments:
  122. </para>
  123. <itemizedlist>
  124. <listitem><para>
  125. <code>$name</code>: the name of the view script to render
  126. </para></listitem>
  127. <listitem><para>
  128. <code>$module</code>: the name of the module in which the
  129. view script resides; or, if no third argument is provided
  130. and this is an array or object, it will be the
  131. <code>$model</code> argument.
  132. </para></listitem>
  133. <listitem><para>
  134. <code>$model</code>: an array or object to pass to the
  135. partial representing the clean data to assign to the view.
  136. </para></listitem>
  137. </itemizedlist>
  138. <para>
  139. The power and use of <code>partial()</code> come from the second
  140. and third arguments. The <code>$module</code> argument allows
  141. <code>partial()</code> to temporarily add a script path for the
  142. given module so that the partial view script will resolve to
  143. that module; the <code>$model</code> argument allows you to
  144. explicitly pass variables for use with the partial view.
  145. If you're not passing either argument, <emphasis>use
  146. <code>render()</code> instead</emphasis>!
  147. </para>
  148. <para>
  149. Basically, unless you are actually passing variables to the
  150. partial and need the clean variable scope, or rendering a view
  151. script from another MVC module, there is no reason to incur the
  152. overhead of <code>partial()</code>; instead, use
  153. <code>Zend_View</code>'s built-in <code>render()</code> method
  154. to render the view script.
  155. </para>
  156. </sect3>
  157. </sect2>
  158. <sect2 id="performance.view.action">
  159. <title>How can I speed up calls to the action() view helper?</title>
  160. <para>
  161. Version 1.5.0 introduced the <code>action()</code> view helper,
  162. which allows you to dispatch an MVC action and capture its rendered
  163. content. This provides an important step towards the DRY principle,
  164. and promotes code reuse. However, as those who profile their
  165. applications will quickly realize, it, too, is an expensive
  166. operation. Internally, the <code>action()</code> view helper needs
  167. to clone new request and response objects, invoke the dispatcher,
  168. invoke the requested controller and action, etc.
  169. </para>
  170. <para>
  171. How can you speed it up?
  172. </para>
  173. <sect3 id="performance.view.action.actionstack">
  174. <title>Use the ActionStack when possible</title>
  175. <para>
  176. Introduced at the same time as the <code>action()</code> view
  177. helper, the <link
  178. linkend="zend.controller.actionhelpers.actionstack">ActionStack</link>
  179. consists of an action helper and a front controller plugin.
  180. Together, they allow you to push additional actions to invoke
  181. during the dispatch cycle onto a stack. If you are calling
  182. <code>action()</code> from your layout view scripts, you may
  183. want to instead use the ActionStack, and render your views to
  184. discrete response segments. As an example, you could write a
  185. <code>dispatchLoopStartup()</code> plugin like the following to
  186. add a login form box to each page:
  187. </para>
  188. <programlisting role="php"><![CDATA[
  189. class LoginPlugin extends Zend_Controller_Plugin_Abstract
  190. {
  191. protected $_stack;
  192. public function dispatchLoopStartup(
  193. Zend_Controller_Request_Abstract $request
  194. ) {
  195. $stack = $this->getStack();
  196. $loginRequest = new Zend_Controller_Request_Simple();
  197. $loginRequest->setControllerName('user')
  198. ->setActionName('index')
  199. ->setParam('responseSegment', 'login');
  200. $stack->pushStack($loginRequest);
  201. }
  202. public function getStack()
  203. {
  204. if (null === $this->_stack) {
  205. $front = Zend_Controller_Front::getInstance();
  206. if (!$front->hasPlugin('Zend_Controller_Plugin_ActionStack')) {
  207. $stack = new Zend_Controller_Plugin_ActionStack();
  208. $front->registerPlugin($stack);
  209. } else {
  210. $stack = $front->getPlugin('ActionStack')
  211. }
  212. $this->_stack = $stack;
  213. }
  214. return $this->_stack;
  215. }
  216. }
  217. ]]></programlisting>
  218. <para>
  219. The <code>UserController::indexAction()</code> method might then
  220. use the <code>responseSegment</code> parameter to indicate which
  221. response segment to render to. In the layout script, you would
  222. then simply render that response segment:
  223. </para>
  224. <programlisting role="php"><![CDATA[
  225. <?= $this->layout()->login ?>
  226. ]]></programlisting>
  227. <para>
  228. While the ActionStack still requires a dispatch cycle, this is
  229. still cheaper than the <code>action()</code> view helper as it
  230. does not need to clone objects and reset internal state.
  231. Additionally, it ensures that all pre/post dispatch plugins are
  232. invoked, which may be of particular concern if you are using
  233. front controller plugins for handling ACLs to particular
  234. actions.
  235. </para>
  236. </sect3>
  237. <sect3 id="performance.view.action.model">
  238. <title>Favor helpers that query the model over action()</title>
  239. <para>
  240. In most cases, using <code>action()</code> is simply overkill.
  241. If you have most business logic nested in your models and are
  242. simply querying the model and passing the results to a view
  243. script, it will typically be faster and cleaner to simply write
  244. a view helper that pulls the model, queries it, and does
  245. something with that information.
  246. </para>
  247. <para>
  248. As an example, consider the following controller action and view
  249. script:
  250. </para>
  251. <programlisting role="php"><![CDATA[
  252. class BugController extends Zend_Controller_Action
  253. {
  254. public function listAction()
  255. {
  256. $model = new Bug();
  257. $this->view->bugs = $model->fetchActive();
  258. }
  259. }
  260. // bug/list.phtml:
  261. echo "<ul>\n";
  262. foreach ($this->bugs as $bug) {
  263. printf("<li><b>%s</b>: %s</li>\n", $this->escape($bug->id), $this->escape($bug->summary));
  264. }
  265. echo "</ul>\n";
  266. ]]></programlisting>
  267. <para>
  268. Using <code>action()</code>, you would then invoke it with the
  269. following:
  270. </para>
  271. <programlisting role="php"><![CDATA[
  272. <?= $this->action('list', 'bug') ?>
  273. ]]></programlisting>
  274. <para>
  275. This could be refactored to a view helper that looks like the
  276. following:
  277. </para>
  278. <programlisting role="php"><![CDATA[
  279. class My_View_Helper_BugList extends Zend_View_Helper_Abstract
  280. {
  281. public function direct()
  282. {
  283. $model = new Bug();
  284. $html = "<ul>\n";
  285. foreach ($model->fetchActive() as $bug) {
  286. $html .= sprintf(
  287. "<li><b>%s</b>: %s</li>\n",
  288. $this->view->escape($bug->id),
  289. $this->view->escape($bug->summary)
  290. );
  291. }
  292. $html .= "</ul>\n";
  293. return $html;
  294. }
  295. }
  296. ]]></programlisting>
  297. <para>
  298. You would then invoke the helper as follows:
  299. </para>
  300. <programlisting role="php"><![CDATA[
  301. <?= $this->bugList() ?>
  302. ]]></programlisting>
  303. <para>
  304. This has two benefits: it no longer incurs the overhead of the
  305. <code>action()</code> view helper, and also presents a more
  306. semantically understandable API.
  307. </para>
  308. </sect3>
  309. </sect2>
  310. </sect1>
  311. <!--
  312. vim:se ts=4 sw=4 et:
  313. -->