Skip to content

Commit ace21a5

Browse files
committed
perf:Faster profile and history vectors creation by skipping transpose. Using in-place transposition.
1 parent 99852d4 commit ace21a5

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

lib/ML/SparseMatrixRecommender.rakumod

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,35 @@ class ML::SparseMatrixRecommender
3838
self!file-in-items-and-tags if %!items.elems == 0 || %!tags.elems == 0 || %!items.elems != $!M.nrow || %!tags.elems != $!M.ncol;
3939

4040
# Make the rules
41-
my @rules = $mix.map({ %!tags{$_.key}:exists ?? ((%!tags{$_.key}, 0) => $_.value) !! Empty });
41+
my @rules = do if $column {
42+
$mix.map({ %!tags{$_.key}:exists ?? ((%!tags{$_.key}, 0) => $_.value) !! Empty })
43+
} else {
44+
$mix.map({ %!tags{$_.key}:exists ?? ((0, %!tags{$_.key}) => $_.value) !! Empty })
45+
}
4246

4347
if $warn {
4448
note 'None of the keys of the argument are known tags.' if @rules.elems == 0;
4549
note 'Some of the keys of the argument are not known tags.' if 0 < @rules.elems < $mix.elems;
4650
}
4751

48-
# Make the column matrix
49-
my $mat = Math::SparseMatrix.new(
52+
my $mat = do if $column {
53+
# Make the column matrix
54+
Math::SparseMatrix.new(
5055
:@rules,
5156
nrow => $!M.ncol,
5257
ncol => 1,
5358
row-names => $!M.column-names,
5459
column-names => [$item-name, ]);
60+
} else {
61+
# Make the row matrix
62+
Math::SparseMatrix.new(
63+
:@rules,
64+
nrow => 1,
65+
ncol => $!M.ncol,
66+
column-names => $!M.column-names,
67+
row-names => [$item-name, ]);
68+
}
5569

56-
if !$column { $mat .= transpose }
5770
if $!native { $mat.to-adapted }
5871
return $mat;
5972
}
@@ -68,23 +81,37 @@ class ML::SparseMatrixRecommender
6881
self!file-in-items-and-tags if %!items.elems == 0 || %!tags.elems == 0 || %!items.elems != $!M.nrow || %!tags.elems != $!M.ncol;
6982

7083
# Make the rules
71-
my @rules = $mix.map({ %!items{$_.key}:exists ?? ((0, %!items{$_.key}) => $_.value) !! Empty });
84+
my @rules = do if $column {
85+
$mix.map({ %!items{$_.key}:exists ?? ((%!items{$_.key}, 0) => $_.value) !! Empty });
86+
} else {
87+
$mix.map({ %!items{$_.key}:exists ?? ((0, %!items{$_.key}) => $_.value) !! Empty });
88+
}
7289

7390
if $warn {
7491
note 'None of the keys of the argument are known items.' if @rules.elems == 0;
7592
note 'Some of the keys of the argument are not known items.' if 0 < @rules.elems < $mix.elems;
7693
}
7794

78-
# Make the row matrix
79-
my $mat = Math::SparseMatrix.new(
80-
:@rules,
81-
nrow => 1,
82-
ncol => $!M.nrow,
83-
row-names => [$tag-name, ],
84-
column-names => $!M.row-names,
85-
);
95+
my $mat = do if $column {
96+
# Make the column matrix
97+
Math::SparseMatrix.new(
98+
:@rules,
99+
ncol => 1,
100+
nrow => $!M.nrow,
101+
column-names => [$tag-name, ],
102+
row-names => $!M.row-names,
103+
);
104+
} else {
105+
# Make the row matrix
106+
Math::SparseMatrix.new(
107+
:@rules,
108+
nrow => 1,
109+
ncol => $!M.nrow,
110+
row-names => [$tag-name, ],
111+
column-names => $!M.row-names,
112+
);
113+
}
86114

87-
if $column { $mat .= transpose }
88115
if $!native { $mat.to-adapted }
89116
return $mat;
90117
}
@@ -789,8 +816,8 @@ class ML::SparseMatrixRecommender
789816

790817
$mat-tag-type.to-adapted if $!native;
791818

792-
# Transpose in place
793-
$recs = $recs.transpose;
819+
# Transpose
820+
$recs = $recs.transpose(:!clone);
794821

795822
# Respect voting
796823
if $voting {
@@ -817,7 +844,7 @@ class ML::SparseMatrixRecommender
817844
}
818845

819846
# Reverse sort
820-
$cl-res = $cl-res.sort({ -$_.value }).Hash;
847+
# $cl-res = $cl-res.sort({ -$_.value });
821848

822849
# Pick max-top labels
823850
if $max-number-of-labels && $max-number-of-labels < $cl-res.elems {

0 commit comments

Comments
 (0)