{"action":"opened","issue":{"url":"https://api.github.com/repos/symfony/symfony/issues/18145","repository_url":"https://api.github.com/repos/symfony/symfony","labels_url":"https://api.github.com/repos/symfony/symfony/issues/18145/labels{/name}","comments_url":"https://api.github.com/repos/symfony/symfony/issues/18145/comments","events_url":"https://api.github.com/repos/symfony/symfony/issues/18145/events","html_url":"https://github.com/symfony/symfony/issues/18145","id":140441636,"number":18145,"title":"RouteCollection and UrlMatcher Performance [Enhancement]","user":{"login":"leonelvsc","id":2332029,"avatar_url":"https://avatars.githubusercontent.com/u/2332029?v=3","gravatar_id":"","url":"https://api.github.com/users/leonelvsc","html_url":"https://github.com/leonelvsc","followers_url":"https://api.github.com/users/leonelvsc/followers","following_url":"https://api.github.com/users/leonelvsc/following{/other_user}","gists_url":"https://api.github.com/users/leonelvsc/gists{/gist_id}","starred_url":"https://api.github.com/users/leonelvsc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leonelvsc/subscriptions","organizations_url":"https://api.github.com/users/leonelvsc/orgs","repos_url":"https://api.github.com/users/leonelvsc/repos","events_url":"https://api.github.com/users/leonelvsc/events{/privacy}","received_events_url":"https://api.github.com/users/leonelvsc/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-03-13T03:15:11Z","updated_at":"2016-03-13T03:15:11Z","closed_at":null,"body":"Hi all, first than anything my english it's not so good so i apologize and this is just a suggestion.\r\n\r\nReading the RouteCollection class i saw that currently you store the routes in an array and then in the UrlMatcher you iterate the routes array until you find a match.\r\n\r\nMy suggestion is that you use a Hash structure in the array for optimizing performance , i.e if the app has 100000 routes that loop for in the UrlMatcher class has to iterate all the array (in the worst case).\r\n\r\nMaybe we can research if it's possible to store routes in the following way.\r\n\r\nFor the RouteCollection class add a hashedRoutes array\r\n\r\n     /**\r\n     * @var Route[]\r\n     */\r\n    private $routes = array();\r\n\r\n    /**\r\n     * @var Route[]\r\n     */\r\n    private $hashedRoutes = array();\r\n\r\n\r\nThen when you add a route to the collection you build some sort of hash\r\n\r\ni.e\r\n\r\n    /**\r\n     * Adds a route.\r\n     *\r\n     * @param string $name  The route name\r\n     * @param Route  $route A Route instance\r\n     */\r\n    public function add($name, Route $route)\r\n    {\r\n        unset($this->routes[$name]);\r\n\r\n        $compiledRoute = $route->compile();\r\n        $prefix = $compiledRoute->getStaticPrefix();\r\n        $index = substr($prefix, 0, strpos($prefix, '/'));\r\n\r\n        $this->hashedRoutes[$index][] = $route;\r\n\r\n        $this->routes[$name] = $route;\r\n    }\r\n\r\nthen we could add the following method to the RouteCollection\r\n\r\n    /**\r\n     *\r\n     * @param $pathInfo\r\n     */\r\n    public function getWithPrefix($pathInfo)\r\n    {\r\n        $index = substr($pathInfo, 0, strpos($pathInfo, '/'));\r\n        return (isset($this->hashedRoutes[$index]))? $this->hashedRoutes[$index] : [];\r\n    }\r\n\r\nOf course that for this to work we would have to regenerate the hash in several methods of the RouteCollection i.e the addPrefix method \r\n\r\n     /**\r\n     * Adds a prefix to the path of all child routes.\r\n     *\r\n     * @param string $prefix       An optional prefix to add before each pattern of the route collection\r\n     * @param array  $defaults     An array of default values\r\n     * @param array  $requirements An array of requirements\r\n     */\r\n    public function addPrefix($prefix, array $defaults = array(), array $requirements = array())\r\n    {\r\n        $prefix = trim(trim($prefix), '/');\r\n\r\n        if ('' === $prefix) {\r\n            return;\r\n        }\r\n\r\n        foreach ($this->routes as $route) {\r\n            $compiled = $route->compile();\r\n            $compiledPrefix = $compiled->getStaticPrefix();\r\n\r\n            $oldRoutes = $this->hashedRoutes[$compiledPrefix];\r\n            unset($this->hashedRoutes[$compiledPrefix]);\r\n\r\n            $route->setPath('/'.$prefix.$route->getPath());\r\n            $route->addDefaults($defaults);\r\n            $route->addRequirements($requirements);\r\n\r\n            $compiled = $route->compile();\r\n            $compiledPrefix = $compiled->getStaticPrefix();\r\n\r\n            $this->hashedRoutes[$compiledPrefix] = $oldRoutes;\r\n        }\r\n    }\r\n\r\nThen if we store the Routes in a Hash inside the RouteCollection the UrlMatcher class can take advantage of this, and just itarate over the routes that matches the hash , i.e\r\n\r\n    protected function matchCollection($pathinfo, RouteCollection $routes)\r\n    {\r\n        foreach ($routes->getWithPrefix($pathinfo) as $name => $route) {"}}