Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use map::find rather than insert to avoid creating temporary #13884

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions include/deal.II/matrix_free/dof_info.templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ namespace internal
next_constraint.first[j] = constraint_entries[j].second;
}
}
next_constraint.second = constraints.size();

// check whether or not constraint is already in pool. the initial
// implementation computed a hash value based on the truncated array (to
Expand All @@ -83,13 +82,16 @@ namespace internal
// equal. this was quite lengthy and now we use a std::map with a
// user-defined comparator to compare floating point arrays to a
// tolerance 1e-13.
const auto it = constraints.insert(next_constraint);

types::global_dof_index insert_position = numbers::invalid_dof_index;
if (it.second == false)
insert_position = it.first->second;
const auto position = constraints.find(next_constraint.first);
if (position != constraints.end())
insert_position = position->second;
else
insert_position = next_constraint.second;
{
next_constraint.second = constraints.size();
constraints.insert(next_constraint);
insert_position = next_constraint.second;
}

// we want to store the result as a short variable, so we have to make
// sure that the result does not exceed the limits when casting.
Expand Down
20 changes: 12 additions & 8 deletions include/deal.II/matrix_free/mapping_info.templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -1054,17 +1054,17 @@ namespace internal
for (unsigned int cell = 0; cell < jacobians_on_stencil.size(); ++cell)
{
// check in the map for the index of this cell
auto inserted = compressed_jacobians.insert(
std::make_pair(jacobians_on_stencil[cell], cell));
bool add_this_cell = inserted.second;
if (inserted.second == false)
const auto position =
compressed_jacobians.find(jacobians_on_stencil[cell]);
bool add_this_cell = position == compressed_jacobians.end();
if (!add_this_cell)
{
// check if the found duplicate really is a translation and
// the similarity identified by the map is not by accident
double max_distance = 0;
const double *ptr_origin =
plain_quadrature_points.data() +
inserted.first->second * dim * n_mapping_points;
position->second * dim * n_mapping_points;
const double *ptr_mine = plain_quadrature_points.data() +
cell * dim * n_mapping_points;
for (unsigned int d = 0; d < dim; ++d)
Expand All @@ -1084,16 +1084,20 @@ namespace internal
if (max_distance > 1e-10 * jacobian_size)
add_this_cell = true;
}
else
compressed_jacobians.insert(
std::make_pair(jacobians_on_stencil[cell], cell));

if (add_this_cell)
cell_data_index[cell] = n_data_buckets++;
else
{
cell_data_index[cell] = cell_data_index[inserted.first->second];
cell_data_index[cell] = cell_data_index[position->second];
// make sure that the cell type is the same as in the original
// field, despite possible small differences due to roundoff
// field, despite possibly small differences due to roundoff
// and the tolerances we use
preliminary_cell_type[cell] =
preliminary_cell_type[inserted.first->second];
preliminary_cell_type[position->second];
}
}
return cell_data_index;
Expand Down