croxton / Stash

Stash allows you to stash text and snippets of code for reuse throughout your templates.
GNU General Public License v3.0
198 stars 20 forks source link

Many calls to $ee_uri->_fetch_uri_string(); #140

Closed leevigraham closed 8 years ago

leevigraham commented 8 years ago

It seems stash creates a new EE_URI object for every exp:stash:get tag and then fires a couple of methods.

This seems quite expensive. Could the $uri be cached?

        // fetch the *unadulterated* URI of the current page
        $ee_uri = new EE_URI;

        // documented as a 'private' method, but not actually. Called in CI_Router so unlikely to ever be made private.
        $ee_uri->_fetch_uri_string();
        $ee_uri->_remove_url_suffix();
        $ee_uri->_explode_segments();

        // provide a fallback value for index pages
        $uri = $ee_uri->uri_string();
        $uri = empty($uri) ? $this->EE->stash_model->get_index_key() : $uri;

https://github.com/croxton/Stash/blob/dev/system/expressionengine/third_party/stash/mod.stash.php#L4088

Blackfire.io is showing these calls take up 5% of the page render in a stash heavy site I'm working on.

screen shot 2015-12-08 at 11 31 48 pm screen shot 2015-12-08 at 11 42 16 pm
leevigraham commented 8 years ago

The following code adds caching and removes all but one call to _explode_segments:

    /**
     * Replace the current context in a variable name
     *
     * @access private
     * @param string    $name The variable name
     * @return string
     */
    private function _parse_context($name)
    {

        $uri = ee()->session->cache('stash', 'uri');

        if(!$uri) {
            // fetch the *unadulterated* URI of the current page
            $ee_uri = new EE_URI;

            // documented as a 'private' method, but not actually. Called in CI_Router so unlikely to ever be made private.
            $ee_uri->_fetch_uri_string();
            $ee_uri->_remove_url_suffix();
            $ee_uri->_explode_segments();

            // provide a fallback value for index pages
            $uri = $ee_uri->uri_string();

            ee()->session->set_cache('stash', 'uri', $uri);
        }

        $uri = empty($uri) ? $this->EE->stash_model->get_index_key() : $uri;
screen shot 2015-12-08 at 11 51 14 pm screen shot 2015-12-08 at 11 51 02 pm
croxton commented 8 years ago

Nice. But, I'd store the value in self::$_cache['uri']rather then Stash's session cache (where stash vars are stored). Want to make a pr against the dev branch?

leevigraham commented 8 years ago

:+1: Probably should have updated my PR :smile: