Skip to content

Commit

Permalink
[Optimization/dataquery] Remove unnecessary array (aces#9348)
Browse files Browse the repository at this point in the history
The InstrumentQueryEngine converts the results of a query
that returns all values from an iterator to an array because
it's used twice. However, the second usage is unnecessary as the
data is available in another way. This switches the second usage
to be array_keys of a variable created in the first iteration and
removes the iterator_to_array, which should result in less memory
usage as it can be freed after the first iteration.
  • Loading branch information
driusan authored and ZhichGaming committed Nov 25, 2024
1 parent a95d9df commit 256a850
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
2 changes: 1 addition & 1 deletion modules/dataquery/php/query.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ class Query implements \LORIS\StudyEntities\AccessibleResource,

return array_map(
function ($row) {
return new CandID($row);
return new CandID(strval($row));
},
$results
);
Expand Down
15 changes: 4 additions & 11 deletions modules/instruments/php/instrumentqueryengine.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,8 @@ class InstrumentQueryEngine implements \LORIS\Data\Query\QueryEngine
$q = $DB->prepare($insertstmt);
$q->execute([]);

$rows = iterator_to_array(
$DB->pselect(
"SELECT c.CandID, CommentID FROM flag f
$rows = $DB->pselect(
"SELECT c.CandID, CommentID FROM flag f
JOIN test_names tn ON (f.TestID=tn.ID)
JOIN session s ON (f.SessionID=s.ID)
JOIN candidate c ON (s.CandID=c.CandID)
Expand All @@ -346,8 +345,7 @@ class InstrumentQueryEngine implements \LORIS\Data\Query\QueryEngine
AND tn.Test_name IN ('" . join("', '", $instruments). "')
AND c.Active='Y' AND s.Active='Y'
ORDER BY c.CandID",
[],
)
[],
);

$commentID2CandID = [];
Expand All @@ -359,12 +357,7 @@ class InstrumentQueryEngine implements \LORIS\Data\Query\QueryEngine
foreach ($instruments as $instrument) {
$inst = \NDB_BVL_Instrument::factory($this->loris, $instrument);
$values = $inst->bulkLoadInstanceData(
array_map(
function ($row) {
return $row['CommentID'];
},
$rows
),
array_keys($commentID2CandID)
);

$instrumentIterators[$instrument] = $this->_dataToIterator(
Expand Down

0 comments on commit 256a850

Please sign in to comment.