Remove a ton of dict lookups for minor perf gains

This commit is contained in:
Josh Klar 2018-09-30 22:50:04 -07:00
parent a089675aa8
commit 784f8885bd
No known key found for this signature in database
GPG Key ID: 220F99BD7DB7A99E
4 changed files with 85 additions and 69 deletions

View File

@ -1,4 +1,5 @@
import logging
from collections import namedtuple
from micropython import const
@ -16,31 +17,46 @@ MACRO_COMPLETE_EVENT = const(8)
logger = logging.getLogger(__name__)
InitFirmware = namedtuple('InitFirmware', (
'type',
'keymap',
'row_pins',
'col_pins',
'diode_orientation',
'unicode_mode',
))
KeyUpDown = namedtuple('KeyUpDown', ('type', 'row', 'col'))
KeycodeUpDown = namedtuple('KeycodeUpDown', ('type', 'keycode'))
NewMatrix = namedtuple('NewMatrix', ('type', 'matrix'))
BareEvent = namedtuple('BareEvent', ('type',))
def init_firmware(keymap, row_pins, col_pins, diode_orientation, unicode_mode):
return {
'type': INIT_FIRMWARE_EVENT,
'keymap': keymap,
'row_pins': row_pins,
'col_pins': col_pins,
'diode_orientation': diode_orientation,
'unicode_mode': unicode_mode,
}
return InitFirmware(
type=INIT_FIRMWARE_EVENT,
keymap=keymap,
row_pins=row_pins,
col_pins=col_pins,
diode_orientation=diode_orientation,
unicode_mode=unicode_mode,
)
def key_up_event(row, col):
return {
'type': KEY_UP_EVENT,
'row': row,
'col': col,
}
return KeyUpDown(
type=KEY_UP_EVENT,
row=row,
col=col,
)
def key_down_event(row, col):
return {
'type': KEY_DOWN_EVENT,
'row': row,
'col': col,
}
return KeyUpDown(
type=KEY_DOWN_EVENT,
row=row,
col=col,
)
def keycode_up_event(keycode):
@ -48,10 +64,10 @@ def keycode_up_event(keycode):
Press a key by Keycode object, bypassing the keymap. Used mostly for
macros.
'''
return {
'type': KEYCODE_UP_EVENT,
'keycode': keycode,
}
return KeycodeUpDown(
type=KEYCODE_UP_EVENT,
keycode=keycode,
)
def keycode_down_event(keycode):
@ -59,29 +75,29 @@ def keycode_down_event(keycode):
Release a key by Keycode object, bypassing the keymap. Used mostly for
macros.
'''
return {
'type': KEYCODE_DOWN_EVENT,
'keycode': keycode,
}
return KeycodeUpDown(
type=KEYCODE_DOWN_EVENT,
keycode=keycode,
)
def new_matrix_event(matrix):
return {
'type': NEW_MATRIX_EVENT,
'matrix': matrix,
}
return NewMatrix(
type=NEW_MATRIX_EVENT,
matrix=matrix,
)
def hid_report_event():
return {
'type': HID_REPORT_EVENT,
}
return BareEvent(
type=HID_REPORT_EVENT,
)
def macro_complete_event():
return {
'type': MACRO_COMPLETE_EVENT,
}
return BareEvent(
type=MACRO_COMPLETE_EVENT,
)
def matrix_changed(new_matrix):

View File

@ -30,7 +30,7 @@ def process_internal_key_event(state, action, changed_key, logger=None):
def grave_escape(state, action, logger):
if action['type'] == KEY_DOWN_EVENT:
if action[0] == KEY_DOWN_EVENT:
for key in state.keys_pressed:
if key in {Keycodes.Modifiers.KC_LSHIFT, Keycodes.Modifiers.KC_RSHIFT}:
# if Shift is held, return KC_GRAVE which will become KC_TILDE on OS level
@ -54,7 +54,7 @@ def grave_escape(state, action, logger):
),
)
elif action['type'] == KEY_UP_EVENT:
elif action[0] == KEY_UP_EVENT:
return state.update(
keys_pressed=frozenset(
key for key in state.keys_pressed
@ -65,7 +65,7 @@ def grave_escape(state, action, logger):
def df(state, action, changed_key, logger):
"""Switches the default layer"""
if action['type'] == KEY_DOWN_EVENT:
if action[0] == KEY_DOWN_EVENT:
state.active_layers[0] = changed_key.layer
return state
@ -73,12 +73,12 @@ def df(state, action, changed_key, logger):
def mo(state, action, changed_key, logger):
"""Momentarily activates layer, switches off when you let go"""
if action['type'] == KEY_UP_EVENT:
if action[0] == KEY_UP_EVENT:
state.active_layers = [
layer for layer in state.active_layers
if layer != changed_key.layer
]
elif action['type'] == KEY_DOWN_EVENT:
elif action[0] == KEY_DOWN_EVENT:
state.active_layers.append(changed_key.layer)
return state
@ -94,7 +94,7 @@ def lt(layer, kc):
def tg(state, action, changed_key, logger):
"""Toggles the layer (enables it if not active, and vise versa)"""
if action['type'] == KEY_DOWN_EVENT:
if action[0] == KEY_DOWN_EVENT:
if changed_key.layer in state.active_layers:
state.active_layers = [
layer for layer in state.active_layers
@ -108,7 +108,7 @@ def tg(state, action, changed_key, logger):
def to(state, action, changed_key, logger):
"""Activates layer and deactivates all other layers"""
if action['type'] == KEY_DOWN_EVENT:
if action[0] == KEY_DOWN_EVENT:
state.active_layers = [changed_key.layer]
return state
@ -119,7 +119,7 @@ def tt(layer):
def unicode_mode(state, action, changed_key, logger):
if action['type'] == KEY_DOWN_EVENT:
if action[0] == KEY_DOWN_EVENT:
state.unicode_mode = changed_key.mode
return state

View File

@ -26,9 +26,9 @@ class ReduxStore:
self.logger.debug('Finished thunk')
return None
self.logger.debug('Dispatching action: Type {} >> {}'.format(action['type'], action))
self.logger.debug('Dispatching action: Type {} >> {}'.format(action[0], action))
self.state = self.reducer(self.state, action, logger=self.logger)
self.logger.debug('Dispatching complete: Type {}'.format(action['type']))
self.logger.debug('Dispatching complete: Type {}'.format(action[0]))
self.logger.debug('New state: {}'.format(self.state))
@ -133,28 +133,28 @@ def kmk_reducer(state=None, action=None, logger=None):
return state
if action['type'] == NEW_MATRIX_EVENT:
if action[0] == NEW_MATRIX_EVENT:
return state.update(
matrix=action['matrix'],
matrix=action[1],
)
if action['type'] == KEYCODE_UP_EVENT:
if action[0] == KEYCODE_UP_EVENT:
return state.update(
keys_pressed=frozenset(
key for key in state.keys_pressed if key != action['keycode']
key for key in state.keys_pressed if key != action[1]
),
)
if action['type'] == KEYCODE_DOWN_EVENT:
if action[0] == KEYCODE_DOWN_EVENT:
return state.update(
keys_pressed=(
state.keys_pressed | {action['keycode']}
state.keys_pressed | {action[1]}
),
)
if action['type'] == KEY_UP_EVENT:
row = action['row']
col = action['col']
if action[0] == KEY_UP_EVENT:
row = action[1]
col = action[2]
changed_key = find_key_in_map(state, row, col)
@ -182,9 +182,9 @@ def kmk_reducer(state=None, action=None, logger=None):
return newstate
if action['type'] == KEY_DOWN_EVENT:
row = action['row']
col = action['col']
if action[0] == KEY_DOWN_EVENT:
row = action[1]
col = action[2]
changed_key = find_key_in_map(state, row, col)
@ -212,16 +212,16 @@ def kmk_reducer(state=None, action=None, logger=None):
return newstate
if action['type'] == INIT_FIRMWARE_EVENT:
if action[0] == INIT_FIRMWARE_EVENT:
return state.update(
keymap=action['keymap'],
row_pins=action['row_pins'],
col_pins=action['col_pins'],
diode_orientation=action['diode_orientation'],
unicode_mode=action['unicode_mode'],
keymap=action.keymap,
row_pins=action.row_pins,
col_pins=action.col_pins,
diode_orientation=action.diode_orientation,
unicode_mode=action.unicode_mode,
matrix=[
[False for c in action['col_pins']]
for r in action['row_pins']
[False for c in action.col_pins]
for r in action.row_pins
],
)
@ -229,10 +229,10 @@ def kmk_reducer(state=None, action=None, logger=None):
# they should be doing things. This could/should arguably be folded back
# into KEY_UP_EVENT and KEY_DOWN_EVENT, but for now it's nice to separate
# this out for debugging's sake.
if action['type'] == HID_REPORT_EVENT:
if action[0] == HID_REPORT_EVENT:
return state
if action['type'] == MACRO_COMPLETE_EVENT:
if action[0] == MACRO_COMPLETE_EVENT:
return state.update(macro_pending=None)
# On unhandled events, log and do not mutate state

View File

@ -43,7 +43,7 @@ class HIDHelper:
self.report_non_mods = memoryview(self._evt)[3:]
def _subscription(self, state, action):
if action['type'] == HID_REPORT_EVENT:
if action[0] == HID_REPORT_EVENT:
self.clear_all()
consumer_key = None