Skip to content

Commit

Permalink
more micro optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 17, 2011
1 parent 60d64cf commit 558f7fe
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 72 deletions.
10 changes: 5 additions & 5 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -163,7 +163,7 @@ sub parse {
if (!$self->xml && $VOID{$start}) || $attr =~ /\/\s*$/;

# Relaxed "script" or "style"
if ($start eq 'script' || $start eq 'style') {
if ($start ~~ [qw/script style/]) {
if ($html =~ /\G(.*?)<\s*\/\s*$start\s*>/gcsi) {
$self->_raw($1, \$current);
$self->_end($start, \$current);
Expand Down Expand Up @@ -371,7 +371,7 @@ sub _start {
elsif ($start eq 'optgroup') { $self->_end('optgroup', $current) }

# "<option>"
elsif ($start eq 'option' || $start eq 'optgroup') {
elsif ($start ~~ [qw/option optgroup/]) {
$self->_end('option', $current);
$self->_end('optgroup', $current) if $start eq 'optgroup';
}
Expand All @@ -392,19 +392,19 @@ sub _start {
elsif ($start eq 'tr') { $self->_close($current, {tr => 1}) }

# "<th>" and "<td>"
elsif ($start eq 'th' || $start eq 'td') {
elsif ($start ~~ [qw/th td/]) {
$self->_close($current, {th => 1});
$self->_close($current, {td => 1});
}

# "<dt>" and "<dd>"
elsif ($start eq 'dt' || $start eq 'dd') {
elsif ($start ~~ [qw/dt dd/]) {
$self->_end('dt', $current);
$self->_end('dd', $current);
}

# "<rt>" and "<rp>"
elsif ($start eq 'rt' || $start eq 'rp') {
elsif ($start ~~ [qw/rt rp/]) {
$self->_end('rt', $current);
$self->_end('rp', $current);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/Mojo/Headers.pm
Expand Up @@ -300,6 +300,7 @@ sub transfer_encoding { scalar shift->header('Transfer-Encoding' => @_) }
sub upgrade { scalar shift->header(Upgrade => @_) }
sub user_agent { scalar shift->header('User-Agent' => @_) }
sub www_authenticate { scalar shift->header('WWW-Authenticate' => @_) }
sub x_forwarded_for { scalar shift->header('X-Forwarded-For' => @_) }

1;
__END__
Expand Down Expand Up @@ -695,6 +696,13 @@ Shortcut for the C<User-Agent> header.
Shortcut for the C<WWW-Authenticate> header.
=head2 C<x_forwarded_for>
my $x_forwarded_for = $headers->x_forwarded_for;
$headers = $headers->x_forwarded_for('127.0.0.1');
Shortcut for the C<X-Forwarded-For> header.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Message.pm
Expand Up @@ -437,8 +437,7 @@ sub _parse {
}

# Content
my $state = $self->{state} || '';
if ($state eq 'body' || $state eq 'content' || $state eq 'done') {
if (($self->{state} || '') ~~ [qw/body content done/]) {

# Until body
my $content = $self->content;
Expand Down
3 changes: 0 additions & 3 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -172,9 +172,6 @@ sub _finish {
# Successful upgrade
if ($ws->res->code eq '101') {

# Make sure connection stays active
$tx->keep_alive(1);

# Upgrade connection timeout
$self->ioloop->connection_timeout($id, $self->websocket_timeout);

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Template.pm
Expand Up @@ -92,7 +92,7 @@ sub build {
if ($type eq 'code' || $multi) { $lines[-1] .= "$value" }

# Expression
if ($type eq 'expr' || $type eq 'escp') {
if ($type ~~ [qw/expr escp/]) {

# Start
unless ($multi) {
Expand Down
38 changes: 4 additions & 34 deletions lib/Mojo/Transaction.pm
Expand Up @@ -4,7 +4,6 @@ use Mojo::Base 'Mojo::EventEmitter';
use Carp 'croak';

has [qw/connection kept_alive local_address local_port previous remote_port/];
has keep_alive => 0;

# "Please don't eat me! I have a wife and kids. Eat them!"
sub client_close { shift->server_close(@_) }
Expand All @@ -30,11 +29,7 @@ sub is_websocket {undef}

sub is_writing {
return 1 unless my $state = shift->{state};
return 1
if $state eq 'write'
|| $state eq 'write_start_line'
|| $state eq 'write_headers'
|| $state eq 'write_body';
return 1 if $state ~~ [qw/write write_start_line write_headers write_body/];
return;
}

Expand Down Expand Up @@ -67,20 +62,9 @@ sub remote_address {

# Reverse proxy
if ($ENV{MOJO_REVERSE_PROXY}) {

# Forwarded
my $forwarded = $self->{forwarded_for};
return $forwarded if $forwarded;

# Reverse proxy
if ($forwarded = $self->req->headers->header('X-Forwarded-For')) {

# Real address
if ($forwarded =~ /([^,\s]+)$/) {
$self->{forwarded_for} = $1;
return $1;
}
}
return $self->{forwarded_for} if $self->{forwarded_for};
return $self->{forwarded_for} = $1
if ($self->req->headers->x_forwarded_for || '') =~ /([^,\s]+)$/;
}

return $self->{remote_address};
Expand All @@ -91,16 +75,9 @@ sub res { croak 'Method "res" not implemented by subclass' }

sub resume {
my $self = shift;

# Delayed
if (($self->{state} || '') eq 'paused') { $self->{state} = 'write_body' }

# Writing
elsif (!$self->is_writing) { $self->{state} = 'write' }

# Resume
$self->emit('resume');

return $self;
}

Expand Down Expand Up @@ -165,13 +142,6 @@ L<Mojo::Transaction> implements the following attributes.
Connection identifier or socket.
=head2 C<keep_alive>
my $keep_alive = $tx->keep_alive;
$tx = $tx->keep_alive(1);
Connection can be kept alive.
=head2 C<kept_alive>
my $kept_alive = $tx->kept_alive;
Expand Down
32 changes: 10 additions & 22 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -120,34 +120,23 @@ sub client_write {
}

sub keep_alive {
my ($self, $ka) = @_;

# Change default
if ($ka) {
$self->{ka} = $ka;
return $self;
}
my $self = shift;

# Close
my $req = $self->req;
my $res = $self->res;
my $req_conn = $req->headers->connection || '';
my $res_conn = $res->headers->connection || '';
my $req_ver = $req->version;
my $res_ver = $res->version;

# Close
if ($req_conn =~ /^close$/i || $res_conn =~ /^close$/i) { $self->{ka} = 0 }
my $req_conn = lc($req->headers->connection || '');
my $res_conn = lc($res->headers->connection || '');
return 0 if $req_conn eq 'close' || $res_conn eq 'close';

# Keep alive
elsif ($req_conn =~ /^keep-alive$/i || $res_conn =~ /^keep-alive$/i) {
$self->{ka} = 1;
}
return 1 if $req_conn eq 'keep-alive' || $res_conn eq 'keep-alive';

# No keep alive for 0.9 and 1.0
elsif ($req_ver eq '0.9' || $req_ver eq '1.0') { $self->{ka} ||= 0 }
elsif ($res_ver eq '0.9' || $res_ver eq '1.0') { $self->{ka} ||= 0 }
return 0 if $req->version ~~ [qw/0.9 1.0/];
return 0 if $res->version ~~ [qw/0.9 1.0/];

return $self->{ka} //= 1;
return 1;
}

# DEPRECATED in Smiling Face With Sunglasses!
Expand Down Expand Up @@ -412,9 +401,8 @@ Write client data.
=head2 C<keep_alive>
my $keep_alive = $tx->keep_alive;
$tx = $tx->keep_alive(1);
Connection can be kept alive.
Check if connection can be kept alive.
=head2 C<server_leftovers>
Expand Down
6 changes: 1 addition & 5 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -222,11 +222,7 @@ sub _tokenize {
while (length(my $char = substr $pattern, 0, 1, '')) {

# Inside a symbol
my $symbol = 0;
$symbol = 1
if $state eq 'relaxed'
|| $state eq 'symbol'
|| $state eq 'wildcard';
my $symbol = $state ~~ [qw/relaxed symbol wildcard/] ? 1 : 0;

# Quote start
if ($char eq $quote_start) {
Expand Down

0 comments on commit 558f7fe

Please sign in to comment.