commit d7597d96d0c336838adb32e3e175d3ea6f9763e8
parent 2b693d3498e79463aa48043c27906f7ccc6d03e2
Author: SomberNight <somber.night@protonmail.com>
Date: Sun, 28 Feb 2021 16:26:15 +0100
lnaddr: 15x speedup for lndecode
benchmarked with:
```
import time
import electrum
from electrum.lnaddr import lndecode
electrum.constants.set_testnet()
inv = "lntb4m1p00zfpppp597ely08ffhk8n3emeswukt0y3qfvt3sj3ufkhnaatlrswj2xvwuqsp5vu3ezu44ka8arvgda44yalysp3k3edlvg56cjkk5lvu4e4anmdssdq2v9ekgctnvscqzynxqyz5vq9qypqsqrzjqv8shunq4nda8mw2mpxhtz8v03wlgug7sln2yvqklxym35ayz3erqxct8vqqqcqqqqqqqqlgqqqqqqgq9qrzjqdxvvgt048y4htef7r63r4ha9kctz3d6l3za0053ahe597wgrkc4gxct8cqqqfsqqqqqqqlgqqqqqqgq9qrzjqwyx8nu2hygyvgc02cwdtvuxe0lcxz06qt3lpsldzcdr46my5epmjxct8vqqqdcqqqqqqqlgqqqqqqgq9qrzjqf56jn5txtqqtepnd0ahg0qg5m5mavfajsx403rem9wgu6rue0de7xct8vqqqtgqqqqqqqlgqqqq86qq9qrzjq027z73uyyl7fy8pkrpcn7x0el82pz3fw974p2052de4uz4j5lqqxx49tuqqqwgqqqqqqqqqqqqqqqqqpurzjqfj34n62wztqjxl59w4drxekg04rrrtf08mdestwhtky84ds7ja0yxct8sqqq3qqqqqqqqlgqqqqqqgq9qrzjqd872t5c5r5a8ssmwelpkdccsyn9mrr40rpp7khad4jr3kssxj9nvx49vgqqqnqqqqqqqqlgqqqq05qqgcxwu0ervh6atmqmqv7pmenhmc207gncyj0mcxedpwm8f56y2yl3qpq6mzjak37ddmeayd9unektmffv5rq8dvlpgq00rmmdalda73yhgqep0zuz"
def f():
for _ in range(100):
addr = lndecode(inv)
t0 = time.monotonic()
f()
t1 = time.monotonic()
print(f"{t1-t0:.4f}")
```
Diffstat:
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/electrum/lnaddr.py b/electrum/lnaddr.py
@@ -63,12 +63,12 @@ def unshorten_amount(amount) -> Decimal:
else:
return Decimal(amount)
+_INT_TO_BINSTR = {a: '0' * (5-len(bin(a)[2:])) + bin(a)[2:] for a in range(32)}
+
# Bech32 spits out array of 5-bit values. Shim here.
def u5_to_bitarray(arr):
- ret = bitstring.BitArray()
- for a in arr:
- ret += bitstring.pack("uint:5", a)
- return ret
+ b = ''.join(_INT_TO_BINSTR[a] for a in arr)
+ return bitstring.BitArray(bin=b)
def bitarray_to_u5(barr):
assert barr.len % 5 == 0