this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

replace hash_to_cid with cbrrr.CID constructors

+17 -25
+4 -4
mst_bench.py
··· 1 1 import random 2 2 from atmst.all import MemoryBlockStore, NodeStore, NodeWrangler 3 - from atmst.util import hash_to_cid 3 + from cbrrr import CID 4 4 import time 5 5 6 - dummy_cid = hash_to_cid(b"hello") 6 + dummy_cid = CID.cidv1_dag_cbor_sha256_32_from(b"hello") 7 7 8 8 def insert_random(n: int): 9 9 bs = MemoryBlockStore() ··· 27 27 28 28 if __name__ == "__main__": 29 29 start = time.time() 30 - insert_random(1000) 30 + insert_random(10000) 31 31 print("insert random", time.time() - start) 32 32 33 33 start = time.time() 34 - insert_sequential(1000) 34 + insert_sequential(10000) 35 35 print("insert sequential", time.time() - start)
+5 -5
mst_test.py
··· 1 1 import random 2 2 from atmst.all import MemoryBlockStore, NodeStore, NodeWrangler, mst_diff, very_slow_mst_diff 3 - from atmst.util import hash_to_cid 3 + from cbrrr import CID 4 4 import time 5 5 6 6 PERF_BENCH = False ··· 14 14 for _ in range(10240 if PERF_BENCH else random.randrange(0, 32)): 15 15 k = random.randbytes(8).hex() 16 16 keys.append(k) 17 - root = nw.put_record(root, k, hash_to_cid(random.randbytes(8))) 17 + root = nw.put_record(root, k, CID.cidv1_dag_cbor_sha256_32_from(random.randbytes(8))) 18 18 root_a = root 19 19 for _ in range(8 if PERF_BENCH else random.randrange(0, 8)): 20 20 # some random additions 21 - root = nw.put_record(root, random.randbytes(8).hex(), hash_to_cid(random.randbytes(8))) 21 + root = nw.put_record(root, random.randbytes(8).hex(), CID.cidv1_dag_cbor_sha256_32_from(random.randbytes(8))) 22 22 if keys: 23 23 # some random modifications 24 24 for _ in range(4 if PERF_BENCH else random.randrange(0, 4)): 25 25 for k in random.choice(keys): 26 - root = nw.put_record(root, k, hash_to_cid(random.randbytes(8))) 26 + root = nw.put_record(root, k, CID.cidv1_dag_cbor_sha256_32_from(random.randbytes(8))) 27 27 # some random deletions 28 28 for _ in range(4 if PERF_BENCH else random.randrange(0, 4)): 29 29 for k in random.choice(keys): ··· 43 43 44 44 if __name__ == "__main__": 45 45 duration = 0 46 - for _ in range(1 if PERF_BENCH else 200): 46 + for _ in range(1 if PERF_BENCH else 2000): 47 47 duration += random_test() 48 48 print("time spent diffing (ms):", duration*1000)
-2
src/atmst/mst/diff.py
··· 1 - import operator 2 1 from typing import Tuple, Set, Dict, Iterable, Optional 3 2 from enum import Enum 4 3 from dataclasses import dataclass 5 - from functools import reduce 6 4 import json 7 5 8 6 from cbrrr import CID
+1 -2
src/atmst/mst/node.py
··· 8 8 9 9 from cbrrr import encode_dag_cbor, parse_dag_cbor, CID 10 10 11 - from ..util import hash_to_cid 12 11 13 12 @dataclass(frozen=True) # frozen == immutable == win 14 13 class MSTNode: ··· 56 55 # since we're immutable, this can be cached 57 56 @cached_property 58 57 def cid(self) -> CID: 59 - return hash_to_cid(self.serialised) 58 + return CID.cidv1_dag_cbor_sha256_32_from(self.serialised) 60 59 61 60 # likewise 62 61 @cached_property
+5 -10
src/atmst/util.py
··· 1 - import hashlib 2 - from cbrrr import CID 3 - 4 - def indent(msg: str) -> str: 5 - ISTR = " " 6 - return ISTR + msg.replace("\n", "\n"+ISTR) 7 - 8 - def hash_to_cid(data: bytes, codec="dag-cbor") -> CID: 9 - digest = b"\x12\x20" + hashlib.sha256(data).digest() 10 - return CID((b"\x00\x01q" if codec == "dag-cbor" else b'\x00\x01U') + digest) # XXX: this is a hack! 1 + def indent(msg: str, istr=" ") -> str: 2 + """ 3 + Indent a multi-line string 4 + """ 5 + return istr + msg.replace("\n", "\n"+istr)
+2 -2
tests/test_mst_diff.py
··· 2 2 3 3 from atmst.all import MemoryBlockStore, NodeStore, NodeWrangler, mst_diff, very_slow_mst_diff 4 4 from atmst.mst.node import MSTNode 5 - from atmst.util import hash_to_cid 5 + from cbrrr import CID 6 6 7 7 class MSTDiffTestCase(unittest.TestCase): 8 8 def setUp(self): 9 9 keys = [] 10 - dummy_value = hash_to_cid(b"value") 10 + dummy_value = CID.cidv1_dag_cbor_sha256_32_from(b"value") 11 11 i = 0 12 12 for height in [0, 1, 0, 2, 0, 1, 0]: # if all these keys are added to a MST, it'll form a perfect binary tree. 13 13 while True: