We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39177
    • 22 Posts
    Ok,

    We have built a site for a client who wanted a number 'widgets' (twitter feed, related pages box, page author, etc) to show in the right hand column. They wanted the flexibility to pick and chose these depending on the page rather than have them pre defined by the template.

    We set this up using a number of check box TV's (for the sake of argument I will call these [[*show-chunk]]) with yes and no options. Yes=1 and No=0 which is the default.

    There is then a chunk like this to process this tv value and return the 'widget':
    [[*show-chunk:equals=`1`:then=`[[$chunk]]`:else=``]]

    When a number of these are used together it seems to have a huge impact on page load times. By this I mean the actual PHP processing time of the page.

    By removing parts of the template and chunks we had worked out [[*show-chunk:equals=`1`]] disproportionately to the load time of the page.

    With one of these calls: <!--MySQL: 0.0267 s, 3 request(s), PHP: 4.7374 s, total: 4.7641 s, document retrieved from database. -->

    Without it: <!--MySQL: 0.0132 s, 2 request(s), PHP: 3.2442 s, total: 3.2574 s, document retrieved from database. -->

    On its own not a major issue but when this is combined with 6 other identical calls these push page loads over 10 seconds before caching. Which is not acceptable!

    Has anyone have similar issues?

    OR

    Would a snippet do this faster for multiple yes/no tv's?


    This is in Modx 2.2.6pl (traditional). I can post the whole stack of template, chunks and snippets but this might just be distracting. It took us about 3 hours of trying different combinations to trace back the performance hit to this.
      • 40706
      • 128 Posts
      The Output filters are not the issue.
      All Parsing goes from the inner to the outer ones... so your [[$chunk]] is always executed, even before modx checks if its displayed or not.

      For Example if show-chunk is false:

      Parse 1
      [[*show-chunk:equals=`1`:then=`[[$chunk]]`:else=``]]

      Parse 2
      [[*show-chunk:equals=`1`:then=`<div>[[snippet]]</div>`:else=``]]

      Parse 3
      [[*show-chunk:equals=`1`:then=`<div>hello world</div>`:else=``]]

      Parse 4


      You can optimize this when you do something like that
      Parse 1
      [[[[*show-chunk:equals=`1`:then=`$chunk`:else=`-`]]]]

      Parse 2
      [[-]]

      Parse 3


      The chunk is not executed, snippets and other chunks inside the chunks are not executed. The more complex the Chunk and more code is inside are, the bigger is the speed diffrence. Hope you got it. At first look its more complex, but it gets clear.
        • 39177
        • 22 Posts
        Ok so really the issue must be that $chunk gets parsed weither *show-chunk:equals=`1` or not.
        Cumulatively this might account for the hit to page load speeds.

        Your improvement saves about 0.5 seconds per optional chunk.

        Is there a away to only parse the $chunk if *show-chunk:equals=`1` and not parse otherwise? Or is that just how output filters work?





          • 3749
          • 24,544 Posts
          See how a snippet compares:


          [[ShowChunk]]


          <?php
          /* ShowChunk snippet */
          if ($modx->resource->getTVValue('show-chunk')) {
              return $modx->getChunk('ChunkName');
          }



          If speed is really an issue, repurposing an unused resource field should be faster yet. Say you're not using the description field for anything, you could put a 1 or a 0 in that field and do this:

          <?php
          /* ShowChunk snippet */
          if ($modx->resource->get('introtext')) {
              return $modx->getChunk('ChunkName');
          }



          You could even keep the TV and write the introtext (or whatever) field from it in a plugin connected to OnDocFormSave. The TV would be ignored during page loads. It would just be there for user convenience. If you did that, you could use the deprecated 'donthit' field (assuming that it still exists), which is a boolean field. It can't be set in the Create/Edit Resource form, but the plugin would set it for you.

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 40131
            • 40 Posts
            Slightly off topic but is this still an impact on performance:

            [[*TV:notemtpy=`[[$chunk]]`]] ?

            I have multiple such calls, could they impact performance?
              • 40131
              • 40 Posts
              Quote from: m.engel at Mar 05, 2013, 03:18 PM
              The Output filters are not the issue.
              All Parsing goes from the inner to the outer ones... so your [[$chunk]] is always executed, even before modx checks if its displayed or not.

              For Example if show-chunk is false:

              Parse 1
              [[*show-chunk:equals=`1`:then=`[[$chunk]]`:else=``]]

              Parse 2
              [[*show-chunk:equals=`1`:then=`<div>[[snippet]]</div>`:else=``]]

              Parse 3
              [[*show-chunk:equals=`1`:then=`<div>hello world</div>`:else=``]]

              Parse 4


              You can optimize this when you do something like that
              Parse 1
              [[[[*show-chunk:equals=`1`:then=`$chunk`:else=`-`]]]]

              Parse 2
              [[-]]

              Parse 3


              The chunk is not executed, snippets and other chunks inside the chunks are not executed. The more complex the Chunk and more code is inside are, the bigger is the speed diffrence. Hope you got it. At first look its more complex, but it gets clear.

              is there a way with this syntax to pass some variables to a chunk or snippet, i.e.:

              [[[[*show-chunk:equals=`1`:then=`$chunk? &param=`x``:else=`-`]]]]


              the above does not work.
                • 42967
                • 143 Posts
                Quote from: nickyz at Mar 07, 2013, 08:15 AM
                Slightly off topic but is this still an impact on performance:
                [[*TV:notemtpy=`[[$chunk]]`]] ?
                I have multiple such calls, could they impact performance?

                it's better to do:
                [[*TV:default=`[[$chunk]]`]] 
                [ed. note: eighthday last edited this post 13 years, 6 months ago.]
                  Eighth day design
                  Design agency with offices in Edinburgh and Brighton
                  • 40131
                  • 40 Posts
                  Quote from: eighthday at Mar 07, 2013, 08:51 AM
                  Quote from: nickyz at Mar 07, 2013, 08:15 AM
                  Slightly off topic but is this still an impact on performance:
                  [[*TV:notemtpy=`[[$chunk]]`]] ?
                  I have multiple such calls, could they impact performance?

                  it's better to do:
                  [[*TV:default=`[[$chunk]]`]] 

                  my ideas was to execute a snippet e.g. GetResources being in this chunk, if the TV is not empty. Default value will execute always the code.
                    • 40706
                    • 128 Posts
                    Quote from: nickyz at Mar 07, 2013, 08:18 AM

                    is there a way with this syntax to pass some variables to a chunk or snippet, i.e.:

                    [[[[*show-chunk:equals=`1`:then=`$chunk? ¶m=`x``:else=`-`]]]]


                    the above does not work.

                    If the else does not need any parameters you could do

                    [[[[*show-chunk:equals=`1`:then=`$chunk`:else=`-`]]? &m=`x`  &param2=`xx2`]]




                    Quote from: eighthday at Mar 07, 2013, 08:51 AM

                    it's better to do:
                    [[*TV:default=`[[$chunk]]`]] 
                    default is an alias of notempty, so its the same.

                    The parsing order stays the same, the chunk is executed before it is checked, if it is needed.
                      • 39177
                      • 22 Posts
                      [quote author=BobRay date=1362538623]See how a snippet compares:
                      [[ShowChunk]]
                      <!--?php
                      /* ShowChunk snippet */
                      if ($modx--->resource->getTVValue('show-chunk')) {
                      return $modx->getChunk('ChunkName');
                      }
                      [/quote] Thanks Bob. The Snippet seems to work out about the same. The introtext idea has good but a little confusing for site admins. [quote author=m.engel date=1362518284]
                      [[[[*show-chunk:equals=`1`:then=`$chunk`:else=`-`]]]]


                      Michael's solution seems to be a big enough improvement to get load from database times under 4 seconds. Still pretty sluggish but there seems little that can be done without reducing functionality. If anyone is interested: http://www3.centreforcities.org/40/about/

                      Thanks everyone for your help!