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

Use mypyc to compile modules #25

Merged
merged 13 commits into from
Sep 10, 2023
Merged

Use mypyc to compile modules #25

merged 13 commits into from
Sep 10, 2023

Conversation

Dreamsorcerer
Copy link
Collaborator

@Dreamsorcerer Dreamsorcerer commented May 18, 2021

This compiles the modules which are involved in processing BT/HID events in order to increase performance.

Fixes #24

@@ -0,0 +1,25 @@
[mypy]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic idea here is to completely disallow all dynamic typing in these modules. This ensures mypyc has all the information needed to provide the best possible performance improvements.

@@ -0,0 +1,48 @@
from typing import Callable, Dict, List, Optional
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stubs/ just contains the minimal stubs needed to satisfy mypy and eliminate all dynamic typing. There are some cheats and such in here, but that shouldn't be an issue.

@@ -0,0 +1,23 @@
name: CI
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this won't run on this repository until it's actually been merged initially.
You can see the CI run on my fork though: https://github.com/Dreamsorcerer/bthidhub/commits/master

@ruundii
Copy link
Owner

ruundii commented May 19, 2021

Hi Sam,

I won't be able to look at the pull request for the next few weeks, but I appreciate your contributions. I have invited you as a contributor to this repository, so feel free to merge your changes to the main branch

@Dreamsorcerer
Copy link
Collaborator Author

@ruundii Unfortunately, I've just realised it doesn't really resolve the lag issue after all (though may still be a good idea overall).

When I start the application, there is no noticeable lag. If I disconnect my laptop and reconnect it (from the laptop's BT settings), then often the lag appears after reconnecting. Restarting the app will often go back to being lag-free.
Additionally, whenever I have the BT settings open on my laptop (Ubuntu) there is a very high level of lag. As if the laptop's scanning slows down the RPi's processing time or something (but, this doesn't happen when the mouse is connected directly).

Any ideas what might be causing this lag after reconnection?

I've tried profiling the app, but I don't see any major differences. Maybe there is more time spent in asyncio_glib/glib_selector.py, but I'm not too convinced the problem is there, it might be an issue not exposed by the profiler.

@ruundii
Copy link
Owner

ruundii commented May 19, 2021

from when i was developing and profiling it I remember few things which may cause issues

  1. scanning. as far as I remember when you open BT in ubuntu it does scanning, basically, BT slows down then. It is just a thing in BT protocol, basically, it needs to send/receive many messages over limited number of frequencies etc. so it is bound to slow down. At some point I want to check if it would be better to utilise HoG/GATT for the bthidhub, possibly this will solve some of such issues, but it is a big thing to try out. (e.g. see https://github.com/HeadHodge/Bluez-HID-over-GATT-Keyboard-Emulator-Example/blob/main/gattServer.py)

  2. there is master/slave role in Bluetooth. basically out of two connected devices one is 'controlling the clock'. it is very important for RPi to be in master role for the BT connection with the host PC, otherwise it is bound to lag. bluetooth_devices.py sets this master role. I would check the state of master role when lag is occurring to ensure it is set

  3. Wifi. Unfortunately I found sometimes massive interference between Bluetooth and Wifi.

@Dreamsorcerer
Copy link
Collaborator Author

I think you've got it. It appears that it sets to master on startup, but doesn't do it again when the device reconnects. I'll have a look tomorrow and create a PR to fix it.

@Dreamsorcerer
Copy link
Collaborator Author

Nevermind, that only took 5 mins. :P

@Dreamsorcerer
Copy link
Collaborator Author

I'm happy to merge the other PRs, but I think this one is probably best if tested by someone other than me first.

Also, when you do have some time, it'd be a great help if you could take a look at #21. This is the last issue causing me major problems, and the problem appears to be in the forked bluez, which is not something I know how to debug.

@Dreamsorcerer Dreamsorcerer merged commit 8e44095 into ruundii:master Sep 10, 2023
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

Successfully merging this pull request may close these issues.

Pointer lag
2 participants