Skip to content

Commit 1908455

Browse files
committed
Reduce number of count checks for > 2
1 parent 9be5f71 commit 1908455

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/core/Hash.nqp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,16 @@ sub hash(*%new) {
1010
sub sorted_keys($hash) {
1111
my @keys := nqp::list_s();
1212

13+
# not empty
1314
if nqp::elems($hash) -> int $count {
1415
my $iter := nqp::iterator($hash);
1516
nqp::while(
1617
$iter,
1718
nqp::push_s(@keys,nqp::iterkey_s(nqp::shift($iter)))
1819
);
1920

20-
if $count == 1 {
21-
# all ok already
22-
}
23-
elsif $count == 2 {
24-
# swap if necessary
25-
if nqp::atpos_s(@keys, 0) gt nqp::atpos_s(@keys, 1) {
26-
nqp::push_s(@keys, nqp::shift_s(@keys));
27-
}
28-
}
29-
else {
21+
# need to do actual sorting
22+
if $count > 2 {
3023
# need to do actual sorting
3124
sub sift_down(@a, int $start, int $end) {
3225
my int $root := $start;
@@ -68,6 +61,12 @@ sub sorted_keys($hash) {
6861
sift_down(@keys, 0, $end);
6962
}
7063
}
64+
# swap if necessary
65+
else {
66+
nqp::push_s(@keys, nqp::shift_s(@keys))
67+
if $count == 2
68+
&& nqp::atpos_s(@keys, 0) gt nqp::atpos_s(@keys, 1);
69+
}
7170
}
7271

7372
@keys

0 commit comments

Comments
 (0)