Reduce number of new / delete operations for class LanguageModel

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2017-05-19 18:31:37 +02:00
parent 562de89728
commit e6d683923c
2 changed files with 25 additions and 30 deletions

View File

@ -118,20 +118,15 @@ LanguageModel::LanguageModel(const UnicityTable<FontInfo> *fontinfo_table,
BOOL_INIT_MEMBER(language_model_use_sigmoidal_certainty, false, BOOL_INIT_MEMBER(language_model_use_sigmoidal_certainty, false,
"Use sigmoidal score for certainty", "Use sigmoidal score for certainty",
dict->getCCUtil()->params()), dict->getCCUtil()->params()),
dawg_args_(nullptr, new DawgPositionVector(), NO_PERM),
fontinfo_table_(fontinfo_table), dict_(dict), fontinfo_table_(fontinfo_table), dict_(dict),
fixed_pitch_(false), max_char_wh_ratio_(0.0), fixed_pitch_(false), max_char_wh_ratio_(0.0),
acceptable_choice_found_(false) { acceptable_choice_found_(false) {
ASSERT_HOST(dict_ != NULL); ASSERT_HOST(dict_ != NULL);
dawg_args_ = new DawgArgs(NULL, new DawgPositionVector(), NO_PERM);
very_beginning_active_dawgs_ = new DawgPositionVector();
beginning_active_dawgs_ = new DawgPositionVector();
} }
LanguageModel::~LanguageModel() { LanguageModel::~LanguageModel() {
delete very_beginning_active_dawgs_; delete dawg_args_.updated_dawgs;
delete beginning_active_dawgs_;
delete dawg_args_->updated_dawgs;
delete dawg_args_;
} }
void LanguageModel::InitForWord(const WERD_CHOICE *prev_word, void LanguageModel::InitForWord(const WERD_CHOICE *prev_word,
@ -144,10 +139,10 @@ void LanguageModel::InitForWord(const WERD_CHOICE *prev_word,
correct_segmentation_explored_ = false; correct_segmentation_explored_ = false;
// Initialize vectors with beginning DawgInfos. // Initialize vectors with beginning DawgInfos.
very_beginning_active_dawgs_->clear(); very_beginning_active_dawgs_.clear();
dict_->init_active_dawgs(very_beginning_active_dawgs_, false); dict_->init_active_dawgs(&very_beginning_active_dawgs_, false);
beginning_active_dawgs_->clear(); beginning_active_dawgs_.clear();
dict_->default_dawgs(beginning_active_dawgs_, false); dict_->default_dawgs(&beginning_active_dawgs_, false);
// Fill prev_word_str_ with the last language_model_ngram_order // Fill prev_word_str_ with the last language_model_ngram_order
// unichars from prev_word. // unichars from prev_word.
@ -791,18 +786,18 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
// Initialize active_dawgs from parent_vse if it is not NULL. // Initialize active_dawgs from parent_vse if it is not NULL.
// Otherwise use very_beginning_active_dawgs_. // Otherwise use very_beginning_active_dawgs_.
if (parent_vse == NULL) { if (parent_vse == NULL) {
dawg_args_->active_dawgs = very_beginning_active_dawgs_; dawg_args_.active_dawgs = &very_beginning_active_dawgs_;
dawg_args_->permuter = NO_PERM; dawg_args_.permuter = NO_PERM;
} else { } else {
if (parent_vse->dawg_info == NULL) return NULL; // not a dict word path if (parent_vse->dawg_info == NULL) return NULL; // not a dict word path
dawg_args_->active_dawgs = &parent_vse->dawg_info->active_dawgs; dawg_args_.active_dawgs = &parent_vse->dawg_info->active_dawgs;
dawg_args_->permuter = parent_vse->dawg_info->permuter; dawg_args_.permuter = parent_vse->dawg_info->permuter;
} }
// Deal with hyphenated words. // Deal with hyphenated words.
if (word_end && dict_->has_hyphen_end(b.unichar_id(), curr_col == 0)) { if (word_end && dict_->has_hyphen_end(b.unichar_id(), curr_col == 0)) {
if (language_model_debug_level > 0) tprintf("Hyphenated word found\n"); if (language_model_debug_level > 0) tprintf("Hyphenated word found\n");
return new LanguageModelDawgInfo(dawg_args_->active_dawgs, return new LanguageModelDawgInfo(dawg_args_.active_dawgs,
COMPOUND_PERM); COMPOUND_PERM);
} }
@ -815,7 +810,7 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
// Do not allow compounding of words with lengths shorter than // Do not allow compounding of words with lengths shorter than
// language_model_min_compound_length // language_model_min_compound_length
if (parent_vse == NULL || word_end || if (parent_vse == NULL || word_end ||
dawg_args_->permuter == COMPOUND_PERM || dawg_args_.permuter == COMPOUND_PERM ||
parent_vse->length < language_model_min_compound_length) return NULL; parent_vse->length < language_model_min_compound_length) return NULL;
int i; int i;
@ -835,7 +830,7 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
if (!has_word_ending) return NULL; if (!has_word_ending) return NULL;
if (language_model_debug_level > 0) tprintf("Compound word found\n"); if (language_model_debug_level > 0) tprintf("Compound word found\n");
return new LanguageModelDawgInfo(beginning_active_dawgs_, COMPOUND_PERM); return new LanguageModelDawgInfo(&beginning_active_dawgs_, COMPOUND_PERM);
} // done dealing with compound words } // done dealing with compound words
LanguageModelDawgInfo *dawg_info = NULL; LanguageModelDawgInfo *dawg_info = NULL;
@ -850,22 +845,22 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
if (language_model_debug_level > 2) if (language_model_debug_level > 2)
tprintf("Test Letter OK for unichar %d, normed %d\n", tprintf("Test Letter OK for unichar %d, normed %d\n",
b.unichar_id(), normed_ids[i]); b.unichar_id(), normed_ids[i]);
dict_->LetterIsOkay(dawg_args_, normed_ids[i], dict_->LetterIsOkay(&dawg_args_, normed_ids[i],
word_end && i == normed_ids.size() - 1); word_end && i == normed_ids.size() - 1);
if (dawg_args_->permuter == NO_PERM) { if (dawg_args_.permuter == NO_PERM) {
break; break;
} else if (i < normed_ids.size() - 1) { } else if (i < normed_ids.size() - 1) {
tmp_active_dawgs = *dawg_args_->updated_dawgs; tmp_active_dawgs = *dawg_args_.updated_dawgs;
dawg_args_->active_dawgs = &tmp_active_dawgs; dawg_args_.active_dawgs = &tmp_active_dawgs;
} }
if (language_model_debug_level > 2) if (language_model_debug_level > 2)
tprintf("Letter was OK for unichar %d, normed %d\n", tprintf("Letter was OK for unichar %d, normed %d\n",
b.unichar_id(), normed_ids[i]); b.unichar_id(), normed_ids[i]);
} }
dawg_args_->active_dawgs = NULL; dawg_args_.active_dawgs = nullptr;
if (dawg_args_->permuter != NO_PERM) { if (dawg_args_.permuter != NO_PERM) {
dawg_info = new LanguageModelDawgInfo(dawg_args_->updated_dawgs, dawg_info = new LanguageModelDawgInfo(dawg_args_.updated_dawgs,
dawg_args_->permuter); dawg_args_.permuter);
} else if (language_model_debug_level > 3) { } else if (language_model_debug_level > 3) {
tprintf("Letter %s not OK!\n", tprintf("Letter %s not OK!\n",
dict_->getUnicharset().id_to_unichar(b.unichar_id())); dict_->getUnicharset().id_to_unichar(b.unichar_id()));
@ -1320,7 +1315,7 @@ void LanguageModel::UpdateBestChoice(
// Update hyphen state if we are dealing with a dictionary word. // Update hyphen state if we are dealing with a dictionary word.
if (vse->dawg_info != NULL) { if (vse->dawg_info != NULL) {
if (dict_->has_hyphen_end(*word)) { if (dict_->has_hyphen_end(*word)) {
dict_->set_hyphen_word(*word, *(dawg_args_->active_dawgs)); dict_->set_hyphen_word(*word, *(dawg_args_.active_dawgs));
} else { } else {
dict_->reset_hyphen_vars(true); dict_->reset_hyphen_vars(true);
} }

View File

@ -361,7 +361,7 @@ class LanguageModel {
// Temporary DawgArgs struct that is re-used across different words to // Temporary DawgArgs struct that is re-used across different words to
// avoid dynamic memory re-allocation (should be cleared before each use). // avoid dynamic memory re-allocation (should be cleared before each use).
DawgArgs *dawg_args_; DawgArgs dawg_args_;
// Scaling for recovering blob outline length from rating and certainty. // Scaling for recovering blob outline length from rating and certainty.
float rating_cert_scale_; float rating_cert_scale_;
@ -392,8 +392,8 @@ class LanguageModel {
STRING prev_word_str_; STRING prev_word_str_;
int prev_word_unichar_step_len_; int prev_word_unichar_step_len_;
// Active dawg vector. // Active dawg vector.
DawgPositionVector *very_beginning_active_dawgs_; // includes continuation DawgPositionVector very_beginning_active_dawgs_; // includes continuation
DawgPositionVector *beginning_active_dawgs_; DawgPositionVector beginning_active_dawgs_;
// Set to true if acceptable choice was discovered. // Set to true if acceptable choice was discovered.
// Note: it would be nice to use this to terminate the search once an // Note: it would be nice to use this to terminate the search once an
// acceptable choices is found. However we do not do that and once an // acceptable choices is found. However we do not do that and once an