Skip to content

Commit

Permalink
Avoid a copy on full vector replacement by using std::move.
Browse files Browse the repository at this point in the history
When a queued update replaces an entire variable, we can re-use the
vector contained in the update by moving it over, instead of copying it.

This situation is actually pretty common in malariasimulation, where
exponentially decaying variables are entirely overwritten at each time
step. This change speeds up the simulation by about 5% for a 1M
population.
  • Loading branch information
plietar committed Mar 5, 2024
1 parent d8ef754 commit 7de77db
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions inst/include/vector_variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ inline void vector_update(
std::vector<A>& values
) {
while(updates.size() > 0) {
const auto& update = updates.front();
const auto& new_values = update.first;
const auto& index = update.second;
auto& update = updates.front();
auto& new_values = update.first;
auto& index = update.second;

auto vector_replacement = (index.size() == 0);
auto value_fill = (new_values.size() == 1);
Expand All @@ -34,7 +34,7 @@ inline void vector_update(
if (value_fill) {
std::fill(values.begin(), values.end(), new_values[0]);
} else {
values = new_values;
values = std::move(new_values);
}
} else {
if (value_fill) {
Expand Down

0 comments on commit 7de77db

Please sign in to comment.