Skip to content

Commit

Permalink
Make List/Array.fmt() about 60x faster
Browse files Browse the repository at this point in the history
Because we don't actually have to do a sprintf internally for the
default format '%s', but we can do just a .Str.  Apparently sprintf
uses a regular expression somewhere under the hood, and that makes
things a *lot* slower in this case.
  • Loading branch information
lizmat committed Jan 19, 2017
1 parent 017c6cf commit 22e589a
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/core/List.pm
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,38 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
)
}

method fmt($format = '%s', $separator = ' ') {
self.map({ .fmt($format) }).join($separator);
proto method fmt(|) { * }
multi method fmt() {
nqp::if(
(my int $elems = self.elems), # reifies
nqp::stmts(
(my $list := $!reified),
(my $strings := nqp::setelems(nqp::list_s,$elems)),
(my int $i = -1),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos_s($strings,$i,nqp::atpos($list,$i).Str)
),
nqp::p6box_s(nqp::join(' ',$strings))
),
''
)
}
multi method fmt($format, $separator = ' ') {
nqp::if(
(my int $elems = self.elems), # reifies
nqp::stmts(
(my $list := $!reified),
(my $strings := nqp::setelems(nqp::list_s,$elems)),
(my int $i = -1),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos_s($strings,$i,nqp::atpos($list,$i).fmt($format))
),
nqp::p6box_s(nqp::join($separator,$strings))
),
''
)
}

multi method elems(List:D:) is nodal {
Expand Down

0 comments on commit 22e589a

Please sign in to comment.