Skip to content

Commit

Permalink
Dict::AcceptableResult(): minimal performance optimization when `stop…
Browse files Browse the repository at this point in the history
…per_no_acceptable_choices` is set: no need to do those calculations as the expected answer is already known.
  • Loading branch information
GerHobbelt committed Jul 27, 2024
1 parent b9d3d62 commit 5b8c08f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/dict/dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class TESS_API Dict : public DictSettings {
/// Returns false if the best choice for the current word is questionable
/// and should be tried again on the second pass or should be flagged to
/// the user.
bool AcceptableResult(WERD_RES *word) const;
bool AcceptableResult(const WERD_RES *word) const;
#if !DISABLED_LEGACY_ENGINE
void EndDangerousAmbigs();
#endif // !DISABLED_LEGACY_ENGINE
Expand Down
13 changes: 8 additions & 5 deletions src/dict/stopper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ bool Dict::AcceptableChoice(const WERD_CHOICE &best_choice,
}
}

bool Dict::AcceptableResult(WERD_RES *word) const {
bool Dict::AcceptableResult(const WERD_RES *word) const {
if (word->best_choice == nullptr) {
return false;
}
if (stopper_no_acceptable_choices) {
return false;
}

float CertaintyThreshold = stopper_nondict_certainty_base - reject_offset_;
int WordSize;

Expand All @@ -133,18 +137,17 @@ bool Dict::AcceptableResult(WERD_RES *word) const {
if (valid_word(*word->best_choice) && case_ok(*word->best_choice)) {
WordSize = LengthOfShortestAlphaRun(*word->best_choice);
WordSize -= stopper_smallword_size;
if (WordSize < 0) {
WordSize = 0;
if (WordSize > 0) {
CertaintyThreshold += WordSize * stopper_certainty_per_char;
}
CertaintyThreshold += WordSize * stopper_certainty_per_char;
}

if (stopper_debug_level >= 1) {
tprintDebug("Rejecter: Certainty = {}, Threshold = {} ", word->best_choice->certainty(),
CertaintyThreshold);
}

if (word->best_choice->certainty() > CertaintyThreshold && !stopper_no_acceptable_choices) {
if (word->best_choice->certainty() > CertaintyThreshold) {
if (stopper_debug_level >= 1) {
tprintDebug("ACCEPTED\n");
}
Expand Down

0 comments on commit 5b8c08f

Please sign in to comment.