Skip to content

Commit e6d6839

Browse files
committed
Reduce number of new / delete operations for class LanguageModel
Signed-off-by: Stefan Weil <[email protected]>
1 parent 562de89 commit e6d6839

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

wordrec/language_model.cpp

+22-27
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,15 @@ LanguageModel::LanguageModel(const UnicityTable<FontInfo> *fontinfo_table,
118118
BOOL_INIT_MEMBER(language_model_use_sigmoidal_certainty, false,
119119
"Use sigmoidal score for certainty",
120120
dict->getCCUtil()->params()),
121+
dawg_args_(nullptr, new DawgPositionVector(), NO_PERM),
121122
fontinfo_table_(fontinfo_table), dict_(dict),
122123
fixed_pitch_(false), max_char_wh_ratio_(0.0),
123124
acceptable_choice_found_(false) {
124125
ASSERT_HOST(dict_ != NULL);
125-
dawg_args_ = new DawgArgs(NULL, new DawgPositionVector(), NO_PERM);
126-
very_beginning_active_dawgs_ = new DawgPositionVector();
127-
beginning_active_dawgs_ = new DawgPositionVector();
128126
}
129127

130128
LanguageModel::~LanguageModel() {
131-
delete very_beginning_active_dawgs_;
132-
delete beginning_active_dawgs_;
133-
delete dawg_args_->updated_dawgs;
134-
delete dawg_args_;
129+
delete dawg_args_.updated_dawgs;
135130
}
136131

137132
void LanguageModel::InitForWord(const WERD_CHOICE *prev_word,
@@ -144,10 +139,10 @@ void LanguageModel::InitForWord(const WERD_CHOICE *prev_word,
144139
correct_segmentation_explored_ = false;
145140

146141
// Initialize vectors with beginning DawgInfos.
147-
very_beginning_active_dawgs_->clear();
148-
dict_->init_active_dawgs(very_beginning_active_dawgs_, false);
149-
beginning_active_dawgs_->clear();
150-
dict_->default_dawgs(beginning_active_dawgs_, false);
142+
very_beginning_active_dawgs_.clear();
143+
dict_->init_active_dawgs(&very_beginning_active_dawgs_, false);
144+
beginning_active_dawgs_.clear();
145+
dict_->default_dawgs(&beginning_active_dawgs_, false);
151146

152147
// Fill prev_word_str_ with the last language_model_ngram_order
153148
// unichars from prev_word.
@@ -791,18 +786,18 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
791786
// Initialize active_dawgs from parent_vse if it is not NULL.
792787
// Otherwise use very_beginning_active_dawgs_.
793788
if (parent_vse == NULL) {
794-
dawg_args_->active_dawgs = very_beginning_active_dawgs_;
795-
dawg_args_->permuter = NO_PERM;
789+
dawg_args_.active_dawgs = &very_beginning_active_dawgs_;
790+
dawg_args_.permuter = NO_PERM;
796791
} else {
797792
if (parent_vse->dawg_info == NULL) return NULL; // not a dict word path
798-
dawg_args_->active_dawgs = &parent_vse->dawg_info->active_dawgs;
799-
dawg_args_->permuter = parent_vse->dawg_info->permuter;
793+
dawg_args_.active_dawgs = &parent_vse->dawg_info->active_dawgs;
794+
dawg_args_.permuter = parent_vse->dawg_info->permuter;
800795
}
801796

802797
// Deal with hyphenated words.
803798
if (word_end && dict_->has_hyphen_end(b.unichar_id(), curr_col == 0)) {
804799
if (language_model_debug_level > 0) tprintf("Hyphenated word found\n");
805-
return new LanguageModelDawgInfo(dawg_args_->active_dawgs,
800+
return new LanguageModelDawgInfo(dawg_args_.active_dawgs,
806801
COMPOUND_PERM);
807802
}
808803

@@ -815,7 +810,7 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
815810
// Do not allow compounding of words with lengths shorter than
816811
// language_model_min_compound_length
817812
if (parent_vse == NULL || word_end ||
818-
dawg_args_->permuter == COMPOUND_PERM ||
813+
dawg_args_.permuter == COMPOUND_PERM ||
819814
parent_vse->length < language_model_min_compound_length) return NULL;
820815

821816
int i;
@@ -835,7 +830,7 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
835830
if (!has_word_ending) return NULL;
836831

837832
if (language_model_debug_level > 0) tprintf("Compound word found\n");
838-
return new LanguageModelDawgInfo(beginning_active_dawgs_, COMPOUND_PERM);
833+
return new LanguageModelDawgInfo(&beginning_active_dawgs_, COMPOUND_PERM);
839834
} // done dealing with compound words
840835

841836
LanguageModelDawgInfo *dawg_info = NULL;
@@ -850,22 +845,22 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
850845
if (language_model_debug_level > 2)
851846
tprintf("Test Letter OK for unichar %d, normed %d\n",
852847
b.unichar_id(), normed_ids[i]);
853-
dict_->LetterIsOkay(dawg_args_, normed_ids[i],
848+
dict_->LetterIsOkay(&dawg_args_, normed_ids[i],
854849
word_end && i == normed_ids.size() - 1);
855-
if (dawg_args_->permuter == NO_PERM) {
850+
if (dawg_args_.permuter == NO_PERM) {
856851
break;
857852
} else if (i < normed_ids.size() - 1) {
858-
tmp_active_dawgs = *dawg_args_->updated_dawgs;
859-
dawg_args_->active_dawgs = &tmp_active_dawgs;
853+
tmp_active_dawgs = *dawg_args_.updated_dawgs;
854+
dawg_args_.active_dawgs = &tmp_active_dawgs;
860855
}
861856
if (language_model_debug_level > 2)
862857
tprintf("Letter was OK for unichar %d, normed %d\n",
863858
b.unichar_id(), normed_ids[i]);
864859
}
865-
dawg_args_->active_dawgs = NULL;
866-
if (dawg_args_->permuter != NO_PERM) {
867-
dawg_info = new LanguageModelDawgInfo(dawg_args_->updated_dawgs,
868-
dawg_args_->permuter);
860+
dawg_args_.active_dawgs = nullptr;
861+
if (dawg_args_.permuter != NO_PERM) {
862+
dawg_info = new LanguageModelDawgInfo(dawg_args_.updated_dawgs,
863+
dawg_args_.permuter);
869864
} else if (language_model_debug_level > 3) {
870865
tprintf("Letter %s not OK!\n",
871866
dict_->getUnicharset().id_to_unichar(b.unichar_id()));
@@ -1320,7 +1315,7 @@ void LanguageModel::UpdateBestChoice(
13201315
// Update hyphen state if we are dealing with a dictionary word.
13211316
if (vse->dawg_info != NULL) {
13221317
if (dict_->has_hyphen_end(*word)) {
1323-
dict_->set_hyphen_word(*word, *(dawg_args_->active_dawgs));
1318+
dict_->set_hyphen_word(*word, *(dawg_args_.active_dawgs));
13241319
} else {
13251320
dict_->reset_hyphen_vars(true);
13261321
}

wordrec/language_model.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ class LanguageModel {
361361

362362
// Temporary DawgArgs struct that is re-used across different words to
363363
// avoid dynamic memory re-allocation (should be cleared before each use).
364-
DawgArgs *dawg_args_;
364+
DawgArgs dawg_args_;
365365
// Scaling for recovering blob outline length from rating and certainty.
366366
float rating_cert_scale_;
367367

@@ -392,8 +392,8 @@ class LanguageModel {
392392
STRING prev_word_str_;
393393
int prev_word_unichar_step_len_;
394394
// Active dawg vector.
395-
DawgPositionVector *very_beginning_active_dawgs_; // includes continuation
396-
DawgPositionVector *beginning_active_dawgs_;
395+
DawgPositionVector very_beginning_active_dawgs_; // includes continuation
396+
DawgPositionVector beginning_active_dawgs_;
397397
// Set to true if acceptable choice was discovered.
398398
// Note: it would be nice to use this to terminate the search once an
399399
// acceptable choices is found. However we do not do that and once an

0 commit comments

Comments
 (0)