performance-view.xml 14 KB

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