Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1459)

Unified Diff: chrome/browser/safe_browsing/prefix_set.h

Issue 6286072: PrefixSet as an alternate to BloomFilter for safe-browsing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix windows build. Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/safe_browsing/prefix_set.h
diff --git a/chrome/browser/safe_browsing/prefix_set.h b/chrome/browser/safe_browsing/prefix_set.h
new file mode 100644
index 0000000000000000000000000000000000000000..29a799d9d1a0d87b6092f46feeff1fe5fd98e49c
--- /dev/null
+++ b/chrome/browser/safe_browsing/prefix_set.h
@@ -0,0 +1,82 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+// A read-only set implementation for |SBPrefix| items. Prefixes are
+// sorted and stored as 16-bit deltas from the previous prefix. An
+// index structure provides quick random access, and also handles
+// cases where 16 bits cannot encode a delta.
+//
+// This structure is intended for storage of sparse uniform sets of
+// prefixes of a certain size. As of this writing, my safe-browsing
+// database contains:
+// 653132 add prefixes
+// 6446 are duplicates (from different chunks)
+// 24301 w/in 2^8 of the prior prefix
+// 622337 w/in 2^16 of the prior prefix
+// 47 further than 2^16 from the prior prefix
+// For this input, the memory usage is approximately 2 bytes per
+// prefix, a bit over 1.2M. The bloom filter used 25 bits per prefix,
+// a bit over 1.9M on this data.
+//
+// Experimenting with random selections of the above data, storage
+// size drops almost linearly as prefix count drops, until the index
+// overhead starts to become a problem a bit under 200k prefixes. The
+// memory footprint gets worse than storing the raw prefix data around
+// 75k prefixes. Fortunately, the actual memory footprint also falls
+// continuously. If the prefix count increases the memory footprint
+// should increase approximately linearly.
+//
+// TODO(shess): Write serialization code. Something like this should
+// work:
+// 4 byte magic number
+// 4 byte version number
+// 4 byte |index_.size()|
+// 4 byte |deltas_.size()|
+// n * 8 byte |&index_[0]..&index_[n]|
+// m * 2 byte |&deltas_[0]..&deltas_[m]|
+// 16 byte digest
+
+#ifndef CHROME_BROWSER_SAFE_BROWSING_PREFIX_SET_H_
+#define CHROME_BROWSER_SAFE_BROWSING_PREFIX_SET_H_
+#pragma once
+
+#include <vector>
+
+#include "chrome/browser/safe_browsing/safe_browsing_util.h"
+
+namespace safe_browsing {
+
+class PrefixSet {
+ public:
+ explicit PrefixSet(const std::vector<SBPrefix>& prefixes);
+
+ // |true| if |prefix| was in |prefixes| passed to the constructor.
+ bool Exists(SBPrefix prefix) const;
+
+ private:
+ // Maximum delta that can be encoded in a 16-bit unsigned.
+ static const unsigned kMaxDelta = 256 * 256;
+
+ // Maximum number of consecutive deltas to encode before generating
+ // a new index entry. This helps keep the worst-case performance
+ // for |Exists()| under control.
+ static const size_t kMaxRun = 100;
+
+ // Top-level index of prefix to offset in |deltas_|. Each pair
lzheng 2011/02/05 01:03:42 You might want to give an example. It is a little
Scott Hess - ex-Googler 2011/02/07 19:20:23 Adding a comment in the file header.
+ // indicates a base prefix and where the deltas from that prefix
+ // begin in |deltas_|. The deltas for a pair end at the next pair's
+ // index into |deltas_|.
+ std::vector<std::pair<SBPrefix,size_t> > index_;
+
+ // Deltas which are added to the prefix in |index_| to generate
+ // prefixes. Deltas are only valid between consecutive items from
+ // |index_|, or the end of |deltas_| for the last |index_| pair.
+ std::vector<uint16> deltas_;
+
+ DISALLOW_COPY_AND_ASSIGN(PrefixSet);
+};
+
+} // namespace safe_browsing
+
+#endif // CHROME_BROWSER_SAFE_BROWSING_PREFIX_SET_H_
« no previous file with comments | « no previous file | chrome/browser/safe_browsing/prefix_set.cc » ('j') | chrome/browser/safe_browsing/prefix_set.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698