Welcome Guest, Not a member yet? Register   Sign In
site_url & base_url performance
#1

(This post was last modified: 06-12-2018, 05:01 AM by puschie.)

hey,
while optimizing my application I noticed that site_url & base_url are very slow. So caching the base_url improved my menu generation speed by 95%. All tests with 100 request & 24 calls
  • site_url( $URI ) : 21ms
  • site_url().$URI : 20ms
  • base_url( $URI ) : 8.8ms
  • base_url().$URI : 3.5ms
  • single base_url() call : 0.2ms
  • single site_url() call : 1.0ms
So maybe we can add an cache mechanic to these functions ?
Reply
#2

It's always slow on the first try because the page has not been cached yet!

Second time around you will get a faster load speed.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 06-12-2018, 05:31 AM by puschie. Edit Reason: add test results )

yes, but not everything can be cached, so it would be very practical if you could use the URL functions very often ( personally I also find it much better readable ).

i am not very familiar with all the scenarios that site_url is about, so it would be nice if someone could give some detailed information about the extent to which site_url can be optimized.

about 1ms execution time for a single site_url call is a way too slow and not acceptable.

Edit:
the slowest part is url_helper.php:58 -> creating a new config | cost about 0.65ms
the constructor of BaseConfig needs about 0.6ms for creating a new Config/App instance
-> do we need a clean instance on every call ? maybe we should use a shared instance as default and create a new one on null passed as third parameter
Reply
#4

That is why I prefer to use a constant.

constants.php
PHP Code:
define('BASE_URL''http://yourdomain.com/codeigniter/'); 


and

config.php
PHP Code:
$config['base_url'] = BASE_URL

In the views:
Code:
<a href="<?php echo BASE_URL."controller/method" ?> >Action</a>

Reply
#5

Good catch. We would definitely be interested in a pull request for that!
Reply
#6

(This post was last modified: 06-14-2018, 07:37 AM by puschie. Edit Reason: add new links )

Pull Request

New PR

Issue
Reply




Theme © iAndrew 2016 - Forum software by © MyBB