Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use nqp::readlinechompfh for .get and .lines.
Saves us chomping the strings that come from the VM, decreasing the
amount of GC-able objects me make when reading lines.
  • Loading branch information
jnthn committed Nov 5, 2015
1 parent 9cc7465 commit 13936ab
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/core/IO/Handle.pm
Expand Up @@ -143,13 +143,12 @@ my class IO::Handle does IO {
}

method get(IO::Handle:D:) {
return Str if self.eof;
return Str if nqp::eoffh($!PIO);

my Str $x = nqp::p6box_s(nqp::readlinefh($!PIO));
my Str $x = nqp::p6box_s($!chomp ?? nqp::readlinechompfh($!PIO) !! nqp::readlinefh($!PIO));
# XXX don't fail() as long as it's fatal
# fail('end of file') if self.eof && $x eq '';
$x.=chomp if $.chomp;
return Str if self.eof && $x eq '';
return Str if nqp::eoffh($!PIO) && $x eq '';

$!ins = $!ins + 1;
$x;
Expand Down Expand Up @@ -656,16 +655,16 @@ my class IO::Handle does IO {
}
}
multi method lines(IO::Handle:D: :$close) {
if $.chomp { # this can go as soon as we have chomp support on PIO
if $!chomp { # this can go as soon as we have chomp support on PIO
Seq.new(class :: does LinesIterCommon {
method pull-one() {
my str $line = nqp::readlinefh($!PIO);
if nqp::chars($line) {
my str $line = nqp::readlinechompfh($!PIO);
if nqp::chars($line) || !nqp::eoffh($!PIO) {
nqp::bindattr_i($!handle, IO::Handle, '$!ins',
nqp::add_i(
nqp::getattr_i($!handle, IO::Handle, '$!ins'),
1));
nqp::p6box_s($line).chomp;
nqp::p6box_s($line)
}
else {
$!handle.close if $!close;
Expand All @@ -674,13 +673,13 @@ my class IO::Handle does IO {
}
method push-exactly($target, int $n) {
my int $found;
my str $line = nqp::readlinefh($!PIO);
while nqp::chars($line) {
$target.push(nqp::p6box_s($line).chomp);
my str $line = nqp::readlinechompfh($!PIO);
while nqp::chars($line) || !nqp::eoffh($!PIO) {
$target.push(nqp::p6box_s($line));
$found = $found + 1;
last if $found == $n;

$line = nqp::readlinefh($!PIO);
$line = nqp::readlinechompfh($!PIO);
}

if $!close { # don't bother updating .ins
Expand All @@ -695,20 +694,20 @@ my class IO::Handle does IO {
nqp::p6box_i($found);
}
method push-all($target) {
my str $line = nqp::readlinefh($!PIO);
my str $line = nqp::readlinechompfh($!PIO);
if $!close { # don't bother keeping track of $!ins
while nqp::chars($line) {
$target.push(nqp::p6box_s($line).chomp);
$line = nqp::readlinefh($!PIO);
while nqp::chars($line) || !nqp::eoffh($!PIO) {
$target.push(nqp::p6box_s($line));
$line = nqp::readlinechompfh($!PIO);
}
$!handle.close;
}
else {
my int $found;
while nqp::chars($line) {
$target.push(nqp::p6box_s($line).chomp);
while nqp::chars($line) || !nqp::eoffh($!PIO) {
$target.push(nqp::p6box_s($line));
$found = $found + 1;
$line = nqp::readlinefh($!PIO);
$line = nqp::readlinechompfh($!PIO);
}
nqp::bindattr_i($!handle, IO::Handle, '$!ins',
nqp::add_i(
Expand Down

0 comments on commit 13936ab

Please sign in to comment.