Browse Source

storage/db: use faster JSON encoder settings when wallet is encrypted

The standard json module has an optimized C encoder, but that doesn't
currently support indentation. So if you request indentation, it falls
back on the slower Python encoder.

Readability doesn't matter for encrypted wallets, so this disables
indentation when the wallet is encrypted.

-----

based on b2399b6a3e

For a large encrypted wallet, compare:
before change:
JsonDB.dump 1.3153 sec
zlib.compress 1.281 sec
ECPubkey.encrypt_message 0.1744 sec

after change:
JsonDB.dump 0.5059 sec
zlib.compress 1.3120 sec
ECPubkey.encrypt_message 0.1630 sec

Co-authored-by: SomberNight <somber.night@protonmail.com>
patch-4
Malcolm Smith 4 years ago
committed by SomberNight
parent
commit
67ae678137
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 12
      electrum/json_db.py
  2. 3
      electrum/storage.py
  3. 4
      electrum/wallet_db.py

12
electrum/json_db.py

@ -193,8 +193,16 @@ class JsonDB(Logger):
return False
@locked
def dump(self):
return json.dumps(self.data, indent=4, sort_keys=True, cls=JsonDBJsonEncoder)
def dump(self, *, human_readable: bool = True) -> str:
"""Serializes the DB as a string.
'human_readable': makes the json indented and sorted, but this is ~2x slower
"""
return json.dumps(
self.data,
indent=4 if human_readable else None,
sort_keys=bool(human_readable),
cls=JsonDBJsonEncoder,
)
def _should_convert_to_stored_dict(self, key) -> bool:
return True

3
electrum/storage.py

@ -78,8 +78,7 @@ class WalletStorage(Logger):
def read(self):
return self.decrypted if self.is_encrypted() else self.raw
@profiler
def write(self, data):
def write(self, data: str) -> None:
s = self.encrypt_before_writing(data)
temp_path = "%s.tmp.%s" % (self.path, os.getpid())
with open(temp_path, "w", encoding='utf-8') as f:

4
electrum/wallet_db.py

@ -1238,13 +1238,15 @@ class WalletDB(JsonDB):
with self.lock:
self._write(storage)
@profiler
def _write(self, storage: 'WalletStorage'):
if threading.currentThread().isDaemon():
self.logger.warning('daemon thread cannot write db')
return
if not self.modified():
return
storage.write(self.dump())
json_str = self.dump(human_readable=not storage.is_encrypted())
storage.write(json_str)
self.set_modified(False)
def is_ready_to_be_used_by_wallet(self):

Loading…
Cancel
Save