Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make main pipeline more efficient? #295

Open
ces42 opened this issue May 29, 2023 · 2 comments
Open

How to make main pipeline more efficient? #295

ces42 opened this issue May 29, 2023 · 2 comments

Comments

@ces42
Copy link
Contributor

ces42 commented May 29, 2023

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.

@iberianpig
Copy link
Owner

Thank you for creating the issue and the pull request. I'm really happy about it.
I'm currently unable to dedicate time immediately, so it may take some time to review and reply. 🙏

@ces42
Copy link
Contributor Author

ces42 commented May 30, 2023

it may take some time to review and reply.

No worries!

I did some more changes to the pr and I've been trying to find a good way to reliably measure the speedups. I might make some more changed in the coming days, so no need to rush. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants