From 65dbe971b1bcefdaa353662f7bde5ea327544113 Mon Sep 17 00:00:00 2001 From: Jon McCaffrey Date: Tue, 18 Nov 2014 12:07:08 -0800 Subject: [PATCH] InputDispatcher: Optimize count() Count items as they are added and removed rather than iterating over the entire list to count them. Increases performance slightly, particularly when tracing is turned on, which causes count to be queried more often, and when the number of items in the queues grow large. This can happen due to applications not responding, for example. Change-Id: I0f11f7edd46089612af910cdfabfeb3ee685d7d9 --- services/inputflinger/InputDispatcher.cpp | 12 ------------ services/inputflinger/InputDispatcher.h | 11 +++++++++-- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/services/inputflinger/InputDispatcher.cpp b/services/inputflinger/InputDispatcher.cpp index 02fb6f04d..1a6ff33ae 100644 --- a/services/inputflinger/InputDispatcher.cpp +++ b/services/inputflinger/InputDispatcher.cpp @@ -3814,18 +3814,6 @@ void InputDispatcher::monitor() { } -// --- InputDispatcher::Queue --- - -template -uint32_t InputDispatcher::Queue::count() const { - uint32_t result = 0; - for (const T* entry = head; entry; entry = entry->next) { - result += 1; - } - return result; -} - - // --- InputDispatcher::InjectionState --- InputDispatcher::InjectionState::InjectionState(int32_t injectorPid, int32_t injectorUid) : diff --git a/services/inputflinger/InputDispatcher.h b/services/inputflinger/InputDispatcher.h index 70b0a34b0..8c78a4460 100644 --- a/services/inputflinger/InputDispatcher.h +++ b/services/inputflinger/InputDispatcher.h @@ -606,8 +606,9 @@ private: struct Queue { T* head; T* tail; + uint32_t entryCount; - inline Queue() : head(NULL), tail(NULL) { + inline Queue() : head(NULL), tail(NULL), entryCount(0) { } inline bool isEmpty() const { @@ -615,6 +616,7 @@ private: } inline void enqueueAtTail(T* entry) { + entryCount++; entry->prev = tail; if (tail) { tail->next = entry; @@ -626,6 +628,7 @@ private: } inline void enqueueAtHead(T* entry) { + entryCount++; entry->next = head; if (head) { head->prev = entry; @@ -637,6 +640,7 @@ private: } inline void dequeue(T* entry) { + entryCount--; if (entry->prev) { entry->prev->next = entry->next; } else { @@ -650,6 +654,7 @@ private: } inline T* dequeueAtHead() { + entryCount--; T* entry = head; head = entry->next; if (head) { @@ -660,7 +665,9 @@ private: return entry; } - uint32_t count() const; + uint32_t count() const { + return entryCount; + } }; /* Specifies which events are to be canceled and why. */