diff options
author | huangs <huangs@chromium.org> | 2016-01-08 10:16:37 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-08 18:17:26 +0000 |
commit | 0827d11ca5438ae4b8a093dd84cbed658f878688 (patch) | |
tree | 8183c71d7798f08c84df574f147d334daf96b9a1 /courgette/label_manager.h | |
parent | 522b66a62ec3b8eea486d31d8ebd535c08f55157 (diff) | |
download | chromium_src-0827d11ca5438ae4b8a093dd84cbed658f878688.zip chromium_src-0827d11ca5438ae4b8a093dd84cbed658f878688.tar.gz chromium_src-0827d11ca5438ae4b8a093dd84cbed658f878688.tar.bz2 |
[Courgette] Make LabelManager an interface; move code to LabelManagerImpl.
LabelManager aims to reduce Courgette peak memory, and we still need to
use it in production. To reduce transition risk and pain, the plan is:
1. Make LabelManager an interface, move the implementation to
LabelMangerImpl.
2. More cleanup of Label-related code in production.
3. Add LabelManagerLegacy implementation of LabelManager and move active
Label-related code there. Update callers to call the LabelManager
interfaces. No change in behavior.
4. After extensive testing, switch to using LabelManagerImpl with
relatively little code change.
5. Remove LabelManagerLegacy.
This CL implements step #1.
Review URL: https://codereview.chromium.org/1567133002
Cr-Commit-Position: refs/heads/master@{#368378}
Diffstat (limited to 'courgette/label_manager.h')
-rw-r--r-- | courgette/label_manager.h | 80 |
1 files changed, 57 insertions, 23 deletions
diff --git a/courgette/label_manager.h b/courgette/label_manager.h index 1b88024..3034ebf 100644 --- a/courgette/label_manager.h +++ b/courgette/label_manager.h @@ -10,6 +10,7 @@ #include <vector> +#include "base/gtest_prod_util.h" #include "base/macros.h" #include "courgette/image_utils.h" @@ -17,11 +18,43 @@ namespace courgette { using LabelVector = std::vector<Label>; -// A container to store and manage Label instances. A key consideration is peak -// memory usage reduction. To this end we preallocate Label instances in bulk, -// and carefully control transient memory usage when initializing Labels. +// A container to store and manage Label instances. class LabelManager { public: + virtual ~LabelManager(); + + // Returns the number of Label instances stored. + virtual size_t Size() const = 0; + + // Efficiently searches for a Label that targets |rva|. Returns the pointer to + // the stored Label instance if found, or null otherwise. Non-const to support + // implementations that allocate-on-read. + virtual Label* Find(RVA rva) = 0; + + // Removes Label instances whose |count_| is less than |count_threshold|. + virtual void RemoveUnderusedLabels(int32_t count_threshold) = 0; + + // Resets all indexes to an unassigned state. + virtual void UnassignIndexes() = 0; + + // Assigns indexes to successive integers from 0, ordered by RVA. + virtual void DefaultAssignIndexes() = 0; + + // Assigns indexes to any Label instances that don't have one yet. + virtual void AssignRemainingIndexes() = 0; + + protected: + LabelManager(); + + private: + DISALLOW_COPY_AND_ASSIGN(LabelManager); +}; + +// An implementation of LabelManager dedicated to reducing peak memory usage. +// To this end we preallocate Label instances in bulk, and carefully control +// transient memory usage when initializing Labels. +class LabelManagerImpl : public LabelManager { + public: // An adaptor to sequentially traverse multiple RVAs. This is useful for RVA // translation without extra storage. For example, we might have a stored list // of RVA locations, but we want to traverse the matching RVA targets. @@ -91,36 +124,37 @@ class LabelManager { DISALLOW_COPY_AND_ASSIGN(SimpleIndexAssigner); }; - LabelManager(); - virtual ~LabelManager(); + LabelManagerImpl(); + ~LabelManagerImpl() override; - // Initializes |labels_| using RVAs from |rva_visitor|. Each distinct RVA from + // LabelManager interfaces. + size_t Size() const override; + Label* Find(RVA rva) override; + void RemoveUnderusedLabels(int32_t count_threshold) override; + void UnassignIndexes() override; + void DefaultAssignIndexes() override; + void AssignRemainingIndexes() override; + + // Populates |labels_| using RVAs from |rva_visitor|. Each distinct RVA from // |rva_visitor| yields a Label with |rva_| assigned as the RVA, and |count_| // assigned as the repeat. void Read(RvaVisitor* rva_visitor); - // Removes |labels_| elements whose |count_| is less than |count_threshold|. - void RemoveUnderusedLabels(int32_t count_threshold); - - // Efficiently searches for a Label that targets |rva|. Returns the pointer to - // the stored Label instance if found, or null otherwise. - Label* Find(RVA rva); - - // Resets all indexes to an unassigend state. - void UnassignIndexes(); - - // Assigns indexes to successive integers from 0, ordered by RVA. - void DefaultAssignIndexes(); - - // Assigns indexes to any Label elements that don't have one yet. - void AssignRemainingIndexes(); - protected: // The main list of Label instances, sorted by the |rva_| member. LabelVector labels_; private: - DISALLOW_COPY_AND_ASSIGN(LabelManager); + FRIEND_TEST_ALL_PREFIXES(LabelManagerTest, TrivialAssign); + FRIEND_TEST_ALL_PREFIXES(LabelManagerTest, AssignRemainingIndexes); + + // Accessor to stored Label instances. For testing only. + const LabelVector& Labels() const { return labels_; } + + // Directly assign |labels_|. For testing only. + void SetLabels(const LabelVector& labels); + + DISALLOW_COPY_AND_ASSIGN(LabelManagerImpl); }; } // namespace courgette |