{"id":515,"date":"2009-11-19T20:31:36","date_gmt":"2009-11-20T04:31:36","guid":{"rendered":"http:\/\/boost-spirit.com\/home\/?page_id=515"},"modified":"2009-11-20T18:43:40","modified_gmt":"2009-11-21T02:43:40","slug":"the-spirit2-advantage","status":"publish","type":"page","link":"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/","title":{"rendered":"The Spirit2 Advantage"},"content":{"rendered":"<p>Why would you want to upgrade to Spirit2 from &#8220;Classic&#8221; Spirit?<\/p>\n<p><strong>First:<\/strong> Design. &#8220;Classic&#8221; Spirit evolved mostly adhoc. Over time, the library accumulated features, one on top of another. While the basic design proved to be sufficient in addressing extensions to a certain degree, after a certain point, the complexity became a real obstacle to advancement. We needed to refactor the library into smaller sub-libraries and modules. Spirit2 is a complete overhaul and redesign. This required new libraries such as <a href=\"http:\/\/www.boost.org\/doc\/libs\/1_41_0\/libs\/fusion\/doc\/html\/index.html\">Fusion <\/a>and <a href=\"http:\/\/www.boost.org\/doc\/libs\/1_37_0\/doc\/html\/proto.html\">Proto <\/a>and the new <a href=\"http:\/\/boost-spirit.com\/dl_docs\/phoenix-2\/libs\/spirit\/phoenix\/doc\/html\/index.html\">Phoenix<\/a>. These infrastructure libraries took lots of time to perfect. Now, these libraries are in fact 100% solid Boost-certified libraries in their own right. In terms of design, Spirit2 is just about perfect! \ud83d\ude42<\/p>\n<p><strong>Second:<\/strong> Performance. &#8220;Classic&#8221; Spirit was not optimized for speed. We didn&#8217;t actually spend time optimizing the code. The main focus was on perfecting the interface and making a solid library. There were a couple of issues with it that are rather problematic. For example, closures and grammars use TLS when multithreading and statics when not. That alone cost dearly.<\/p>\n<p>Now attributes&#8230; Somehow, someway, you&#8217;ll add semantics to your parser. You&#8217;ll want it to compute something, compile something, translate something, etc. In all certainty, you&#8217;ll be generating &#8220;something&#8221;. You won&#8217;t write a parser just to check if the input conforms to the grammar. That something&#8230; is your attribute. It&#8217;s like in functional programming where you always return &#8220;something&#8221;.<\/p>\n<p>In &#8220;Classic&#8221; Spirit, we have facilities to generate an AST. In Spirit2, an AST is just a specialized form of, you guessed it, attribute \u2014the AST <strong>IS<\/strong> your &#8220;something&#8221;. In Spirit2, there&#8217;s no special case for ASTs. You just make your parser generate your tree for you. You map your grammar to your data structure and bingo!, the parser compiles the data in-situ. You can even use your own structs.<\/p>\n<p>Now, in &#8220;Classic&#8221; Spirit, trees cost a lot. The parse-tree and AST in &#8220;classic&#8221; use some form of a runtime tree data structure comprised of vectors of nodes. We&#8217;ve found this to be rather expensive. In Spirit2, we use <a href=\"http:\/\/www.boost.org\/doc\/libs\/1_41_0\/doc\/html\/variant.html\">variants <\/a>and <a href=\"http:\/\/www.boost.org\/doc\/libs\/1_41_0\/libs\/fusion\/doc\/html\/index.html\">tuples <\/a>as much as possible. These data structures are known to be an order of magnitude faster than dynamic containers.<\/p>\n<p>You don&#8217;t pay for it when you don&#8217;t need it. If you don&#8217;t need a certain attribute, just pass unused &#8212; it is a special type that inhibits attributes and attribute computation.<\/p>\n<p>So how does this translate to performance?<\/p>\n<p>Here&#8217;s an informal benchmark sent in by <script type=\"text\/javascript\">\/\/ <![CDATA[\ndocument.write('<a href=\"\" mce_href=\"\"\"' +'\/user\/UserProfile' + '.jtp?'+'user=1425123\" rel=\"nofollow\" ' + Nabble.embeddedTarget('_top') + '>');\n\/\/ ]]><\/script><a rel=\"nofollow\" href=\"http:\/\/old.nabble.com\/user\/UserProfile.jtp?user=1425123\" target=\"_top\">Denis Taniguchi<\/a><a rel=\"nofollow\" href=\"http:\/\/old.nabble.com\/user\/UserProfile.jtp?user=1425123\" target=\"_top\"><script type=\"text\/javascript\">\/\/ <![CDATA[\ndocument.write('<\/a>');\n\/\/ ]]><\/script><\/a>, an early Spirit2 adopter:<\/p>\n<blockquote><p><!-- BODY { FONT-FAMILY:Tahoma; FONT-SIZE:10pt } P { FONT-FAMILY:Tahoma; FONT-SIZE:10pt } DIV { FONT-FAMILY:Tahoma; FONT-SIZE:10pt } TD { FONT-FAMILY:Tahoma; FONT-SIZE:10pt } -->Just a follow up of the performance problems I was facing using spirit1.x with parse tree. I managed to switch completely to spirit 2 and I though it was interesting to post the results:<\/p>\n<p>Spirit 1.x [ straight pattern matching (no actions)\u00a0 ]<br \/>\nreal \u00a0\u00a0\u00a00m0.359s<br \/>\nuser \u00a0 0m0.332s<br \/>\nsys \u00a0\u00a0\u00a0\u00a00m0.012s<\/p>\n<p>Spirit 1.x [ parse tree generation ]<br \/>\nreal \u00a0\u00a0\u00a00m9.305s<br \/>\nuser \u00a0 0m9.209s<br \/>\nsys \u00a0\u00a0\u00a0\u00a00m0.076s<\/p>\n<p>Spirit 2.1 [ AST with variant nodes generation ]<br \/>\nreal \u00a0\u00a0\u00a00m0.459s<br \/>\nuser \u00a0 0m0.432s<br \/>\nsys \u00a0\u00a0\u00a0\u00a00m0.028s<\/p>\n<p>All done in the same computer using the same file as input.<\/p><\/blockquote>\n<p>Take note that the benchmark Spirit 1.x code does not do anything at all \u2014it is just a pattern matcher, while the Spirit 2.1 collects and saves the relevant data from the input.<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li><a href=\"#\" class=\"sharing-anchor sd-button share-more\"><span>Share<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><div class=\"sharing-hidden\"><div class=\"inner\" style=\"display: none;\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-515\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-515\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-515\" class=\"share-pinterest sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span>Pinterest<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-515\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-reddit\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\" ><span>Reddit<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span>Tumblr<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Why would you want to upgrade to Spirit2 from &#8220;Classic&#8221; Spirit? First: Design. &#8220;Classic&#8221; Spirit evolved mostly adhoc. Over time, the library accumulated features, one on top of another. While the basic design proved to be sufficient in addressing extensions to a certain degree, after a certain point, the complexity became a real obstacle to [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li><a href=\"#\" class=\"sharing-anchor sd-button share-more\"><span>Share<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><div class=\"sharing-hidden\"><div class=\"inner\" style=\"display: none;\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-515\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-515\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-515\" class=\"share-pinterest sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span>Pinterest<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-515\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-reddit\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\" ><span>Reddit<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/spirit2-1-release\/the-spirit2-advantage\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span>Tumblr<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"author":2,"featured_media":0,"parent":377,"menu_order":3,"comment_status":"open","ping_status":"open","template":"article-page.php","meta":{"_s2mail":"","spay_email":""},"jetpack_shortlink":"https:\/\/wp.me\/PIHdZ-8j","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/pages\/515"}],"collection":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/comments?post=515"}],"version-history":[{"count":20,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/pages\/515\/revisions"}],"predecessor-version":[{"id":581,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/pages\/515\/revisions\/581"}],"up":[{"embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/pages\/377"}],"wp:attachment":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/media?parent=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}