Skip to content

Commit

Permalink
Merge pull request #352 from pnordhus/reduce_mallocs
Browse files Browse the repository at this point in the history
Avoid unnecessary memory allocations
  • Loading branch information
zdenop committed Apr 30, 2017
2 parents a28b2a0 + 907de59 commit fd3f8f9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
6 changes: 3 additions & 3 deletions ccutil/genericvector.h
Expand Up @@ -37,9 +37,9 @@
template <typename T>
class GenericVector {
public:
GenericVector() {
init(kDefaultVectorSize);
}
GenericVector() : size_used_(0), size_reserved_(0), data_(NULL),
clear_cb_(NULL), compare_cb_(NULL) {}

GenericVector(int size, T init_val) {
init(size);
init_to_size(size, init_val);
Expand Down
6 changes: 3 additions & 3 deletions wordrec/language_model.cpp
Expand Up @@ -796,7 +796,7 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
dawg_args_->permuter = NO_PERM;
} else {
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;
}

Expand All @@ -822,8 +822,8 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
int i;
// Check a that the path terminated before the current character is a word.
bool has_word_ending = false;
for (i = 0; i < parent_vse->dawg_info->active_dawgs->size(); ++i) {
const DawgPosition &pos = (*parent_vse->dawg_info->active_dawgs)[i];
for (i = 0; i < parent_vse->dawg_info->active_dawgs.size(); ++i) {
const DawgPosition &pos = parent_vse->dawg_info->active_dawgs[i];
const Dawg *pdawg = pos.dawg_index < 0
? NULL : dict_->GetDawg(pos.dawg_index);
if (pdawg == NULL || pos.back_to_punc) continue;;
Expand Down
10 changes: 3 additions & 7 deletions wordrec/lm_state.h
Expand Up @@ -59,13 +59,9 @@ typedef unsigned char LanguageModelFlagsType;
/// component. It stores the set of active dawgs in which the sequence of
/// letters on a path can be found.
struct LanguageModelDawgInfo {
LanguageModelDawgInfo(DawgPositionVector *a, PermuterType pt) : permuter(pt) {
active_dawgs = new DawgPositionVector(*a);
}
~LanguageModelDawgInfo() {
delete active_dawgs;
}
DawgPositionVector *active_dawgs;
LanguageModelDawgInfo(const DawgPositionVector *a, PermuterType pt)
: active_dawgs(*a), permuter(pt) {}
DawgPositionVector active_dawgs;
PermuterType permuter;
};

Expand Down

0 comments on commit fd3f8f9

Please sign in to comment.