Skip to content

Commit

Permalink
Fix a number of performance issues (reported by Coverity Scan)
Browse files Browse the repository at this point in the history
Coverity Scan reports "Unnecessary object copies can affect performance"
and suggests using the auto keyword with an &.

In many cases the const attribute can also be added.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Nov 19, 2022
1 parent 4c0051d commit e5b400f
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/api/hocrrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ char *TessBaseAPI::GetHOCRText(ETEXT_DESC *monitor, int page_number) {
<< " id='"
<< "timestep" << page_id << "_" << wcnt << "_" << tcnt
<< "'>";
for (auto conf : timestep) {
for (const auto &conf : timestep) {
hocr_str << "\n <span class='ocrx_cinfo'"
<< " id='"
<< "choice_" << page_id << "_" << wcnt << "_" << ccnt
Expand Down
2 changes: 1 addition & 1 deletion src/ccmain/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ bool Tesseract::SelectGoodDiacriticOutlines(int pass, float certainty_threshold,
*ok_outlines = best_outlines;
if (debug_noise_removal) {
tprintf("%s noise combination ", blob ? "Adding" : "New");
for (auto best_outline : best_outlines) {
for (const auto &best_outline : best_outlines) {
tprintf("%c", best_outline ? 'T' : 'F');
}
tprintf(" yields certainty %g, beating target of %g\n", best_cert, target_cert);
Expand Down
2 changes: 1 addition & 1 deletion src/ccstruct/fontinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool FontInfoTable::DeSerialize(TFile *fp) {
bool FontInfoTable::SetContainsFontProperties(int font_id,
const std::vector<ScoredFont> &font_set) const {
uint32_t properties = at(font_id).properties;
for (auto f : font_set) {
for (const auto &f : font_set) {
if (at(f.fontinfo_id).properties == properties) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ccutil/serialis.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class TESS_API TFile {
return false;
} else if constexpr (std::is_same<T, std::string>::value) {
// Serialize strings.
for (auto string : data) {
for (const auto &string : data) {
if (!Serialize(string)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/classify/adaptmatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ void Classify::MasterMatcher(INT_TEMPLATES_STRUCT *templates, int16_t num_featur
int top = blob_box.top();
int bottom = blob_box.bottom();
UnicharRating int_result;
for (auto result : results) {
for (const auto &result : results) {
CLASS_ID class_id = result.Class;
BIT_VECTOR protos = classes != nullptr ? classes[class_id]->PermProtos : AllProtosOn;
BIT_VECTOR configs = classes != nullptr ? classes[class_id]->PermConfigs : AllConfigsOn;
Expand Down
2 changes: 1 addition & 1 deletion src/classify/shapeclassifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void ShapeClassifier::UnicharPrintResults(const char *context,
GetUnicharset().id_to_unichar(result.unichar_id));
if (!result.fonts.empty()) {
tprintf(" Font Vector:");
for (auto font : result.fonts) {
for (const auto &font : result.fonts) {
tprintf(" %d", font.fontinfo_id);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/dict/dawg.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class DawgPositionVector : public std::vector<DawgPosition> {
/// true otherwise.
inline bool add_unique(const DawgPosition &new_pos, bool debug,
const char *debug_msg) {
for (auto position : *this) {
for (const auto &position : *this) {
if (position == new_pos) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lstm/networkio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void NetworkIO::FromPixes(const StaticShape &shape, const std::vector<Image> &pi
int target_height = shape.height();
int target_width = shape.width();
std::vector<std::pair<int, int>> h_w_pairs;
for (auto pix : pixes) {
for (const auto &pix : pixes) {
Image var_pix = pix;
int width = pixGetWidth(var_pix);
if (target_width != 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/lstm/recodebeam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ void RecodeBeamSearch::PrintBeam2(bool uids, int num_outputs,
// fill the topology with depths first
for (int step = beam.size() - 1; step >= 0; --step) {
std::vector<tesseract::RecodePair> &heaps = beam.at(step)->beams_->heap();
for (auto node : heaps) {
for (const auto &node : heaps) {
int backtracker = 0;
const RecodeNode *curr = &node.data();
while (curr != nullptr && !visited.count(curr)) {
Expand Down Expand Up @@ -426,7 +426,7 @@ void RecodeBeamSearch::extractSymbolChoices(const UNICHARSET *unicharset) {
std::vector<const RecodeNode *> best_nodes;
std::vector<const RecodeNode *> best;
// Scan the segmented node chain for valid unichar ids.
for (auto entry : heaps) {
for (const auto &entry : heaps) {
bool validChar = false;
int backcounter = 0;
const RecodeNode *node = &entry.data();
Expand Down
2 changes: 1 addition & 1 deletion src/training/pango/pango_font_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ int FontUtils::FontScore(const std::unordered_map<char32, int64_t> &ch_map,
}
*raw_score = 0;
int ok_chars = 0;
for (auto it : ch_map) {
for (auto &it : ch_map) {
bool covered =
(coverage != nullptr) && (IsWhitespace(it.first) ||
(pango_coverage_get(coverage, it.first) == PANGO_COVERAGE_EXACT));
Expand Down
2 changes: 1 addition & 1 deletion src/wordrec/chopper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ SEAM *Wordrec::chop_overlapping_blob(const std::vector<TBOX> &boxes, bool italic

bool almost_equal_box = false;
int num_overlap = 0;
for (auto boxe : boxes) {
for (const auto &boxe : boxes) {
if (original_box.overlap_fraction(boxe) > 0.125) {
num_overlap++;
}
Expand Down
4 changes: 2 additions & 2 deletions src/wordrec/lm_pain_points.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void LMPainPoints::GenerateFromAmbigs(const DANGERR &fixpt, ViterbiStateEntry *v
WERD_RES *word_res) {
// Begins and ends in DANGERR vector now record the blob indices as used
// by the ratings matrix.
for (auto danger : fixpt) {
for (const auto &danger : fixpt) {
// Only use dangerous ambiguities.
if (danger.dangerous) {
GeneratePainPoint(danger.begin, danger.end - 1, LM_PPTYPE_AMBIG, vse->cost, true,
Expand Down Expand Up @@ -203,7 +203,7 @@ bool LMPainPoints::GeneratePainPoint(int col, int row, LMPainPointsType pp_type,
void LMPainPoints::RemapForSplit(int index) {
for (auto &pain_points_heap : pain_points_heaps_) {
std::vector<MatrixCoordPair> &heap = pain_points_heap.heap();
for (auto entry : heap) {
for (auto &entry : heap) {
entry.data().MapForSplit(index);
}
}
Expand Down

0 comments on commit e5b400f

Please sign in to comment.