<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://blog.invenzzia.org/en/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>Invenzzia... in English - opt2</title>
  <link>http://blog.invenzzia.org/en/</link>
  <description></description>
  <language>en</language>
  <pubDate>Fri, 25 Jul 2008 17:12:15 +0100</pubDate>
  <copyright>Copyright &amp;copy; Invenzzia</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>OPT requirements</title>
    <link>http://blog.invenzzia.org/en/post/OPT-requirements</link>
    <guid isPermaLink="false">urn:md5:8a825db4d71c176b511d8fd4cad97d53</guid>
    <pubDate>Sun, 06 Apr 2008 10:28:00 +0200</pubDate>
    <dc:creator>Zyx</dc:creator>
        <category>Open Power Template</category>
        <category>development</category><category>opt2</category><category>optimization</category>    
    <description>&lt;p&gt;I've found a new entry on the bugtracker recently. The author informed that he gets &quot;Nesting level too deep - recursive dependency&quot; error while running the development examples provided by the newest OPTv2 dev version. I looked at it deeper and found out that he lowered the default nesting level limit 4 times from 64 to just 16. Obviously, this is a quite drastic change and would affect not only OPT, but many other scripts as well. However, I think it is time to take a look at the OPT issues concerning the requirements, because several of them needs a deeper explaination.&lt;/p&gt;    &lt;p&gt;The optimizations implemented in OPT, mostly concern the main parser class and the compiler. The first one is the part that is loaded every time the script wants to use templates, so it must provide huge quality standards. There are no sophisticated algorithms, so the speed improvements are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;To minimize the size of the code.&lt;/li&gt;
&lt;li&gt;To minimize the amount of points that try to access the hard drive.&lt;/li&gt;
&lt;li&gt;To use the fastest elements of the language.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The point is this class must be as light as possible; I would even say: almost transparent for the script.&lt;/p&gt;


&lt;p&gt;The situation is quite different, when it comes to the compiler. It is loaded from time to time, so it can be big and splitted for more files (of course without exaggeration). The key role is played by the limits set in the PHP configuration, because in the pesimistic situations, the compiler runs out of resources to complete the task. Unfortunately, some of the new features cost a lot, and if the users wanted XML tag parsing, they must face the consequences: every such tag requires its own node object in the document tree and they need some preparation tasks to be done. My role is to minimize the time and memory necessary to do this.&lt;/p&gt;


&lt;p&gt;The elements that can be exhausted during the compilation are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The stack&lt;/li&gt;
&lt;li&gt;The memory&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The algorithms used in the compiler operate on a data structure called tree, and the use lots of recursion. It consumes the stack, because the number of currently executed methods and functions grows quite fast. In PHP, the default limit for the stack depth is 64 and if you are going to use OPT, the worst you can do is to lower it to ridiculous values like &quot;16&quot; that would affect not only OPT, but also many other scripts :). However, every recursion has an imperative version, constructed for example by queues. Maybe it does not consume less memory, but at least it does not depend on the limits. Such changes have been applied to many compiler methods so far, but some of them still require my attention, because they are not so trivial:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Processing the tree by the instruction processors&lt;/li&gt;
&lt;li&gt;Expression compilation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the first one the situation is clear: we get a node of some instruction and want to process it. We find its processor and redirect the node to it. It generates some PHP code and decides, what to do with its children. By default, it wants to process them, too. Here comes the recursion and the process starts from the beginning. There are some difficulties. The algorithm is split over many classes, and the programmers may create their own. What is more, some instructions need to start the subnode processing immediately, because their next decision depends on the code generated by the children. Obviously, this eliminates queues. An elegant replacement could be made, if we had an access to the low level programming.&lt;/p&gt;


&lt;p&gt;Let's take a look, how it affects the compilation process. We have a huge template, with the tags nested up to the level 30. OPT with the compiler can take another 10 calls, and in the deepest place we have a complicated expression with brackets. To sum up, the library uses approx. 45 calls from 64 available, which is more than 70% of the limit! I know this case is quite pesimistic and in normal life such HTML documents are very rare, but it shows, how demanding the compilation can be.&lt;/p&gt;


