Commit 7a51f364 authored by shess@chromium.org's avatar shess@chromium.org

Reserve space before building potentially large vectors in safe-browsing.

In cases where we can approximate the vector size in advance,
reserve() that much space rather than building organically.

BUG=97102
TEST=Reduction in crash rate for that bug.


Review URL: http://codereview.chromium.org/8005001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@102435 0039d316-1c4b-4281-b951-d872f2087c98
parent 19868d29
......@@ -42,6 +42,13 @@ namespace safe_browsing {
PrefixSet::PrefixSet(const std::vector<SBPrefix>& sorted_prefixes)
: checksum_(0) {
if (sorted_prefixes.size()) {
// Estimate the resulting vector sizes. There will be strictly
// more than |min_runs| entries in |index_|, but there generally
// aren't many forced breaks.
const size_t min_runs = sorted_prefixes.size() / kMaxRun;
index_.reserve(min_runs);
deltas_.reserve(sorted_prefixes.size() - min_runs);
// Lead with the first prefix.
SBPrefix prev_prefix = sorted_prefixes[0];
size_t run_length = 0;
......@@ -141,6 +148,8 @@ bool PrefixSet::Exists(SBPrefix prefix) const {
}
void PrefixSet::GetPrefixes(std::vector<SBPrefix>* prefixes) const {
prefixes->reserve(index_.size() + deltas_.size());
for (size_t ii = 0; ii < index_.size(); ++ii) {
// The deltas for this |index_| entry run to the next index entry,
// or the end of the deltas.
......
......@@ -269,6 +269,7 @@ safe_browsing::PrefixSet* PrefixSetFromAddPrefixes(
// |prefixes|. For now, |prefixes| is useful while debugging
// things.
std::vector<SBPrefix> prefixes;
prefixes.reserve(add_prefixes.size());
for (size_t i = 0; i < add_prefixes.size(); ++i) {
prefixes.push_back(add_prefixes[i].prefix);
}
......@@ -1406,6 +1407,7 @@ void SafeBrowsingDatabaseNew::LoadWhitelist(
}
std::vector<SBFullHash> new_whitelist;
new_whitelist.reserve(full_hashes.size());
for (std::vector<SBAddFullHash>::const_iterator it = full_hashes.begin();
it != full_hashes.end(); ++it) {
new_whitelist.push_back(it->full_hash);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment