Skip to content

Commit

Permalink
micro-optimized Mojolicious::Routes a little
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 2, 2012
1 parent ee1c877 commit dbd971e
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 78 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/Exception.pm
Expand Up @@ -15,7 +15,7 @@ has verbose => sub { $ENV{MOJO_EXCEPTION_VERBOSE} || 0 };
# "Attempted murder? Now honestly, what is that?
# Do they give a Nobel Prize for attempted chemistry?"
sub new {
my $self = shift->SUPER::new();
my $self = shift->SUPER::new;
return @_ ? $self->_detect(@_) : $self;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Home.pm
Expand Up @@ -19,7 +19,7 @@ has app_class => 'Mojo::HelloWorld';

# "I'm normally not a praying man, but if you're up there,
# please save me Superman."
sub new { shift->SUPER::new()->parse(@_) }
sub new { shift->SUPER::new->parse(@_) }

sub detect {
my ($self, $class) = @_;
Expand Down
12 changes: 4 additions & 8 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -47,8 +47,7 @@ sub client {
my $id = $args->{id} || $self->_id;
my $c = $self->{connections}->{$id} ||= {};
$c->{client} = $client;
$client->iowatcher($self->iowatcher);
weaken $client->{iowatcher};
weaken $client->iowatcher($self->iowatcher)->{iowatcher};

# Events
weaken $self;
Expand Down Expand Up @@ -84,8 +83,7 @@ sub delay {
$self = $self->singleton unless ref $self;

my $delay = Mojo::IOLoop::Delay->new;
$delay->ioloop($self);
weaken $delay->{ioloop};
weaken $delay->ioloop($self)->{ioloop};
$delay->once(finish => $cb) if $cb;

return $delay;
Expand Down Expand Up @@ -133,8 +131,7 @@ sub server {
my $server = $self->server_class->new;
my $id = $self->_id;
$self->{servers}->{$id} = $server;
$server->iowatcher($self->iowatcher);
weaken $server->{iowatcher};
weaken $server->iowatcher($self->iowatcher)->{iowatcher};

# Events
weaken $self;
Expand Down Expand Up @@ -195,8 +192,7 @@ sub stream {
my $id = shift || $self->_id;
my $c = $self->{connections}->{$id} ||= {};
$c->{stream} = $stream;
$stream->iowatcher($self->iowatcher);
weaken $stream->{iowatcher};
weaken $stream->iowatcher($self->iowatcher)->{iowatcher};

# Events
weaken $self;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Parameters.pm
Expand Up @@ -17,7 +17,7 @@ has pair_separator => '&';
# HOMER!
# I gotta go Moe my damn weiner kids are listening."
sub new {
my $self = shift->SUPER::new();
my $self = shift->SUPER::new;

# Hash/Array
if (@_ > 1) { $self->append(@_) }
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Path.pm
Expand Up @@ -11,7 +11,7 @@ use Mojo::URL;
has [qw/leading_slash trailing_slash/];
has parts => sub { [] };

sub new { shift->SUPER::new()->parse(@_) }
sub new { shift->SUPER::new->parse(@_) }

sub canonicalize {
my $self = shift;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/URL.pm
Expand Up @@ -19,7 +19,7 @@ our $PCHAR = "$UNRESERVED$SUBDELIM\%\:\@";

# "Homer, it's easy to criticize.
# Fun, too."
sub new { shift->SUPER::new()->parse(@_) }
sub new { shift->SUPER::new->parse(@_) }

sub authority {
my ($self, $authority) = @_;
Expand Down
81 changes: 24 additions & 57 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -36,16 +36,13 @@ sub AUTOLOAD {

sub DESTROY { }

sub new { shift->SUPER::new()->parse(@_) }
sub new { shift->SUPER::new->parse(@_) }

sub add_child {
my ($self, $route) = @_;

$route->parent($self);
weaken $route->{parent};
weaken $route->parent($self)->{parent};
$route->shortcuts($self->shortcuts);
push @{$self->children}, $route;

return $self;
}

Expand All @@ -61,55 +58,44 @@ sub add_shortcut {
return $self;
}

sub any {
shift->_generate_route((ref $_[0] || '') eq 'ARRAY' ? shift : [], @_);
}
sub any { shift->_generate_route(ref $_[0] eq 'ARRAY' ? shift : [], @_) }

# "Hey. What kind of party is this? There's no booze and only one hooker."
sub auto_render {
my ($self, $c) = @_;

# Try rendering template or not_found if the route never reached an action
eval {
my $stash = $c->stash;
unless ($stash->{'mojo.rendered'} || $c->tx->is_websocket) {

# Render template or not_found if the route never reached an action
$c->render or ($stash->{'mojo.routed'} or $c->render_not_found);
}

$c->render
or ($stash->{'mojo.routed'} or $c->render_not_found)
unless $stash->{'mojo.rendered'} || $c->tx->is_websocket;
1;
} or $c->render_exception($@);

return 1;
}

sub bridge { shift->route(@_)->inline(1) }

sub delete { shift->_generate_route(delete => @_) }

sub detour {
my $self = shift;
$self->partial(1);
$self->to(@_);
return $self;
}
sub detour { shift->partial(1)->to(@_) }

sub dispatch {
my ($self, $c) = @_;

# Path
# Prepare path
my $req = $c->req;
my $path = $c->stash->{path};
if (defined $path) { $path = "/$path" if $path !~ m#^/# }
else { $path = $req->url->path->to_abs_string }

# Match
# Prepare match
my $method = $req->method;
my $websocket = $c->tx->is_websocket ? 1 : 0;
my $m = Mojolicious::Routes::Match->new($method => $path, $websocket);
$c->match($m);

# Cached
# Check cache
my $cache = $self->cache;
if ($cache && (my $cached = $cache->get("$method:$path:$websocket"))) {
$m->root($self);
Expand All @@ -118,14 +104,12 @@ sub dispatch {
$m->endpoint($cached->{endpoint});
}

# Lookup
# Check routes
else {
$m->match($self, $c);

# Endpoint found
# Cache routes without conditions
if ($cache && (my $endpoint = $m->endpoint)) {

# Cache routes without conditions
$cache->set(
"$method:$path:$websocket" => {
endpoint => $endpoint,
Expand Down Expand Up @@ -176,7 +160,7 @@ sub is_websocket { shift->{websocket} }
sub name {
my $self = shift;

# Custom names get precedence
# Custom names have precedence
if (@_) {
if (defined(my $name = shift)) {
$self->{name} = $name;
Expand Down Expand Up @@ -204,16 +188,9 @@ sub over {

sub parse {
my $self = shift;

# Pattern does the real work
$self->pattern->parse(@_);

# Default name
my $name = $self->pattern->pattern // '';
my $name = $self->pattern->parse(@_)->pattern // '';
$name =~ s/\W+//g;
$self->{name} = $name;
$self->{custom} = 0;

$self->{name} = $name;
return $self;
}

Expand Down Expand Up @@ -358,9 +335,9 @@ sub _dispatch_controller {
return 1
unless my $app = $field->{app} || $self->_generate_class($field, $c);
my $method = $self->_generate_method($field, $c);
my $dispatch = ref $app || $app;
$dispatch .= "->$method" if $method;
$c->app->log->debug(qq/Dispatching "$dispatch"./);
my $target = ref $app || $app;
$target .= "->$method" if $method;
$c->app->log->debug(qq/Dispatching "$target"./);

# Load class
if (!ref $app && !$self->{loaded}->{$app}) {
Expand Down Expand Up @@ -398,8 +375,7 @@ sub _dispatch_controller {
# Render
else {
$c->app->log->debug(
qq/Action "$dispatch" not found, assuming template without action./
);
qq/Action "$target" not found, assuming template without action./);
$self->auto_render($app) unless $staging;
}

Expand All @@ -408,18 +384,12 @@ sub _dispatch_controller {
@{$stash}{keys %$new} = values %$new;
}

# Handler
# Application
else {

# Connect routes
if ($app->can('routes')) {
my $r = $app->routes;
unless ($r->parent) {
$r->parent($c->match->endpoint);
weaken $r->{parent};
}
weaken $r->parent($c->match->endpoint)->{parent} unless $r->parent;
}

$app->handler($c);
}

Expand Down Expand Up @@ -518,11 +488,8 @@ sub _generate_route {
if !ref $methods && $methods eq 'under';

# Create route
my $route =
$self->route($pattern, {@$constraints})->over($conditions)->via($methods)
->to($defaults)->name($name);

return $route;
return $self->route($pattern, {@$constraints})->over($conditions)
->via($methods)->to($defaults)->name($name);
}

sub _walk_stack {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes/Match.pm
Expand Up @@ -9,7 +9,7 @@ has [qw/endpoint root/];

# "I'm Bender, baby, please insert liquor!"
sub new {
my $self = shift->SUPER::new();
my $self = shift->SUPER::new;

# Method
$self->{method} = lc shift;
Expand Down
10 changes: 3 additions & 7 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -14,7 +14,7 @@ has tree => sub { [] };
has wildcard_start => '*';

# "This is the worst kind of discrimination. The kind against me!"
sub new { shift->SUPER::new()->parse(@_) }
sub new { shift->SUPER::new->parse(@_) }

sub match {
my ($self, $path) = @_;
Expand All @@ -39,10 +39,7 @@ sub parse {
if $pattern =~ m#\.([^/\)]+)$#;

# Tokenize
$self->pattern($pattern);
$self->_tokenize;

return $self;
return $self->pattern($pattern)->_tokenize;
}

sub render {
Expand Down Expand Up @@ -277,9 +274,8 @@ sub _tokenize {
$tree->[-1]->[-1] .= $char;
}
}
$self->tree($tree);

return $self;
return $self->tree($tree);
}

1;
Expand Down

0 comments on commit dbd971e

Please sign in to comment.