From 7de77db3f91443d00ed257ccd17b688dd1aea8c8 Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Tue, 5 Mar 2024 19:35:31 +0000 Subject: [PATCH] Avoid a copy on full vector replacement by using std::move. 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. --- inst/include/vector_variables.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/include/vector_variables.h b/inst/include/vector_variables.h index b9f4fc6..7e5f7b5 100644 --- a/inst/include/vector_variables.h +++ b/inst/include/vector_variables.h @@ -22,9 +22,9 @@ inline void vector_update( std::vector& 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); @@ -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) {