&lt;p&gt;If we can't remove the recursion without ugly tricks which would be never accepted by the instruction authors, how we can get around it? The idea is not to remove recursion completely, but we keep it in some places. I use here the fact that the immediate access to the subnode compilation results is necessary only for some instructions, and for example the HTML tags do not need it at all. If we get a random node from the tree, we have a big chance that it would not need a recursion here. So, why don't we provide a dual version of this algorithm? By default, it is executed with a queue, but for more sophisticated needs, some nodes can switch it to recursion. Now it can happen that a tree with a depth 30 is processed in one single loop, with no recursion! I predict that in normal situations, the algorithm will consume only 3, 4 spare &quot;items&quot; in the stack, which is fully acceptable.&lt;/p&gt;


&lt;p&gt;The second issue concerns the memory usage. It's not so tragic here, but we can notice a significant rise when compared to the OPT 1.x compiler, even with using some patents from that version. The most memory is consumed by the tree. I've measured some single nodes recently and the results were from 2 kilobytes for a single node to 3 kilobytes for a node with one attribute. When the instructions start to add the PHP code to the buffers, the things are getting worse, but it happens only in some nodes in the whole template. We can safe some memory here. The objects use various arrays, which are initialized just in the beginning, for example:&lt;/p&gt;

&lt;pre name=&quot;code&quot; class=&quot;php&quot;&gt;abstract class optScannable extends optNode implements Iterator
{
	protected $subnodes = array();
&lt;/pre&gt;


&lt;p&gt;In most of the nodes, the arrays are empty, so it it no sense for them to exist all the time. I fixed the code so that it creates an array only when it is necessary. From the declarations, the &lt;code&gt;= array()&lt;/code&gt; parts were removed. The effect is quite interesting. On a single array, we safe 400 bytes. The new results are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Single node with no attributes: 1400 b.&lt;/li&gt;
&lt;li&gt;Single node with one attribute: 2350 b.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The additional memory may be safed by releasing the huge data that will not be used anymore, for example the template content.&lt;/p&gt;


&lt;p&gt;What do we have to remember, while using the new OPT, when it comes to the requirement issues:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;With the terribly low limits, the compiler will always crash. I thing 16 megabytes for the script (the default limit) is big enough both for the compiler and the script.&lt;/li&gt;
&lt;li&gt;The best place to run the template parsing is the end of the script. We can delete some unncessary data earlier to free some memory.&lt;/li&gt;
&lt;li&gt;The templates using the inheritance are more demanding, especially when overwriting snippets, because the compiler must keep also the old versions in the memory.&lt;/li&gt;
&lt;li&gt;The quirks mode will consume less memory (simpler tree - no XML tags), but it is not implemented yet.&lt;/li&gt;
&lt;li&gt;The compilation is run once for a long time. During the normal execution, we can forget about the limit issues etc.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I think this note shows, how difficult the library creation can be. The key is to know, what we can use in current place and that there is also the rest of the script which uses the same memory and has its own requirements.&lt;/p&gt;</description>
    
    
    
          <comments>http://blog.invenzzia.org/en/post/OPT-requirements#comment-form</comments>
      <wfw:comment>http://blog.invenzzia.org/en/post/OPT-requirements#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.invenzzia.org/en/feed/rss2/comments/24</wfw:commentRss>
      </item>
    
  <item>
    <title>OPT 2.0.0-dev5</title>
    <link>http://blog.invenzzia.org/en/post/OPT-200-dev5</link>
    <guid isPermaLink="false">urn:md5:33bdfac78deef21d193dfecd8a90f928</guid>
    <pubDate>Fri, 21 Mar 2008 14:38:00 +0100</pubDate>
    <dc:creator>Zyx</dc:creator>
        <category>Open Power Template</category>
        <category>components</category><category>development</category><category>opt2</category>    
    <description>&lt;p&gt;The new development version became available a bit later that I planned, but on the other hand, I managed to add much more new things. The DTD issue is solved, as well as XML prologs, CDATA support and code escaping. The expression compiler is done and the programmers may use now brand new component implementation. This is a significant step towards the first beta release and the end is closer than further. There are only a few instructions to code, the rest is almost completed and works. The compiler, programmers' API and most of the stuff also works. I think of making the first real tests on my home website and creating the Open Power Forms library compatible with the new OPT.&lt;/p&gt;    &lt;p&gt;In the new OPT, I've redesigned the component design from scratch. It is much more flexible now and allows to make easily many interesting effects. The base rules are still the same, but the rest has changed. This is a sample code showing, how the component-based work with the forms is going to look like:&lt;/p&gt;

&lt;pre name=&quot;code&quot; class=&quot;xml&quot;&gt;&amp;lt;opt:snippet name=&amp;quot;componentView&amp;quot;&amp;gt;
  &amp;lt;com:div class=&amp;quot;field&amp;quot; invalid=&amp;quot;field error&amp;quot;&amp;gt;
    &amp;lt;p&amp;gt;{$opt.component.title}: &amp;lt;opt:display /&amp;gt;&amp;lt;/p&amp;gt;
    &amp;lt;p class=&amp;quot;desc&amp;quot; opt:if=&amp;quot;$opt.component.description&amp;quot;&amp;gt;{$opt.component.description}&amp;lt;/p&amp;gt;
    &amp;lt;opt:onEvent name=&amp;quot;error&amp;quot; message=&amp;quot;info&amp;quot;&amp;gt;
      &amp;lt;p class=&amp;quot;error&amp;quot;&amp;gt;{@info}&amp;lt;/p&amp;gt;
    &amp;lt;/opt:onEvent&amp;gt;
  &amp;lt;/com:div&amp;gt;
&amp;lt;/opt:snippet&amp;gt;

&amp;lt;div class=&amp;quot;form&amp;quot;&amp;gt;
&amp;lt;form method=&amp;quot;post&amp;quot; action=&amp;quot;skrypt.php&amp;quot;&amp;gt;

&amp;lt;opt:input name=&amp;quot;name&amp;quot; template=&amp;quot;componentView&amp;quot;&amp;gt;
   &amp;lt;opt:set name=&amp;quot;title&amp;quot; str:value=&amp;quot;Enter your name&amp;quot; /&amp;gt;
   &amp;lt;opt:set name=&amp;quot;description&amp;quot; str:value=&amp;quot;At least 3 letters&amp;quot; /&amp;gt;
&amp;lt;/opt:input&amp;gt;

&amp;lt;opt:input name=&amp;quot;surname&amp;quot; template=&amp;quot;componentView&amp;quot;&amp;gt;
   &amp;lt;opt:set name=&amp;quot;title&amp;quot; str:value=&amp;quot;Enter your surname&amp;quot; /&amp;gt;
   &amp;lt;opt:set name=&amp;quot;description&amp;quot; str:value=&amp;quot;At least 3 letters&amp;quot; /&amp;gt;
&amp;lt;/opt:input&amp;gt;

&amp;lt;opt:input name=&amp;quot;age&amp;quot; template=&amp;quot;componentView&amp;quot;&amp;gt;
   &amp;lt;opt:set name=&amp;quot;title&amp;quot; str:value=&amp;quot;Enter your age&amp;quot; /&amp;gt;
   &amp;lt;opt:set name=&amp;quot;description&amp;quot; str:value=&amp;quot;Scope: 10-99 years&amp;quot; /&amp;gt;
&amp;lt;/opt:input&amp;gt;

&amp;lt;div class=&amp;quot;foot&amp;quot;&amp;gt;&amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Send&amp;quot; /&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;


&lt;p&gt;The new implementation makes use of the &lt;code&gt;opt:snippet&lt;/code&gt; instruction and template inheritance. If you had enough time, you might code the component objects, so that the field titles and descriptions will be unnecessary in the template. A component can handle events, displaying itself and its neighbourhood, managing parameters and tag attributes (we use the &lt;code&gt;com&lt;/code&gt; namespace for it). OPT contains only the component mechanism, and the components itself must be written by the end user. The reason is very simple. In order to make a useful component, we should integrate it with the input validation classes, language system, error management etc. The demands are much bigger than OPT is able and is supposed to provide. However, there will be a ready-to-use component implementation: Open Power Forms library.&lt;/p&gt;


&lt;p&gt;Unfortunately for the English manual is not started yet. Even the Polish users must use a partially-complete text, without formatting, because the new rendering system is not ready. However, we'll do everything to translate all the text together with some other materials (advices, FAQ, maybe even a tutorial?) into English, before the first final release is done. There is a team, I'll try also to ask some friends and we'll make a serious English support. Just be patient.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://www.invenzzia.org/download/opt-2.0.0-dev5.tar.gz&quot; hreflang=&quot;en&quot;&gt;Download OPT-2.0.0-dev5&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</description>
    
    
    
          <comments>http://blog.invenzzia.org/en/post/OPT-200-dev5#comment-form</comments>
      <wfw:comment>http://blog.invenzzia.org/en/post/OPT-200-dev5#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.invenzzia.org/en/feed/rss2/comments/21</wfw:commentRss>
      </item>
    
</channel>
</rss>