Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get_component() optimization
#1
(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?
Reply
#2
doesnt $GLOBALS expect that register_globals is ON? I think most hosts have this disabled...
- Chris
Thanks for using GetSimple! - Download

Please do not email me directly for help regarding GetSimple. Please post all your questions/problems in the forum!
Reply
#3
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...
Reply
#4
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 ");
            }
        }
    }
}
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply
#5
Wow! You're fast mvlcek!
Reply
#6
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)
- Chris
Thanks for using GetSimple! - Download

Please do not email me directly for help regarding GetSimple. Please post all your questions/problems in the forum!
Reply
#7
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.
Reply
#8
mvlcek & Carlos - thanks! Ive added it to the core... next up... please optimize get_navigation() ... Smile
- Chris
Thanks for using GetSimple! - Download

Please do not email me directly for help regarding GetSimple. Please post all your questions/problems in the forum!
Reply
#9
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!
Reply




Users browsing this thread: 1 Guest(s)