Skip to content

Commit 47079b9

Browse files
authored
(perf): Add missing move in sp matrix caster and microopt char concats (#3823)
* Add missing move in sp matrix caster and microopt char concat * Remove useless move * Add a couple more std::move * Missed one char * Improve error_string * Ensure no temp reallocs in errorString concat * Remove useless move
1 parent b22ee64 commit 47079b9

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

include/pybind11/detail/type_caster_base.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp,
225225
if (throw_if_missing) {
226226
std::string tname = tp.name();
227227
detail::clean_type_id(tname);
228-
pybind11_fail("pybind11::detail::get_type_info: unable to find type info for \"" + tname
229-
+ "\"");
228+
pybind11_fail("pybind11::detail::get_type_info: unable to find type info for \""
229+
+ std::move(tname) + '"');
230230
}
231231
return nullptr;
232232
}
@@ -512,9 +512,13 @@ PYBIND11_NOINLINE std::string error_string() {
512512
Py_INCREF(f_code);
513513
# endif
514514
int lineno = PyFrame_GetLineNumber(frame);
515-
errorString += " " + handle(f_code->co_filename).cast<std::string>() + "("
516-
+ std::to_string(lineno)
517-
+ "): " + handle(f_code->co_name).cast<std::string>() + "\n";
515+
errorString += " ";
516+
errorString += handle(f_code->co_filename).cast<std::string>();
517+
errorString += '(';
518+
errorString += std::to_string(lineno);
519+
errorString += "): ";
520+
errorString += handle(f_code->co_name).cast<std::string>();
521+
errorString += '\n';
518522
Py_DECREF(f_code);
519523
# if PY_VERSION_HEX >= 0x030900B1
520524
auto *b_frame = PyFrame_GetBack(frame);

include/pybind11/eigen.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
668668
Type::Flags &(Eigen::RowMajor | Eigen::ColMajor),
669669
StorageIndex>(shape[0].cast<Index>(),
670670
shape[1].cast<Index>(),
671-
nnz,
671+
std::move(nnz),
672672
outerIndices.mutable_data(),
673673
innerIndices.mutable_data(),
674674
values.mutable_data());
@@ -686,7 +686,8 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
686686
array outerIndices((rowMajor ? src.rows() : src.cols()) + 1, src.outerIndexPtr());
687687
array innerIndices(src.nonZeros(), src.innerIndexPtr());
688688

689-
return matrix_type(std::make_tuple(data, innerIndices, outerIndices),
689+
return matrix_type(std::make_tuple(
690+
std::move(data), std::move(innerIndices), std::move(outerIndices)),
690691
std::make_pair(src.rows(), src.cols()))
691692
.release();
692693
}

include/pybind11/numpy.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ class array : public buffer {
940940

941941
void fail_dim_check(ssize_t dim, const std::string &msg) const {
942942
throw index_error(msg + ": " + std::to_string(dim) + " (ndim = " + std::to_string(ndim())
943-
+ ")");
943+
+ ')');
944944
}
945945

946946
template <typename... Ix>
@@ -1144,11 +1144,11 @@ struct format_descriptor<T, detail::enable_if_t<detail::is_pod_struct<T>::value>
11441144

11451145
template <size_t N>
11461146
struct format_descriptor<char[N]> {
1147-
static std::string format() { return std::to_string(N) + "s"; }
1147+
static std::string format() { return std::to_string(N) + 's'; }
11481148
};
11491149
template <size_t N>
11501150
struct format_descriptor<std::array<char, N>> {
1151-
static std::string format() { return std::to_string(N) + "s"; }
1151+
static std::string format() { return std::to_string(N) + 's'; }
11521152
};
11531153

11541154
template <typename T>

include/pybind11/pybind11.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,14 @@ class cpp_function : public function {
561561
for (auto *it = chain_start; it != nullptr; it = it->next) {
562562
if (options::show_function_signatures()) {
563563
if (index > 0) {
564-
signatures += "\n";
564+
signatures += '\n';
565565
}
566566
if (chain) {
567567
signatures += std::to_string(++index) + ". ";
568568
}
569569
signatures += rec->name;
570570
signatures += it->signature;
571-
signatures += "\n";
571+
signatures += '\n';
572572
}
573573
if (it->doc && it->doc[0] != '\0' && options::show_user_defined_docstrings()) {
574574
// If we're appending another docstring, and aren't printing function signatures,
@@ -577,15 +577,15 @@ class cpp_function : public function {
577577
if (first_user_def) {
578578
first_user_def = false;
579579
} else {
580-
signatures += "\n";
580+
signatures += '\n';
581581
}
582582
}
583583
if (options::show_function_signatures()) {
584-
signatures += "\n";
584+
signatures += '\n';
585585
}
586586
signatures += it->doc;
587587
if (options::show_function_signatures()) {
588-
signatures += "\n";
588+
signatures += '\n';
589589
}
590590
}
591591
}
@@ -1055,7 +1055,7 @@ class cpp_function : public function {
10551055
msg += it2->signature;
10561056
}
10571057

1058-
msg += "\n";
1058+
msg += '\n';
10591059
}
10601060
msg += "\nInvoked with: ";
10611061
auto args_ = reinterpret_borrow<tuple>(args_in);

0 commit comments

Comments
 (0)