GetSimple Support Forum
get_component() optimization - Printable Version

+- GetSimple Support Forum (http://get-simple.info/forums)
+-- Forum: GetSimple (http://get-simple.info/forums/forumdisplay.php?fid=3)
+--- Forum: Developer Discussions (http://get-simple.info/forums/forumdisplay.php?fid=8)
+--- Thread: get_component() optimization (/showthread.php?tid=1814)



get_component() optimization - Carlos - 2011-06-05

(I submitted this to the svn...)

Every time a template calls get_component() the components.xml file is read. With GS bundled themes that's 3 file reads per rendered page, but could be even more...

I suggest making $components a global variable, and checking if it exists before loading the file.

Something like this:
Code:
function get_component($id) {
    if (!array_key_exists('components', $GLOBALS)) {
        global $components;
        if (file_exists(GSDATAOTHERPATH.'components.xml')) {
            $data = getXML(GSDATAOTHERPATH.'components.xml');
            $components = $data->item;
        }
    } else {
        global $components;
    }
    
    if (count($components) != 0) {
        foreach ($components as $component) {
            if ($id == $component->slug) {
                eval("?>" . strip_decode($component->value) . "<?php ");
            }
        }
    }
}

PS: Opinions? Do you think of another way?


get_component() optimization - ccagle8 - 2011-06-05

doesnt $GLOBALS expect that register_globals is ON? I think most hosts have this disabled...


get_component() optimization - Carlos - 2011-06-06

Oops, didn't know that (it works for me in XAMPP and my typical cpanel shared hosting).
I will think and try a different (more fail-proof) way to check that...


get_component() optimization - mvlcek - 2011-06-06

To not use $GLOBALS:
Code:
$components = null;

function get_component($id) {
    global $components;
    if (!$components) {
         if (file_exists(GSDATAOTHERPATH.'components.xml')) {
            $data = getXML(GSDATAOTHERPATH.'components.xml');
            $components = $data->item;
        } else {
            $components = array();
        }
    }
    if (count($components) > 0) {
        foreach ($components as $component) {
            if ($id == $component->slug) {
                eval("?>" . strip_decode($component->value) . "<?php ");
            }
        }
    }
}



get_component() optimization - Carlos - 2011-06-06

Wow! You're fast mvlcek!


get_component() optimization - ccagle8 - 2011-06-06

I Like it! Trying to think out load here: will it update if/when a component is changed though? (before admin panel logout, but after component change)


get_component() optimization - Carlos - 2011-06-06

ccagle8 Wrote:will it update if/when a component is changed though? (before admin panel logout, but after component change)

Yes.
The whole components.xml is updated when you save changes in /admin/components.php
This fix is not for the backend, but for theme_functions.php
Anyway I've tested this, just in case, and works ok.


get_component() optimization - ccagle8 - 2011-06-12

mvlcek & Carlos - thanks! Ive added it to the core... next up... please optimize get_navigation() ... Smile


get_component() optimization - Carlos - 2011-06-25

ccagle8 Wrote:next up... please optimize get_navigation() ... Smile

Luckily Mike dit it yesterday: r505 ;-)

With these 2 changes, GS 3.1 page rendering is gonna (or should) be much faster!