Avoid Temporary Memory Allocation in Impulse Velocity Calculation
During calculating impulse velocity, we used to create arrays to store
the data ponits that are within the HORIZON, and then pass those arrays
(1 for position and one for time) to a separate static function to
calculate velocity. This means that we have to do extra space allocation
(~O(n)), plus process most/all data points twice - once during
collecting the valid data points, and once during using their data to
get velocity.
This implementation avoids new array allocation by using a single linear
processing to both get the valid data points (i.e. the ones within
HORIZON from the latest data point), and to get the velocity. This
approach also has the side-effect benefit of avoiding consideration of
any old data-point on future calls on "computeCurrentVelocity", as we
will effectively mark a data point permanently "invalid" if it fails to
fall within the HORIZON at any point.
Trace-based analysis indicates that this approach improves the
`getVelocity` method by ~20% in time.
Bug: 267211645
Test: unit tests unaffected
Change-Id: Ie7671194476cd17131d79c06b5bc1440a958d384
diff --git a/include/input/VelocityTracker.h b/include/input/VelocityTracker.h
index 6d1de64..f218c51 100644
--- a/include/input/VelocityTracker.h
+++ b/include/input/VelocityTracker.h
@@ -159,6 +159,8 @@
*/
class AccumulatingVelocityTrackerStrategy : public VelocityTrackerStrategy {
public:
+ AccumulatingVelocityTrackerStrategy(nsecs_t horizonNanos, bool maintainHorizonDuringAdd);
+
void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override;
void clearPointer(int32_t pointerId) override;
@@ -173,6 +175,16 @@
// protected const field.
static constexpr uint32_t HISTORY_SIZE = 20;
+ /**
+ * Duration, in nanoseconds, since the latest movement where a movement may be considered for
+ * velocity calculation.
+ */
+ const nsecs_t mHorizonNanos;
+ /**
+ * If true, data points outside of horizon (see `mHorizonNanos`) will be cleared after each
+ * addition of a new movement.
+ */
+ const bool mMaintainHorizonDuringAdd;
std::map<int32_t /*pointerId*/, RingBuffer<Movement>> mMovements;
};