Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

perf(routing): replace recursion in favor of iteration #705

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions src/Tempest/Http/src/Routing/Construction/RouteTreeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RouteTreeNode
/** @var array<string, RouteTreeNode> */
private array $dynamicPaths = [];

private ?MarkedRoute $leaf = null;
private ?MarkedRoute $targetRoute = null;

private function __construct(
public readonly RouteTreeNodeType $type,
Expand All @@ -40,34 +40,26 @@ public static function createStaticRouteNode(string $name): self
return new self(RouteTreeNodeType::Static, $name);
}

public function addPath(array $pathSegments, MarkedRoute $markedRoute): void
public function findOrCreateNodeForSegment(string $routeSegment): self
{
// If path segments is empty this node should target to given marked route
if ($pathSegments === []) {
if ($this->leaf !== null) {
throw new DuplicateRouteException($markedRoute->route);
}

$this->leaf = $markedRoute;
// Translates a path segment like {id} into it's matching regex. Static segments remain the same
$regexRouteSegment = self::convertDynamicSegmentToRegex($routeSegment);

return;
// Returns a static or dynamic child node depending on the segment is dynamic or static
if ($routeSegment === $regexRouteSegment) {
return $this->staticPaths[$regexRouteSegment] ??= self::createStaticRouteNode($routeSegment);
}

// Removes the first element of the pathSegments and use it to determin the next routing node
$currentPathSegment = array_shift($pathSegments);

// Translates a path segment like {id} into it's matching regex. Static segments remain the same
$regexPathSegment = self::convertDynamicSegmentToRegex($currentPathSegment);
return $this->dynamicPaths[$regexRouteSegment] ??= self::createDynamicRouteNode($regexRouteSegment);
}

// Find or create the next node to recurse into
if ($currentPathSegment !== $regexPathSegment) {
$node = $this->dynamicPaths[$regexPathSegment] ??= self::createDynamicRouteNode($regexPathSegment);
} else {
$node = $this->staticPaths[$regexPathSegment] ??= self::createStaticRouteNode($currentPathSegment);
public function setTargetRoute(MarkedRoute $markedRoute): void
{
if ($this->targetRoute !== null) {
throw new DuplicateRouteException($markedRoute->route);
}

// Recurse into the newly created node to add the remainder of the path segments
$node->addPath($pathSegments, $markedRoute);
$this->targetRoute = $markedRoute;
}

private static function convertDynamicSegmentToRegex(string $uriPart): string
Expand Down Expand Up @@ -107,14 +99,14 @@ public function toRegex(): string
// Add a leaf alteration with an optional slash and end of line match `$`.
// The `(*MARK:x)` is a marker which when this regex is matched will cause the matches array to contain
// a key `"MARK"` with value `"x"`, it is used to track which route has been matched
if ($this->leaf !== null) {
$regexp .= '|\/?$(*' . MarkedRoute::REGEX_MARK_TOKEN . ':' . $this->leaf->mark . ')';
if ($this->targetRoute !== null) {
$regexp .= '|\/?$(*' . MarkedRoute::REGEX_MARK_TOKEN . ':' . $this->targetRoute->mark . ')';
}

$regexp .= ")";
} elseif ($this->leaf !== null) {
} elseif ($this->targetRoute !== null) {
// Add a singular leaf regex without alteration
$regexp .= '\/?$(*' . MarkedRoute::REGEX_MARK_TOKEN . ':' . $this->leaf->mark . ')';
$regexp .= '\/?$(*' . MarkedRoute::REGEX_MARK_TOKEN . ':' . $this->targetRoute->mark . ')';
}

return $regexp;
Expand Down
10 changes: 7 additions & 3 deletions src/Tempest/Http/src/Routing/Construction/RoutingTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ public function add(MarkedRoute $markedRoute): void
$method = $markedRoute->route->method;

// Find the root tree node based on HTTP method
$root = $this->roots[$method->value] ??= RouteTreeNode::createRootRoute();
$node = $this->roots[$method->value] ??= RouteTreeNode::createRootRoute();

// Add path to tree using recursion
$root->addPath($markedRoute->route->split(), $markedRoute);
// Traverse the tree and find the node for each route segment
foreach ($markedRoute->route->split() as $routeSegment) {
$node = $node->findOrCreateNodeForSegment($routeSegment);
}

$node->setTargetRoute($markedRoute);
}

/** @return array<string, string> */
Expand Down
Loading