Skip to content

How to make main pipeline more efficient? #295

@ces42

Description

@ces42

Is your feature request related to a problem? Please describe.
Fusuma has a somewhat high cpu usage (~9% while performing a swipe on my laptop) for a background process performing a task that isn't computationally intensive.
I was looking at the "main pipeline" to see if there are any inefficiencies in the way events are processed. There seem to be a few.

Running fusuma with the builtin profiles shows that lines 21, 22 and 24 in swipe_detector.rb take a significant amount of time

gesture_buffer = buffers.find { |b| b.type == BUFFER_TYPE }
.select_from_last_begin
.select_by_events { |e| e.record.gesture == GESTURE_RECORD_TYPE }
updating_events = gesture_buffer.updating_events

Closer insepction of the function definitions in gesture_buffer.rb shows that this is due to searching long arrays that contain all events since the beginning of the last gesture in lines 102
index_from_last = @events.reverse.find_index { |e| e.record.status == "begin" }
and 66
@events.select { |e| e.record.status == "update" }

Since this search is performed at every event, the work that is done is quadratic in the number of events in one gesture, and touchpads emit more than 100 events per second.
But there should be no need to re-select the first 99 "update" events in line 24 when we're processing the 100th update event, since we already selected them when processing the 99th event.

It seems like the "merge" step of the pipeline also takes plenty of time, but I don't really understand the code there well enough to identify the reason.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions