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
4 changed files with 85 additions and 69 deletions

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