From db0e3cd2091bc3ede16106ca794f96de10eeaab9 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Tue, 6 Mar 2018 16:56:51 +0100 Subject: [PATCH 1/2] speed-up wallet.get_full_history: cache coin_price --- lib/wallet.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/wallet.py b/lib/wallet.py index 7e9a884cb..3d5488f2c 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -229,6 +229,8 @@ class Abstract_Wallet(PrintError): self.invoices = InvoiceStore(self.storage) self.contacts = Contacts(self.storage) + self.coin_price_cache = {} + def diagnostic_name(self): return self.basename() @@ -1757,15 +1759,22 @@ class Abstract_Wallet(PrintError): Acquisition price of a coin. This assumes that either all inputs are mine, or no input is mine. """ + cache_key = "{}:{}:{}".format(str(txid), str(ccy), str(txin_value)) + result = self.coin_price_cache.get(cache_key, None) + if result is not None: + return result if self.txi.get(txid, {}) != {}: - return self.average_price(txid, price_func, ccy) * txin_value/Decimal(COIN) + result = self.average_price(txid, price_func, ccy) * txin_value/Decimal(COIN) else: fiat_value = self.get_fiat_value(txid, ccy) if fiat_value is not None: - return fiat_value + result = fiat_value else: p = self.price_at_timestamp(txid, price_func) - return p * txin_value/Decimal(COIN) + result = p * txin_value/Decimal(COIN) + self.coin_price_cache[cache_key] = result + return result + class Simple_Wallet(Abstract_Wallet): # wallet with a single keystore From e0cda39ae5ebe84eb8fb0a188c9965e7dcf8dbfe Mon Sep 17 00:00:00 2001 From: SomberNight Date: Tue, 6 Mar 2018 17:15:14 +0100 Subject: [PATCH 2/2] only cache expensive case --- lib/wallet.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/wallet.py b/lib/wallet.py index 3d5488f2c..892d66a12 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -1765,15 +1765,15 @@ class Abstract_Wallet(PrintError): return result if self.txi.get(txid, {}) != {}: result = self.average_price(txid, price_func, ccy) * txin_value/Decimal(COIN) + self.coin_price_cache[cache_key] = result + return result else: fiat_value = self.get_fiat_value(txid, ccy) if fiat_value is not None: - result = fiat_value + return fiat_value else: p = self.price_at_timestamp(txid, price_func) - result = p * txin_value/Decimal(COIN) - self.coin_price_cache[cache_key] = result - return result + return p * txin_value/Decimal(COIN) class Simple_Wallet(Abstract_Wallet):