From d821e26f6e583847c694a82eee46ebab4f6e852f Mon Sep 17 00:00:00 2001 From: SomberNight Date: Mon, 19 Dec 2022 08:31:04 +0000 Subject: [PATCH] hw DeviceMgr: speed-up client_for_keystore() for common-case This method is often called when there is already an existing paired client for the keystore, in which case we can avoid scan_devices() - which would needlessly take several seconds. --- electrum/plugin.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/electrum/plugin.py b/electrum/plugin.py index 8839d6d66..d080e7544 100644 --- a/electrum/plugin.py +++ b/electrum/plugin.py @@ -511,10 +511,16 @@ class DeviceMgr(ThreadJob): if handler is None: raise Exception(_("Handler not found for") + ' ' + plugin.name + '\n' + _("A library is probably missing.")) handler.update_status(False) - if devices is None: - devices = self.scan_devices() - client = self.client_by_pairing_code( - plugin=plugin, pairing_code=keystore.pairing_code(), handler=handler, devices=devices) + pcode = keystore.pairing_code() + client = None + # search existing clients first (fast-path) + if not devices: + client = self.client_by_pairing_code(plugin=plugin, pairing_code=pcode, handler=handler, devices=[]) + # search clients again, now allowing a (slow) scan + if client is None: + if devices is None: + devices = self.scan_devices() + client = self.client_by_pairing_code(plugin=plugin, pairing_code=pcode, handler=handler, devices=devices) if client is None and force_pair: try: info = self.select_device(plugin, handler, keystore, devices,