Commit 7c417db
committed
Further optimize the code, assuming that the invariant holds
this invariant is: the report keys are unique (any nonzero entry appears
at most once) and compact (all nonzero entries are first).
A benchmark loop measures how long add/remove report take depending
on the number of other keys pressed:
```
kbd = Keyboard(usb_hid.devices)
t0 = ticks_ms()
for _ in range(1000):
kbd._add_keycode_to_report(K.A)
kbd._remove_keycode_from_report(K.A)
t1 = ticks_ms()
print(f"Press release key time {ticks_diff(t1,t0)}us/call")
kbd = Keyboard(usb_hid.devices)
t0 = ticks_ms()
for _ in range(1000):
kbd._add_keycode_to_report(K.A)
kbd._remove_keycode_from_report(K.A)
t1 = ticks_ms()
print(f"Press release key time {ticks_diff(t1,t0)}us/call")
for k in range(K.ONE, K.SIX):
kbd._add_keycode_to_report(k)
t0 = ticks_ms()
for _ in range(1000):
kbd._add_keycode_to_report(K.A)
kbd._remove_keycode_from_report(K.A)
t1 = ticks_ms()
print(f"Press release key time {ticks_diff(t1,t0)}us/call")
kbd.release_all()
```
Timings were done on a KB2040 with 8.0.0-beta.
| Test | Before | After |
| Empty | 238 | 158 |
| 1 | 246 | 188 |
| 2 | 255 | 219 |
| 3 | 266 | 249 |
| 4 | 274 | 280 |
| 5 | 283 | 301 |
So in the usual case (fewer than 4 non-modifier keys pressed) this
slightly improves performance, but when the report is full (even if
it doesn't overflow) performance decreases, albeit by just 18us. Compared
to the 8ms time to send a report or the default 20ms between polling
a keypad.KeyMatrix this is minor.1 parent b7ad1d5 commit 7c417db
1 file changed
+13
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
137 | | - | |
138 | | - | |
139 | 137 | | |
140 | | - | |
141 | | - | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
142 | 143 | | |
143 | 144 | | |
144 | 145 | | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | 146 | | |
150 | 147 | | |
151 | 148 | | |
| |||
158 | 155 | | |
159 | 156 | | |
160 | 157 | | |
161 | | - | |
| 158 | + | |
162 | 159 | | |
163 | 160 | | |
164 | 161 | | |
165 | | - | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
166 | 165 | | |
167 | | - | |
| 166 | + | |
| 167 | + | |
168 | 168 | | |
169 | | - | |
170 | | - | |
| 169 | + | |
| 170 | + | |
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
| |||
0 commit comments