performance-view.xml 15 KB

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