Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 22, 2013
1 parent 8696a62 commit 81ce848
Show file tree
Hide file tree
Showing 59 changed files with 49 additions and 240 deletions.
3 changes: 0 additions & 3 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -59,7 +59,6 @@ sub add_chunk {
sub contains {
my ($self, $string) = @_;

# Seek to start
my $handle = $self->handle;
$handle->sysseek($self->start_range, SEEK_SET);

Expand Down Expand Up @@ -95,12 +94,10 @@ sub contains {
sub get_chunk {
my ($self, $start) = @_;

# Seek to start
$start += $self->start_range;
my $handle = $self->handle;
$handle->sysseek($start, SEEK_SET);

# Range support
my $buffer;
if (defined(my $end = $self->end_range)) {
my $chunk = $end + 1 - $start;
Expand Down
9 changes: 1 addition & 8 deletions lib/Mojo/Base.pm
Expand Up @@ -2,8 +2,6 @@ package Mojo::Base;

use strict;
use warnings;

# Mojo modules are modern!
use utf8;
use feature ();

Expand All @@ -16,8 +14,6 @@ use IO::Handle ();
sub import {
my $class = shift;
return unless my $flag = shift;

# No limits!
no strict 'refs';

# Base
Expand All @@ -37,8 +33,6 @@ sub import {
if ($flag) {
my $caller = caller;
push @{"${caller}::ISA"}, $flag;

# Can haz?
*{"${caller}::has"} = sub { attr($caller, @_) };
}

Expand All @@ -61,11 +55,10 @@ sub attr {
my ($class, $attrs, $default) = @_;
return unless ($class = ref $class || $class) && $attrs;

# Check default
Carp::croak('Default has to be a code reference or constant value')
if ref $default && ref $default ne 'CODE';

# Create attributes
# Compile attributes
for my $attr (@{ref $attrs eq 'ARRAY' ? $attrs : [$attrs]}) {
Carp::croak(qq{Attribute "$attr" invalid})
unless $attr =~ /^[a-zA-Z_]\w*$/;
Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/Cache.pm
Expand Up @@ -8,7 +8,6 @@ sub get { (shift->{cache} || {})->{shift()} }
sub set {
my ($self, $key, $value) = @_;

# Cache with size limit
my $cache = $self->{cache} ||= {};
my $queue = $self->{queue} ||= [];
delete $cache->{shift @$queue} if @$queue >= $self->max_keys;
Expand Down
9 changes: 0 additions & 9 deletions lib/Mojo/Content.pm
Expand Up @@ -40,14 +40,9 @@ sub clone {
sub generate_body_chunk {
my ($self, $offset) = @_;

# Drain
$self->emit(drain => $offset)
if !delete $self->{delay} && !length($self->{body_buffer} // '');

# Empty buffer
my $chunk = delete $self->{body_buffer} // '';

# EOF or delay
return $self->{eof} ? '' : undef unless length $chunk;

return $chunk;
Expand Down Expand Up @@ -287,17 +282,13 @@ sub _parse_headers {
sub _parse_until_body {
my ($self, $chunk) = @_;

# Buffer chunk
$self->{raw_size} += length($chunk //= '');
$self->{pre_buffer} .= $chunk;

# Parser started
unless ($self->{state}) {
$self->{header_size} = $self->{raw_size} - length $self->{pre_buffer};
$self->{state} = 'headers';
}

# Parse headers
$self->_parse_headers if ($self->{state} // '') eq 'headers';
}

Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Content/MultiPart.pm
Expand Up @@ -23,7 +23,7 @@ sub body_contains {
sub body_size {
my $self = shift;

# Check for Content-Lenght header
# Check for existing Content-Lenght header
my $content_len = $self->headers->content_length;
return $content_len if $content_len;

Expand Down Expand Up @@ -172,7 +172,6 @@ sub _parse_multipart_preamble {
sub _read {
my ($self, $chunk) = @_;

# Buffer chunk and parse multipart content
$self->{multipart} .= $chunk;
my $boundary = $self->boundary;
until (($self->{multi_state} //= 'multipart_preamble') eq 'finished') {
Expand Down
2 changes: 0 additions & 2 deletions lib/Mojo/DOM.pm
Expand Up @@ -106,7 +106,6 @@ sub content_xml {
sub find {
my ($self, $selector) = @_;

# Match selector against tree and upgrade results
my $charset = $self->charset;
my $xml = $self->xml;
return Mojo::Collection->new(
Expand Down Expand Up @@ -197,7 +196,6 @@ sub replace_content {
sub root {
my $self = shift;

# Find root
my $root = $self->tree;
while ($root->[0] eq 'tag') {
last unless my $parent = $root->[3];
Expand Down
4 changes: 0 additions & 4 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -36,7 +36,6 @@ my $TOKEN_RE = qr/
sub select {
my $self = shift;

# Compile selectors and walk tree
my @results;
my $pattern = $self->_compile(shift);
my $tree = $self->tree;
Expand Down Expand Up @@ -121,7 +120,6 @@ sub _combinator {
sub _compile {
my ($self, $css) = @_;

# Tokenize
my $pattern = [[]];
while ($css =~ /$TOKEN_RE/g) {
my ($separator, $element, $pc, $attrs, $combinator)
Expand Down Expand Up @@ -319,7 +317,6 @@ sub _regex {
sub _selector {
my ($self, $selector, $current) = @_;

# Selectors
for my $s (@$selector[1 .. $#$selector]) {
my $type = $s->[0];

Expand All @@ -346,7 +343,6 @@ sub _selector {
sub _sibling {
my ($self, $selectors, $current, $tree, $immediate) = @_;

# Find preceding elements
my $parent = $current->[3];
my $found;
my $start = $parent->[0] eq 'root' ? 1 : 4;
Expand Down
4 changes: 1 addition & 3 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -76,11 +76,9 @@ my %INLINE = map { $_ => 1 } (
sub parse {
my ($self, $html) = @_;

# Try to decode
my $charset = $self->charset;
$html = decode($charset, $html) // return $self->charset(undef) if $charset;

# Tokenize
my $tree = ['root'];
my $current = $tree;
while ($html =~ m/\G$TOKEN_RE/gcs) {
Expand Down Expand Up @@ -358,7 +356,7 @@ sub _start {
}
}

# New
# New tag
my $new = ['tag', $start, $attrs, $$current];
weaken $new->[3];
push @$$current, $new;
Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/Date.pm
Expand Up @@ -9,7 +9,6 @@ use Time::Local 'timegm';

has 'epoch';

# Days and months
my @DAYS = qw(Sun Mon Tue Wed Thu Fri Sat);
my @MONTHS = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my %MONTHS;
Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/Headers.pm
Expand Up @@ -162,7 +162,6 @@ sub to_string {
push @headers, "$name: " . join("\x0d\x0a ", @$_) for $self->header($name);
}

# Format headers
return join "\x0d\x0a", @headers;
}

Expand Down
8 changes: 2 additions & 6 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -42,7 +42,7 @@ sub acceptor {
# Make sure connection manager is running
$self->_manager;

# New acceptor
# Connect acceptor with reactor
my $id = $self->_id;
$self->{acceptors}{$id} = $acceptor;
weaken $acceptor->reactor($self->reactor)->{reactor};
Expand All @@ -61,13 +61,11 @@ sub client {
# Make sure connection manager is running
$self->_manager;

# New client
my $id = $self->_id;
my $c = $self->{connections}{$id} ||= {};
my $client = $c->{client} = Mojo::IOLoop::Client->new;
weaken $client->reactor($self->reactor)->{reactor};

# Connect
weaken $self;
$client->on(
connect => sub {
Expand Down Expand Up @@ -128,7 +126,6 @@ sub server {
my ($self, $cb) = (shift, pop);
$self = $self->singleton unless ref $self;

# New server
my $server = Mojo::IOLoop::Server->new;
weaken $self;
$server->on(
Expand Down Expand Up @@ -213,7 +210,7 @@ sub _id {
sub _manage {
my $self = shift;

# Start accepting if possible
# Try to acquire accept mutex
$self->_accepting;

# Close connections gracefully
Expand Down Expand Up @@ -242,7 +239,6 @@ sub _not_accepting {
return unless my $cb = $self->unlock;
$self->$cb;

# Stop accepting
$_->stop for values %{$self->{acceptors} || {}};
}

Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/IOLoop/Server.pm
Expand Up @@ -115,7 +115,6 @@ sub stop { $_[0]->reactor->remove($_[0]->{handle}) }
sub _accept {
my $self = shift;

# Accept
return unless my $handle = $self->{handle}->accept;
$handle->blocking(0);

Expand Down
2 changes: 0 additions & 2 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -135,12 +135,10 @@ sub _write {
return $self->emit_safe(error => $!)->close;
}

# Remove written chunk from buffer
$self->emit_safe(write => substr($self->{buffer}, 0, $written, ''));
$self->{active} = time;
}

# Drain
$self->emit_safe('drain') if !length $self->{buffer};

# Stop writing
Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/JSON/Pointer.pm
Expand Up @@ -10,7 +10,6 @@ sub get { shift->_pointer(0, @_) }
sub _pointer {
my ($self, $contains, $data, $pointer) = @_;

# Parse pointer and walk data structure
$pointer = decode('UTF-8', url_unescape $pointer);
return $data unless $pointer =~ s!^/!!;
for my $p (split '/', $pointer) {
Expand Down
4 changes: 0 additions & 4 deletions lib/Mojo/Loader.pm
Expand Up @@ -6,7 +6,6 @@ use File::Spec::Functions qw(catdir catfile splitdir);
use Mojo::Exception;
use Mojo::Util qw(b64_decode class_to_path);

# Cache
my %CACHE;

sub data {
Expand Down Expand Up @@ -34,7 +33,6 @@ sub load {
sub search {
my ($self, $namespace) = @_;

# Check all directories
my (@modules, %found);
for my $directory (@INC) {
next unless -d (my $path = catdir $directory, split(/::|'/, $namespace));
Expand All @@ -43,8 +41,6 @@ sub search {
opendir(my $dir, $path);
for my $file (grep /\.pm$/, readdir $dir) {
next if -d catfile splitdir($path), $file;

# Module found
my $class = "${namespace}::" . fileparse $file, qr/\.pm/;
push @modules, $class unless $found{$class}++;
}
Expand Down
2 changes: 0 additions & 2 deletions lib/Mojo/Log.pm
Expand Up @@ -59,10 +59,8 @@ sub warn { shift->log(warn => @_) }
sub _message {
my ($self, $level, @lines) = @_;

# Check level
return unless $self->is_level($level) && (my $handle = $self->handle);

# Format lines and write to handle
flock $handle, LOCK_EX;
croak "Can't write to log: $!"
unless defined $handle->syswrite($self->format($level, @lines));
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Message.pm
Expand Up @@ -42,9 +42,8 @@ sub body {

sub body_params {
my $self = shift;
return $self->{body_params} if $self->{body_params};

# Detect charset
return $self->{body_params} if $self->{body_params};
my $params = $self->{body_params} = Mojo::Parameters->new;
$params->charset($self->content->charset || $self->default_charset);

Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/Message/Request.pm
Expand Up @@ -100,7 +100,6 @@ sub fix_headers {
sub get_start_line_chunk {
my ($self, $offset) = @_;

# Prepare request line
unless (defined $self->{start_buffer}) {

# Path
Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/Message/Response.pm
Expand Up @@ -116,7 +116,6 @@ sub fix_headers {
sub get_start_line_chunk {
my ($self, $offset) = @_;

# Prepare status line
unless (defined $self->{start_buffer}) {
my $code = $self->code || 404;
my $msg = $self->message || $self->default_message;
Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/Parameters.pm
Expand Up @@ -134,7 +134,6 @@ sub remove {
sub to_hash {
my $self = shift;

# Format
my $params = $self->params;
my %hash;
for (my $i = 0; $i < @$params; $i += 2) {
Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/Path.pm
Expand Up @@ -16,7 +16,6 @@ sub new { shift->SUPER::new->parse(@_) }
sub canonicalize {
my $self = shift;

# Resolve path
my @parts;
for my $part (@{$self->parts}) {

Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -17,7 +17,7 @@ sub is_running { !!shift->{running} }
sub one_tick {
my $self = shift;

# Remember state
# Remember state for later
my $running = $self->{running};
$self->{running} = 1;

Expand Down Expand Up @@ -56,7 +56,6 @@ sub one_tick {
# Normal timer
else { $self->remove($id) }

# Handle timer
++$i and $self->_sandbox("Timer $id", $t->{cb}) if $t->{cb};
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/Server/CGI.pm
Expand Up @@ -6,7 +6,6 @@ has 'nph';
sub run {
my $self = shift;

# Prepare transaction and store connection information
my $tx = $self->build_tx;
my $req = $tx->req->parse(\%ENV);
$tx->local_port($ENV{SERVER_PORT})->remote_address($ENV{REMOTE_ADDR});
Expand Down

0 comments on commit 81ce848

Please sign in to comment.