Commit 86262b2a authored by bratell@opera.com's avatar bratell@opera.com

Make ObserverList non-inline to save 90KB with gcc

With gcc, ObserverList expands to 6-700 bytes every time it is 
iterated. By not trying to force OberservListBase to be inline
that shrinks some. clang already ignores the hints to inline 
the code and actually doesn't change a single bit in the generated
program with this change.

raw data:
Total change: -90875 bytes
==========================
  262 added, totalling +39188 bytes across 2 sources
  49 removed, totalling -27573 bytes across 16 sources
  23 grown, for a net change of +3600 bytes (34968 bytes before, 38568 bytes after) across 8 sources
  228 shrunk, for a net change of -106090 bytes (239819 bytes before, 133729 bytes after) across 53 sources

The additions is about 2-300 different flavours of ObserverListBase, 100-300 bytes each.

The savings are 200-600 bytes each every time an observer list is used. For example:

-870: content::RenderFrameImpl::didFailLoad(blink::WebLocalFrame*, blink::WebURLError const&) type=t, (was 1635 bytes, 
now 765 bytes)
-895: content::RenderFrameImpl::didFinishLoad(blink::WebLocalFrame*) type=t, (was 1646 bytes, now 751 bytes)
-583: content::WebContentsImpl::DidFailProvisionalLoadWithError(content::RenderFrameHostImpl*, FrameHostMsg_DidFailProvisionalLoadWithError_Params const&) type=t, (was 768 bytes, now 185 bytes)
-611: content::WebContentsImpl::DidStartProvisionalLoad(content::RenderFrameHostImpl*, GURL const&, bool, bool) type=t, (was 895 bytes, now 284 bytes)

BUG=394311

Review URL: https://codereview.chromium.org/396473004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283762 0039d316-1c4b-4281-b951-d872f2087c98
parent 34f8cf51
......@@ -79,21 +79,69 @@ class ObserverListBase
// also the FOR_EACH_OBSERVER macro defined below.
class Iterator {
public:
Iterator(ObserverListBase<ObserverType>& list)
Iterator(ObserverListBase<ObserverType>& list);
~Iterator();
ObserverType* GetNext();
private:
base::WeakPtr<ObserverListBase<ObserverType> > list_;
size_t index_;
size_t max_index_;
};
ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {}
explicit ObserverListBase(NotificationType type)
: notify_depth_(0), type_(type) {}
// Add an observer to the list. An observer should not be added to
// the same list more than once.
void AddObserver(ObserverType* obs);
// Remove an observer from the list if it is in the list.
void RemoveObserver(ObserverType* obs);
bool HasObserver(ObserverType* observer) const;
void Clear();
protected:
size_t size() const { return observers_.size(); }
void Compact();
private:
friend class ObserverListThreadSafe<ObserverType>;
typedef std::vector<ObserverType*> ListType;
ListType observers_;
int notify_depth_;
NotificationType type_;
friend class ObserverListBase::Iterator;
DISALLOW_COPY_AND_ASSIGN(ObserverListBase);
};
template <class ObserverType>
ObserverListBase<ObserverType>::Iterator::Iterator(
ObserverListBase<ObserverType>& list)
: list_(list.AsWeakPtr()),
index_(0),
max_index_(list.type_ == NOTIFY_ALL ?
std::numeric_limits<size_t>::max() :
list.observers_.size()) {
++list_->notify_depth_;
}
}
~Iterator() {
template <class ObserverType>
ObserverListBase<ObserverType>::Iterator::~Iterator() {
if (list_.get() && --list_->notify_depth_ == 0)
list_->Compact();
}
}
ObserverType* GetNext() {
template <class ObserverType>
ObserverType* ObserverListBase<ObserverType>::Iterator::GetNext() {
if (!list_.get())
return NULL;
ListType& observers = list_->observers_;
......@@ -102,31 +150,20 @@ class ObserverListBase
while (index_ < max_index && !observers[index_])
++index_;
return index_ < max_index ? observers[index_++] : NULL;
}
private:
base::WeakPtr<ObserverListBase<ObserverType> > list_;
size_t index_;
size_t max_index_;
};
ObserverListBase() : notify_depth_(0), type_(NOTIFY_ALL) {}
explicit ObserverListBase(NotificationType type)
: notify_depth_(0), type_(type) {}
}
// Add an observer to the list. An observer should not be added to
// the same list more than once.
void AddObserver(ObserverType* obs) {
template <class ObserverType>
void ObserverListBase<ObserverType>::AddObserver(ObserverType* obs) {
if (std::find(observers_.begin(), observers_.end(), obs)
!= observers_.end()) {
NOTREACHED() << "Observers can only be added once!";
return;
}
observers_.push_back(obs);
}
}
// Remove an observer from the list if it is in the list.
void RemoveObserver(ObserverType* obs) {
template <class ObserverType>
void ObserverListBase<ObserverType>::RemoveObserver(ObserverType* obs) {
typename ListType::iterator it =
std::find(observers_.begin(), observers_.end(), obs);
if (it != observers_.end()) {
......@@ -136,17 +173,19 @@ class ObserverListBase
observers_.erase(it);
}
}
}
}
bool HasObserver(ObserverType* observer) const {
template <class ObserverType>
bool ObserverListBase<ObserverType>::HasObserver(ObserverType* observer) const {
for (size_t i = 0; i < observers_.size(); ++i) {
if (observers_[i] == observer)
return true;
}
return false;
}
}
void Clear() {
template <class ObserverType>
void ObserverListBase<ObserverType>::Clear() {
if (notify_depth_) {
for (typename ListType::iterator it = observers_.begin();
it != observers_.end(); ++it) {
......@@ -155,30 +194,14 @@ class ObserverListBase
} else {
observers_.clear();
}
}
}
protected:
size_t size() const { return observers_.size(); }
void Compact() {
template <class ObserverType>
void ObserverListBase<ObserverType>::Compact() {
observers_.erase(
std::remove(observers_.begin(), observers_.end(),
static_cast<ObserverType*>(NULL)), observers_.end());
}
private:
friend class ObserverListThreadSafe<ObserverType>;
typedef std::vector<ObserverType*> ListType;
ListType observers_;
int notify_depth_;
NotificationType type_;
friend class ObserverListBase::Iterator;
DISALLOW_COPY_AND_ASSIGN(ObserverListBase);
};
}
template <class ObserverType, bool check_empty = false>
class ObserverList : public ObserverListBase<ObserverType> {
......
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