Commit c37c6180 authored by sclittle's avatar sclittle Committed by Commit bot

Replace std::deque usage in drp_event_store with a circular buffer.

Since std::deque is memory inefficient for small numbers of elements,
especially on Android, (see http://crbug.com/674287), this CL replaces
the std::deque in DataReductionProxyEventStore with a simple circular
buffer.

BUG=679603

Review-Url: https://codereview.chromium.org/2619373003
Cr-Commit-Position: refs/heads/master@{#443073}
parent 90d7824a
...@@ -5,19 +5,16 @@ ...@@ -5,19 +5,16 @@
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h"
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#include <utility> #include <utility>
#include <vector>
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/values.h" #include "base/values.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h"
namespace { namespace {
...@@ -60,7 +57,7 @@ std::string JoinListValueStrings(base::ListValue* list_value) { ...@@ -60,7 +57,7 @@ std::string JoinListValueStrings(base::ListValue* list_value) {
if (!value->GetAsString(&value_string)) if (!value->GetAsString(&value_string))
return std::string(); return std::string();
values.push_back(value_string); values.push_back(std::move(value_string));
} }
return base::JoinString(values, ";"); return base::JoinString(values, ";");
...@@ -93,10 +90,10 @@ void DataReductionProxyEventStore::AddConstants( ...@@ -93,10 +90,10 @@ void DataReductionProxyEventStore::AddConstants(
} }
DataReductionProxyEventStore::DataReductionProxyEventStore() DataReductionProxyEventStore::DataReductionProxyEventStore()
: enabled_(false), : oldest_event_index_(0),
enabled_(false),
secure_proxy_check_state_(CHECK_UNKNOWN), secure_proxy_check_state_(CHECK_UNKNOWN),
expiration_ticks_(0) { expiration_ticks_(0) {}
}
DataReductionProxyEventStore::~DataReductionProxyEventStore() { DataReductionProxyEventStore::~DataReductionProxyEventStore() {
} }
...@@ -137,17 +134,29 @@ DataReductionProxyEventStore::GetSummaryValue() const { ...@@ -137,17 +134,29 @@ DataReductionProxyEventStore::GetSummaryValue() const {
} }
auto events_list = base::MakeUnique<base::ListValue>(); auto events_list = base::MakeUnique<base::ListValue>();
for (const auto& event : stored_events_)
events_list->Append(event->CreateDeepCopy()); DCHECK(oldest_event_index_ == 0 ||
stored_events_.size() == kMaxEventsToStore);
for (size_t i = oldest_event_index_; i < stored_events_.size(); ++i)
events_list->Append(stored_events_[i]->CreateDeepCopy());
for (size_t i = 0; i < oldest_event_index_; ++i)
events_list->Append(stored_events_[i]->CreateDeepCopy());
data_reduction_proxy_values->Set("events", std::move(events_list)); data_reduction_proxy_values->Set("events", std::move(events_list));
return data_reduction_proxy_values; return data_reduction_proxy_values;
} }
void DataReductionProxyEventStore::AddEvent( void DataReductionProxyEventStore::AddEvent(
std::unique_ptr<base::Value> event) { std::unique_ptr<base::Value> event) {
if (stored_events_.size() == kMaxEventsToStore) if (stored_events_.size() < kMaxEventsToStore) {
stored_events_.pop_front(); stored_events_.push_back(std::move(event));
stored_events_.push_back(std::move(event)); return;
}
DCHECK_EQ(kMaxEventsToStore, stored_events_.size());
stored_events_[oldest_event_index_++] = std::move(event);
if (oldest_event_index_ >= stored_events_.size())
oldest_event_index_ = 0;
} }
void DataReductionProxyEventStore::AddEnabledEvent( void DataReductionProxyEventStore::AddEnabledEvent(
......
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
#include <stdint.h> #include <stdint.h>
#include <deque>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector>
#include "base/macros.h" #include "base/macros.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
...@@ -30,15 +30,15 @@ class DataReductionProxyEventStore ...@@ -30,15 +30,15 @@ class DataReductionProxyEventStore
// constants dictionary. // constants dictionary.
static void AddConstants(base::DictionaryValue* constants_dict); static void AddConstants(base::DictionaryValue* constants_dict);
// Constructs a DataReductionProxyEventStore object // Constructs a DataReductionProxyEventStore object.
DataReductionProxyEventStore(); DataReductionProxyEventStore();
virtual ~DataReductionProxyEventStore(); virtual ~DataReductionProxyEventStore();
// Creates a Value summary of Data Reduction Proxy related information: // Creates a Value summary of Data Reduction Proxy related information:
// - Whether the proxy is enabled // - Whether the proxy is enabled,
// - The proxy configuration // - The proxy configuration,
// - The state of the last secure proxy check response // - The state of the last secure proxy check response,
// - A stream of the last Data Reduction Proxy related events. // - A stream of the last Data Reduction Proxy related events.
std::unique_ptr<base::DictionaryValue> GetSummaryValue() const; std::unique_ptr<base::DictionaryValue> GetSummaryValue() const;
...@@ -46,19 +46,18 @@ class DataReductionProxyEventStore ...@@ -46,19 +46,18 @@ class DataReductionProxyEventStore
void AddEvent(std::unique_ptr<base::Value> event) override; void AddEvent(std::unique_ptr<base::Value> event) override;
// Override of DataReductionProxyEventStorageDelegate. // Override of DataReductionProxyEventStorageDelegate.
// Put |entry| on the deque of stored events and set |current_configuration_|. // Adds |entry| to the event store and sets |current_configuration_|.
void AddEnabledEvent(std::unique_ptr<base::Value> entry, void AddEnabledEvent(std::unique_ptr<base::Value> entry,
bool enabled) override; bool enabled) override;
// Override of DataReductionProxyEventStorageDelegate. // Override of DataReductionProxyEventStorageDelegate.
// Put |entry| on a deque of events to store and set // Adds |entry| to the event store and sets |secure_proxy_check_state_|.
// |secure_proxy_check_state_|
void AddEventAndSecureProxyCheckState(std::unique_ptr<base::Value> entry, void AddEventAndSecureProxyCheckState(std::unique_ptr<base::Value> entry,
SecureProxyCheckState state) override; SecureProxyCheckState state) override;
// Override of DataReductionProxyEventStorageDelegate. // Override of DataReductionProxyEventStorageDelegate.
// Put |entry| on a deque of events to store and set |last_bypass_event_| and // Adds |entry| to the event store and sets |last_bypass_event_| and
// |expiration_ticks_| // |expiration_ticks_|.
void AddAndSetLastBypassEvent(std::unique_ptr<base::Value> entry, void AddAndSetLastBypassEvent(std::unique_ptr<base::Value> entry,
int64_t expiration_ticks) override; int64_t expiration_ticks) override;
...@@ -71,9 +70,12 @@ class DataReductionProxyEventStore ...@@ -71,9 +70,12 @@ class DataReductionProxyEventStore
private: private:
friend class DataReductionProxyEventStoreTest; friend class DataReductionProxyEventStoreTest;
// A deque of data reduction proxy related events. It is used as a circular // A vector of data reduction proxy related events. It is used as a circular
// buffer to prevent unbounded memory utilization. // buffer to prevent unbounded memory utilization.
std::deque<std::unique_ptr<base::Value>> stored_events_; std::vector<std::unique_ptr<base::Value>> stored_events_;
// The index of the oldest event in |stored_events_|.
size_t oldest_event_index_;
// Whether the data reduction proxy is enabled or not. // Whether the data reduction proxy is enabled or not.
bool enabled_; bool enabled_;
// The current data reduction proxy configuration. // The current data reduction proxy configuration.
......
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