Skip to content

Commit

Permalink
Optimize code by replacing init_to_size with resize_no_init
Browse files Browse the repository at this point in the history
There is no need to initialize memory with a fixed value which is
overwritten in the next step.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed May 12, 2017
1 parent bb2348b commit 3a67ff9
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ccmain/reject.cpp
Expand Up @@ -232,7 +232,7 @@ float compute_reject_threshold(WERD_CHOICE* word) {

int blob_count = word->length();
GenericVector<float> ratings;
ratings.init_to_size(blob_count, 0.0f);
ratings.resize_no_init(blob_count);
for (int i = 0; i < blob_count; ++i) {
ratings[i] = word->certainty(i);
}
Expand Down
4 changes: 2 additions & 2 deletions ccstruct/imagedata.cpp
Expand Up @@ -139,7 +139,7 @@ ImageData* ImageData::Build(const char* name, int page_number, const char* lang,
image_data->page_number_ = page_number;
image_data->language_ = lang;
// Save the imagedata.
image_data->image_data_.init_to_size(imagedatasize, 0);
image_data->image_data_.resize_no_init(imagedatasize);
memcpy(&image_data->image_data_[0], imagedata, imagedatasize);
if (!image_data->AddBoxes(box_text)) {
if (truth_text == NULL || truth_text[0] == '\0') {
Expand Down Expand Up @@ -329,7 +329,7 @@ void ImageData::SetPixInternal(Pix* pix, GenericVector<char>* image_data) {
size_t size;
pixWriteMem(&data, &size, pix, IFF_PNG);
pixDestroy(&pix);
image_data->init_to_size(size, 0);
image_data->resize_no_init(size);
memcpy(&(*image_data)[0], data, size);
free(data);
}
Expand Down
4 changes: 2 additions & 2 deletions ccutil/serialis.cpp
Expand Up @@ -57,7 +57,7 @@ bool TFile::Open(const char* data, int size) {
}
is_writing_ = false;
swap_ = false;
data_->init_to_size(size, 0);
data_->resize_no_init(size);
memcpy(&(*data_)[0], data, size);
return true;
}
Expand All @@ -79,7 +79,7 @@ bool TFile::Open(FILE* fp, inT64 end_offset) {
data_ = new GenericVector<char>;
data_is_owned_ = true;
}
data_->init_to_size(size, 0);
data_->resize_no_init(size);
return static_cast<int>(fread(&(*data_)[0], 1, size, fp)) == size;
}

Expand Down
6 changes: 3 additions & 3 deletions ccutil/tessdatamanager.cpp
Expand Up @@ -62,7 +62,7 @@ bool TessdataManager::LoadMemBuffer(const char *name, const char *data,
fp.set_swap(swap_);
if (swap_) ReverseN(&num_entries, sizeof(num_entries));
GenericVector<inT64> offset_table;
offset_table.init_to_size(num_entries, -1);
offset_table.resize_no_init(num_entries);
if (fp.FReadEndian(&offset_table[0], sizeof(offset_table[0]), num_entries) !=
num_entries)
return false;
Expand All @@ -72,7 +72,7 @@ bool TessdataManager::LoadMemBuffer(const char *name, const char *data,
int j = i + 1;
while (j < num_entries && offset_table[j] == -1) ++j;
if (j < num_entries) entry_size = offset_table[j] - offset_table[i];
entries_[i].init_to_size(entry_size, 0);
entries_[i].resize_no_init(entry_size);
if (fp.FRead(&entries_[i][0], 1, entry_size) != entry_size) return false;
}
}
Expand All @@ -84,7 +84,7 @@ bool TessdataManager::LoadMemBuffer(const char *name, const char *data,
void TessdataManager::OverwriteEntry(TessdataType type, const char *data,
int size) {
is_loaded_ = true;
entries_[type].init_to_size(size, 0);
entries_[type].resize_no_init(size);
memcpy(&entries_[type][0], data, size);
}

Expand Down
2 changes: 1 addition & 1 deletion lstm/tfnetwork.cpp
Expand Up @@ -46,7 +46,7 @@ bool TFNetwork::Serialize(TFile* fp) const {
string proto_str;
model_proto_.SerializeToString(&proto_str);
GenericVector<char> data;
data.init_to_size(proto_str.size(), 0);
data.resize_no_init(proto_str.size());
memcpy(&data[0], proto_str.data(), proto_str.size());
if (!data.Serialize(fp)) return false;
return true;
Expand Down
2 changes: 1 addition & 1 deletion lstm/weightmatrix.cpp
Expand Up @@ -150,7 +150,7 @@ bool WeightMatrix::DeSerializeOld(bool training, TFile* fp) {
if (!wi_.DeSerialize(fp)) return false;
GenericVector<float> old_scales;
if (!old_scales.DeSerialize(fp)) return false;
scales_.init_to_size(old_scales.size(), 0.0);
scales_.resize_no_init(old_scales.size());
for (int i = 0; i < old_scales.size(); ++i) scales_[i] = old_scales[i];
} else {
if (!float_array.DeSerialize(fp)) return false;
Expand Down

0 comments on commit 3a67ff9

Please sign in to comment.