Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 3, 2012
1 parent 899af07 commit 506214b
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/Asset/Memory.pm
Expand Up @@ -16,7 +16,7 @@ sub new { shift->SUPER::new(@_, content => '') }
sub add_chunk {
my ($self, $chunk) = @_;

$self->{content} .= $chunk if defined $chunk;
$self->{content} .= $chunk // '';
return $self
if !$self->auto_upgrade || $self->size <= $self->max_memory_size;
my $file = Mojo::Asset::File->new;
Expand Down
11 changes: 3 additions & 8 deletions lib/Mojo/Content.pm
Expand Up @@ -165,15 +165,10 @@ sub parse_body_once {
sub parse_until_body {
my ($self, $chunk) = @_;

# Prepare first buffer
$self->{pre_buffer} //= '';
$self->{raw_size} //= 0;

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

# Parser started
unless ($self->{state}) {
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Headers.pm
Expand Up @@ -89,12 +89,11 @@ sub names {
}

sub parse {
my ($self, $chunk) = @_;
my $self = shift;

# Parse headers with size limit
$self->{state} = 'headers';
$self->{buffer} //= '';
$self->{buffer} .= $chunk if defined $chunk;
$self->{buffer} .= shift // '';
my $headers = $self->{cache} ||= [];
my $max = $self->max_line_size;
while (defined(my $line = get_line \$self->{buffer})) {
Expand All @@ -106,7 +105,7 @@ sub parse {
}

# New header
if ($line =~ /^(\S+)\s*:\s*(.*)$/) { push @$headers, $1, $2 }
elsif ($line =~ /^(\S+)\s*:\s*(.*)$/) { push @$headers, $1, $2 }

# Multiline
elsif (@$headers && $line =~ s/^\s+//) { $headers->[-1] .= " $line" }
Expand Down
9 changes: 3 additions & 6 deletions lib/Mojo/Message.pm
Expand Up @@ -339,12 +339,9 @@ sub _parse {
my ($self, $until_body, $chunk) = @_;

# Add chunk
$self->{buffer} //= '';
$self->{raw_size} //= 0;
if (defined $chunk) {
$self->{raw_size} += length $chunk;
$self->{buffer} .= $chunk;
}
$chunk //= '';
$self->{raw_size} += length $chunk;
$self->{buffer} .= $chunk;

# Check message size
return $self->error('Maximum message size exceeded.', 413)
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -239,7 +239,7 @@ sub server_read {
my ($self, $chunk) = @_;

# Parse frames
$self->{read} .= $chunk if defined $chunk;
$self->{read} .= $chunk // '';
while (my $frame = $self->parse_frame(\$self->{read})) {
$self->emit(frame => $frame);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojo/Util.pm
Expand Up @@ -113,13 +113,12 @@ sub encode {
}

sub get_line {
my $stringref = shift;

# Locate line ending
return if (my $pos = index $$stringref, "\x0a") == -1;
return if (my $pos = index ${$_[0]}, "\x0a") == -1;

# Extract line and ending
my $line = substr $$stringref, 0, $pos + 1, '';
my $line = substr ${$_[0]}, 0, $pos + 1, '';
$line =~ s/\x0d?\x0a$//;

return $line;
Expand Down

0 comments on commit 506214b

Please sign in to comment.