summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Yeabkal Wubshit <yeabkal@google.com> 2023-03-09 17:11:39 +0000
committer Yeabkal Wubshit <yeabkal@google.com> 2023-03-09 16:14:26 -0800
commit540dc07e42ce5188d3e7897256ba6e0d8671b903 (patch)
tree854debeada0b3287f622cd17557bc3ca85ff6f1a
parent3700a8f58fcb4dc80cf6c063ae5768ef36564854 (diff)
Avoid RingBuffer creation on each VelocityTracker#add call
The code used to create a new RingBuffer each time a movement was added to VelocityTracker. This is expensive, and it showed up as a regression in our benchmark tests. With this change, we create a new RingBuffer for a pointerId only if there is not one already. Bug: 267211645 Bug: 271935895 Test: atest libinput_tests, benchmark test regression fixed Change-Id: I47c6dda5741459a1bd84f3f5f46f932c26fda523
-rw-r--r--libs/input/VelocityTracker.cpp4
1 files changed, 2 insertions, 2 deletions
diff --git a/libs/input/VelocityTracker.cpp b/libs/input/VelocityTracker.cpp
index 38730dc966..431a7476f9 100644
--- a/libs/input/VelocityTracker.cpp
+++ b/libs/input/VelocityTracker.cpp
@@ -382,8 +382,8 @@ void AccumulatingVelocityTrackerStrategy::clearPointer(int32_t pointerId) {
void AccumulatingVelocityTrackerStrategy::addMovement(nsecs_t eventTime, int32_t pointerId,
float position) {
- auto [movementIt, _] = mMovements.insert({pointerId, RingBuffer<Movement>(HISTORY_SIZE)});
- RingBuffer<Movement>& movements = movementIt->second;
+ auto [ringBufferIt, _] = mMovements.try_emplace(pointerId, HISTORY_SIZE);
+ RingBuffer<Movement>& movements = ringBufferIt->second;
const size_t size = movements.size();
if (size != 0 && movements[size - 1].eventTime == eventTime) {