summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngus Gratton <gus@projectgus.com>2016-05-22 11:47:57 +1000
committerUwe Hermann <uwe@hermann-uwe.de>2016-06-22 19:59:05 +0200
commit1ec9c5e29850740a96d3e2b137ace9a4a678d786 (patch)
treedb92d81eed641569132280b8e4cd7da67b0a534d
parent10d3c8dc2556b08512290afa17d237156a5f0314 (diff)
downloadlibsigrokdecode-1ec9c5e29850740a96d3e2b137ace9a4a678d786.tar.gz
libsigrokdecode-1ec9c5e29850740a96d3e2b137ace9a4a678d786.zip
spiflash: Perf tweak: Build handler lookup table once per decoder
-rw-r--r--decoders/spiflash/pd.py16
1 files changed, 11 insertions, 5 deletions
diff --git a/decoders/spiflash/pd.py b/decoders/spiflash/pd.py
index 8a0b0e3..03e16e2 100644
--- a/decoders/spiflash/pd.py
+++ b/decoders/spiflash/pd.py
@@ -90,6 +90,14 @@ class Decoder(srd.Decoder):
self.on_end_transaction = None
self.end_current_transaction()
+ # Build dict mapping command keys to handler functions. Each
+ # command in 'cmds' (defined in lists.py) has a matching
+ # handler self.handle_<shortname>.
+ def get_handler(cmd):
+ s = 'handle_%s' % cmds[cmd][0].lower().replace('/', '_')
+ return getattr(self, s)
+ self.cmd_handlers = dict((cmd, get_handler(cmd)) for cmd in cmds.keys())
+
def end_current_transaction(self):
if self.on_end_transaction is not None: # Callback for CS# transition.
self.on_end_transaction()
@@ -373,10 +381,8 @@ class Decoder(srd.Decoder):
self.cmdstate = 1
# Handle commands.
- if self.state in cmds:
- s = 'handle_%s' % cmds[self.state][0].lower().replace('/', '_')
- handle_reg = getattr(self, s)
- handle_reg(mosi, miso)
- else:
+ try:
+ self.cmd_handlers[self.state](mosi, miso)
+ except KeyError:
self.putx([24, ['Unknown command: 0x%02x' % mosi]])
self.state = None