Skip to content

Commit 9b7c2b2

Browse files
committed
Apply clang-tidy performance fixes
1 parent d633df6 commit 9b7c2b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+229
-183
lines changed

cpp/dolfinx/common/IndexMap.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@ graph::AdjacencyList<int> IndexMap::index_to_dest_ranks(int tag) const
11831183
// Build list of (ghost index, ghost position) pairs for indices
11841184
// ghosted by this rank, and sort
11851185
std::vector<std::pair<std::int64_t, std::int32_t>> idx_to_pos;
1186+
idx_to_pos.reserve(2 * _ghosts.size());
11861187
for (auto idx : _ghosts)
11871188
idx_to_pos.push_back({idx, idx_to_pos.size()});
11881189
std::ranges::sort(idx_to_pos);

cpp/dolfinx/common/MPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void dolfinx::MPI::check_error(MPI_Comm comm, int code)
8585
std::string error_string(MPI_MAX_ERROR_STRING, ' ');
8686
MPI_Error_string(code, error_string.data(), &len);
8787
error_string.resize(len);
88-
std::cerr << error_string << std::endl;
88+
std::cerr << error_string << '\n';
8989
MPI_Abort(comm, code);
9090
std::abort();
9191
}

cpp/dolfinx/common/MPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
namespace dolfinx::MPI
3131
{
3232
/// MPI communication tags
33-
enum class tag : int
33+
enum class tag : std::uint16_t
3434
{
3535
consensus_pcx = 1200,
3636
consensus_pex = 1201,

cpp/dolfinx/common/Scatterer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Scatterer
3434
using allocator_type = Allocator;
3535

3636
/// Types of MPI communication pattern used by the Scatterer.
37-
enum class type
37+
enum class type : std::uint8_t
3838
{
3939
neighbor, // use MPI neighborhood collectives
4040
p2p // use MPI Isend/Irecv for communication

cpp/dolfinx/common/Table.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ using namespace dolfinx;
3131

3232
//-----------------------------------------------------------------------------
3333
Table::Table(std::string title, bool right_justify)
34-
: name(title), _right_justify(right_justify)
34+
: name(std::move(title)), _right_justify(right_justify)
3535
{
3636
// Do nothing
3737
}
3838
//-----------------------------------------------------------------------------
39-
void Table::set(std::string row, std::string col,
39+
void Table::set(const std::string& row, const std::string& col,
4040
std::variant<std::string, int, double> value)
4141
{
4242
// Add row
@@ -49,11 +49,11 @@ void Table::set(std::string row, std::string col,
4949

5050
// Store value
5151
std::pair<std::string, std::string> key(row, col);
52-
_values[key] = value;
52+
_values[key] = std::move(value);
5353
}
5454
//-----------------------------------------------------------------------------
55-
std::variant<std::string, int, double> Table::get(std::string row,
56-
std::string col) const
55+
std::variant<std::string, int, double> Table::get(const std::string& row,
56+
const std::string& col) const
5757
{
5858
std::pair<std::string, std::string> key(row, col);
5959
auto it = _values.find(key);

cpp/dolfinx/common/Table.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include <cstdint>
910
#include <map>
1011
#include <mpi.h>
1112
#include <string>
@@ -29,7 +30,7 @@ class Table
2930
public:
3031
/// Types of MPI reduction available for Table, to get the max, min or
3132
/// average values over an MPI_Comm
32-
enum class Reduction
33+
enum class Reduction : std::uint8_t
3334
{
3435
average,
3536
max,
@@ -58,15 +59,15 @@ class Table
5859
/// @param[in] row Row name
5960
/// @param[in] col Column name
6061
/// @param[in] value The value to set
61-
void set(std::string row, std::string col,
62+
void set(const std::string& row, const std::string& col,
6263
std::variant<std::string, int, double> value);
6364

6465
/// Get value of table entry
6566
/// @param[in] row Row name
6667
/// @param[in] col Column name
6768
/// @returns Returns the entry for requested row and columns
68-
std::variant<std::string, int, double> get(std::string row,
69-
std::string col) const;
69+
std::variant<std::string, int, double> get(const std::string& row,
70+
const std::string& col) const;
7071

7172
/// Do MPI reduction on Table
7273
/// @param[in] comm MPI communicator

cpp/dolfinx/common/TimeLogger.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ TimeLogger& TimeLogger::instance()
2121

2222
//-----------------------------------------------------------------------------
2323
void TimeLogger::register_timing(
24-
std::string task, std::chrono::duration<double, std::ratio<1>> time)
24+
const std::string& task, std::chrono::duration<double, std::ratio<1>> time)
2525
{
2626
// Print a message
2727
std::string line
@@ -47,7 +47,7 @@ void TimeLogger::list_timings(MPI_Comm comm, Table::Reduction reduction) const
4747

4848
// Print just on rank 0
4949
if (dolfinx::MPI::rank(comm) == 0)
50-
std::cout << str << std::endl;
50+
std::cout << str << '\n';
5151
}
5252
//-----------------------------------------------------------------------------
5353
Table TimeLogger::timing_table() const
@@ -67,7 +67,7 @@ Table TimeLogger::timing_table() const
6767
}
6868
//-----------------------------------------------------------------------------
6969
std::pair<int, std::chrono::duration<double, std::ratio<1>>>
70-
TimeLogger::timing(std::string task) const
70+
TimeLogger::timing(const std::string& task) const
7171
{
7272
// Find timing
7373
auto it = _timings.find(task);

cpp/dolfinx/common/TimeLogger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TimeLogger
2828
static TimeLogger& instance();
2929

3030
/// Register timing (for later summary)
31-
void register_timing(std::string task,
31+
void register_timing(const std::string& task,
3232
std::chrono::duration<double, std::ratio<1>> wall);
3333

3434
/// Return a summary of timings and tasks in a Table
@@ -44,7 +44,7 @@ class TimeLogger
4444
/// @param[in] task The task name to retrieve the timing for
4545
/// @return Values (count, total wall time) for given task.
4646
std::pair<int, std::chrono::duration<double, std::ratio<1>>>
47-
timing(std::string task) const;
47+
timing(const std::string& task) const;
4848

4949
/// @brief Logged elapsed times.
5050
/// @return Elapsed [task id: (count, total wall time)].

cpp/dolfinx/common/Timer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class Timer
4848
/// @param[in] task Name used to registered the elapsed time in the
4949
/// logger. If no name is set, the elapsed time is not registered in
5050
/// the logger.
51-
Timer(std::optional<std::string> task = std::nullopt) : _task(task) {}
51+
Timer(std::optional<std::string> task = std::nullopt) : _task(std::move(task))
52+
{
53+
}
5254

5355
/// If timer is still running, it is stopped. Elapsed time is
5456
/// registered in the logger.

cpp/dolfinx/common/timing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void dolfinx::list_timings(MPI_Comm comm, Table::Reduction reduction)
2121
}
2222
//-----------------------------------------------------------------------------
2323
std::pair<int, std::chrono::duration<double, std::ratio<1>>>
24-
dolfinx::timing(std::string task)
24+
dolfinx::timing(const std::string& task)
2525
{
2626
return dolfinx::common::TimeLogger::instance().timing(task);
2727
}

cpp/dolfinx/common/timing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void list_timings(MPI_Comm comm,
3232
/// @param[in] task Name of a task
3333
/// @return The (count, total wall time) for the task.
3434
std::pair<int, std::chrono::duration<double, std::ratio<1>>>
35-
timing(std::string task);
35+
timing(const std::string& task);
3636

3737
/// @brief Logged elapsed times.
3838
/// @return Elapsed [task id: (count, total wall time)].

cpp/dolfinx/fem/CoordinateElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using namespace dolfinx::fem;
1818
template <std::floating_point T>
1919
CoordinateElement<T>::CoordinateElement(
2020
std::shared_ptr<const basix::FiniteElement<T>> element)
21-
: _element(element)
21+
: _element(std::move(element))
2222
{
2323
int degree = _element->degree();
2424
mesh::CellType cell = this->cell_shape();

cpp/dolfinx/fem/DirichletBC.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ get_remote_dofs(MPI_Comm comm, const common::IndexMap& map, int bs_map,
154154
// NOTE: Should we use map here or just one vector with ghosts and
155155
// std::distance?
156156
std::vector<std::pair<std::int64_t, std::int32_t>> global_local_ghosts;
157+
global_local_ghosts.reserve(ghosts.size());
157158
const std::int32_t local_size = range[1] - range[0];
158159
for (std::size_t i = 0; i < ghosts.size(); ++i)
159160
global_local_ghosts.emplace_back(ghosts[i], i + local_size);
@@ -194,6 +195,7 @@ std::vector<std::int32_t> fem::locate_dofs_topological(
194195
// entity_dim
195196
const int num_cell_entities = mesh::cell_num_entities(cell_type, dim);
196197
std::vector<std::vector<int>> entity_dofs;
198+
entity_dofs.reserve(num_cell_entities);
197199
for (int i = 0; i < num_cell_entities; ++i)
198200
{
199201
entity_dofs.push_back(
@@ -310,6 +312,7 @@ std::array<std::vector<std::int32_t>, 2> fem::locate_dofs_topological(
310312
// Build vector of local dofs for each cell entity
311313
const int num_cell_entities = mesh::cell_num_entities(cell_type, dim);
312314
std::vector<std::vector<int>> entity_dofs;
315+
entity_dofs.reserve(num_cell_entities);
313316
for (int i = 0; i < num_cell_entities; ++i)
314317
{
315318
entity_dofs.push_back(

cpp/dolfinx/fem/DofMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class DofMap
9292
std::vector<std::int32_t>>
9393
DofMap(E&& element, std::shared_ptr<const common::IndexMap> index_map,
9494
int index_map_bs, U&& dofmap, int bs)
95-
: index_map(index_map), _index_map_bs(index_map_bs),
95+
: index_map(std::move(index_map)), _index_map_bs(index_map_bs),
9696
_element_dof_layout(std::forward<E>(element)),
9797
_dofmap(std::forward<U>(dofmap)), _bs(bs),
9898
_shape1(_element_dof_layout.num_dofs()

cpp/dolfinx/fem/ElementDofLayout.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
#pragma once
88

99
#include <array>
10+
#include <cstdint>
1011
#include <span>
1112
#include <vector>
1213

1314
namespace dolfinx::mesh
1415
{
15-
enum class CellType;
16+
enum class CellType : std::int8_t;
1617
}
1718

1819
namespace dolfinx::fem

cpp/dolfinx/fem/FiniteElement.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ _build_element_list(std::vector<BasixElementData<T>> elements)
3232
{
3333
std::vector<std::shared_ptr<const FiniteElement<T>>> _e;
3434
std::ranges::transform(elements, std::back_inserter(_e),
35-
[](auto data)
35+
[](const auto& data)
3636
{
3737
auto& [e, bs, symm] = data;
3838
return std::make_shared<fem::FiniteElement<T>>(e, bs,
@@ -110,7 +110,7 @@ int _compute_block_size(std::optional<std::vector<std::size_t>> value_shape,
110110
template <std::floating_point T>
111111
FiniteElement<T>::FiniteElement(
112112
const basix::FiniteElement<T>& element,
113-
std::optional<std::vector<std::size_t>> value_shape, bool symmetric)
113+
const std::optional<std::vector<std::size_t>>& value_shape, bool symmetric)
114114
: _value_shape(value_shape.value_or(element.value_shape())),
115115
_bs(_compute_block_size(value_shape, symmetric)),
116116
_cell_type(mesh::cell_type_from_basix_type(element.cell_type())),
@@ -161,7 +161,7 @@ FiniteElement<T>::FiniteElement(
161161
//-----------------------------------------------------------------------------
162162
template <std::floating_point T>
163163
FiniteElement<T>::FiniteElement(std::vector<BasixElementData<T>> elements)
164-
: FiniteElement(_build_element_list(elements))
164+
: FiniteElement(_build_element_list(std::move(elements)))
165165
{
166166
}
167167
//-----------------------------------------------------------------------------

cpp/dolfinx/fem/FiniteElement.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace dolfinx::fem
2323
{
2424
/// DOF transformation type
25-
enum class doftransform
25+
enum class doftransform : std::uint8_t
2626
{
2727
standard = 0, ///< Standard
2828
transpose = 1, ///< Transpose
@@ -68,7 +68,7 @@ class FiniteElement
6868
/// @param[in] symmetric Is the element a symmetric tensor? Should
6969
/// only set for 2nd-order tensor blocked elements.
7070
FiniteElement(const basix::FiniteElement<geometry_type>& element,
71-
std::optional<std::vector<std::size_t>> value_shape
71+
const std::optional<std::vector<std::size_t>>& value_shape
7272
= std::nullopt,
7373
bool symmetric = false);
7474

cpp/dolfinx/fem/FunctionSpace.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class FunctionSpace
4444
FunctionSpace(std::shared_ptr<const mesh::Mesh<geometry_type>> mesh,
4545
std::shared_ptr<const FiniteElement<geometry_type>> element,
4646
std::shared_ptr<const DofMap> dofmap)
47-
: _mesh(mesh), _elements{element}, _dofmaps{dofmap},
47+
: _mesh(mesh), _elements{element}, _dofmaps{std::move(dofmap)},
4848
_id(boost::uuids::random_generator()()), _root_space_id(_id)
4949
{
5050
// Do nothing
@@ -62,7 +62,7 @@ class FunctionSpace
6262
std::shared_ptr<const mesh::Mesh<geometry_type>> mesh,
6363
std::vector<std::shared_ptr<const FiniteElement<geometry_type>>> elements,
6464
std::vector<std::shared_ptr<const DofMap>> dofmaps)
65-
: _mesh(mesh), _elements(elements), _dofmaps(dofmaps),
65+
: _mesh(mesh), _elements(elements), _dofmaps(std::move(dofmaps)),
6666
_id(boost::uuids::random_generator()()), _root_space_id(_id)
6767
{
6868
std::vector<mesh::CellType> cell_types = mesh->topology()->cell_types();
@@ -72,7 +72,7 @@ class FunctionSpace
7272
throw std::runtime_error(
7373
"Number of elements must match number of cell types");
7474
}
75-
if (dofmaps.size() != num_cell_types)
75+
if (_dofmaps.size() != num_cell_types)
7676
{
7777
throw std::runtime_error(
7878
"Number of dofmaps must match number of cell types");

cpp/dolfinx/fem/dofmapbuilder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ build_basic_dofmaps(
344344
for (std::size_t k = 0; k < required_dim_et.size(); ++k)
345345
{
346346
const int num_entity_dofs = num_entity_dofs_et[k];
347-
auto map = topo_index_maps[k];
347+
const auto& map = topo_index_maps[k];
348348
assert(map);
349349
std::vector<std::int64_t> global_indices = map->global_indices();
350350

@@ -413,7 +413,7 @@ std::pair<std::vector<std::int32_t>, std::int32_t> compute_reordering_map(
413413
// owned dofs. Set to -1 for unowned dofs.
414414
std::vector<int> original_to_contiguous(dof_entity.size(), -1);
415415
std::int32_t counter_owned(0), counter_unowned(owned_size);
416-
for (auto dofmap : dofmaps)
416+
for (const auto& dofmap : dofmaps)
417417
{
418418
for (std::int32_t dof : dofmap.array)
419419
{
@@ -487,7 +487,7 @@ std::pair<std::vector<std::int64_t>, std::vector<int>> get_global_indices(
487487
std::vector<std::vector<std::int8_t>> shared_entity(index_maps.size());
488488
for (std::size_t d = 0; d < index_maps.size(); ++d)
489489
{
490-
auto map = index_maps[d];
490+
const auto& map = index_maps[d];
491491
assert(map);
492492

493493
shared_entity[d] = std::vector<std::int8_t>(map->size_local(), false);
@@ -524,7 +524,7 @@ std::pair<std::vector<std::int64_t>, std::vector<int>> get_global_indices(
524524
std::vector<std::vector<int>> disp_recv(index_maps.size());
525525
for (std::size_t d = 0; d < index_maps.size(); ++d)
526526
{
527-
auto map = index_maps[d];
527+
const auto& map = index_maps[d];
528528
assert(map);
529529

530530
std::span src = map->src();

cpp/dolfinx/fem/petsc.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace petsc
4444
/// object.
4545
template <std::floating_point T>
4646
Mat create_matrix(const Form<PetscScalar, T>& a,
47-
std::optional<std::string> type = std::nullopt)
47+
const std::optional<std::string>& type = std::nullopt)
4848
{
4949
la::SparsityPattern pattern = fem::create_sparsity_pattern(a);
5050
pattern.finalize();
@@ -64,7 +64,7 @@ Mat create_matrix(const Form<PetscScalar, T>& a,
6464
template <std::floating_point T>
6565
Mat create_matrix_block(
6666
const std::vector<std::vector<const Form<PetscScalar, T>*>>& a,
67-
std::optional<std::string> type = std::nullopt)
67+
const std::optional<std::string>& type = std::nullopt)
6868
{
6969
// Extract and check row/column ranges
7070
std::array<std::vector<std::shared_ptr<const FunctionSpace<T>>>, 2> V
@@ -404,8 +404,8 @@ void apply_lifting(
404404
template <std::floating_point T>
405405
void apply_lifting(
406406
Vec b,
407-
std::vector<
408-
std::optional<std::reference_wrapper<const Form<PetscScalar, double>>>>
407+
const std::vector<
408+
std::optional<std::reference_wrapper<const Form<PetscScalar, double>>>>&
409409
a,
410410
const std::vector<std::vector<
411411
std::reference_wrapper<const DirichletBC<PetscScalar, double>>>>& bcs1,

0 commit comments

Comments
 (0)