CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Slow Router

Open maddrid opened this issue 6 years ago • 3 comments

Describe the bug This is a general issue , router and route collection class needs complete rewrite

CodeIgniter 4 version All.

Affected module(s) Router ,RouteCollection.

Expected behavior, and steps to reproduce if appropriate Router Duplicate amount of used memory by recreating a routes variable $routes inside checkRoutes method

protected function checkRoutes(string $uri): bool
	{
		$routes = $this->collection->getRoutes($this->collection->getHTTPVerb());

Checking for locale segment in foreach(routes) loop

// Are we dealing with a locale?
			if (strpos($key, '{locale}') !== false)
			{
				$localeSegment = array_search('{locale}', preg_split('/[\/]*((^[a-zA-Z0-9])|\(([^()]*)\))*[\/]+/m', $key));

				// Replace it with a regex so it
				// will actually match.
				$key = str_replace('{locale}', '[^/]+', $key);
			}

should have a method or a filter class to find locale before handle (uri) or use route group

$routes->group('es', function($routes) {
    $routes->add('users', 'Admin\Users::index');
    $routes->add('blog',  'Admin\Blog::index');
});

move checkRoutes to RouteCollection

Route Collection

discover routes method should keep track of loaded files and allow other routing files to be loaded not just the default one .

allow static routes ( httpmethod and uri as params ) fast hashtable lookup


//borowed from fastroute
public function checkRoutes(string $httpMethod, string $uri): array
    {
        if (isset($this->staticRouteMap[$httpMethod][$uri])) {
            $handler = $this->staticRouteMap[$httpMethod][$uri];
            return [self::FOUND, $handler, []];
        }
        $varRouteData = $this->variableRouteData;
        if (isset($varRouteData[$httpMethod])) {
            $result = $this->dispatchVariableRoute($varRouteData[$httpMethod], $uri);
            if ($result[0] === self::FOUND) {
                return $result;
            }
        }

implement route cache strategy allow subrouting inside controllers to split routes Thanks !

maddrid avatar Sep 13 '19 20:09 maddrid

A non-trivial problem, which should be raised on the forum instead of here.

jim-parry avatar Oct 19 '19 16:10 jim-parry

You know - you could have already fixed it and submitted a PR for it by now. They're pretty good suggestions, but not going to happen prior to 4.0 final without someone else jumping in to tackle it.

lonnieezell avatar Nov 15 '19 04:11 lonnieezell

@maddrid Thank you for reporting.

Is it true that the current router is slow? Why did you know it?

kenjis avatar Jan 22 '22 01:01 kenjis

Since no one seems interested, can I close this?

There is certainly room for improvement in the detailed implementation, but there is no benchmarks so I don't know if it is really slow or not.

Besides, more route definitions will always slow it down. It is my opinion that it is better not to increase the number of routes.

kenjis avatar Aug 31 '23 01:08 kenjis