this repo has no description
0
fork

Configure Feed

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

mst: remove alloc in leadingZerosOnHash

goos: darwin
goarch: arm64
pkg: github.com/bluesky-social/indigo/mst
│ before │ after │
│ sec/op │ sec/op vs base │
LeadingZerosOnHash-8 72.28n ± 3% 58.76n ± 2% -18.71% (p=0.000 n=10)

│ before │ after │
│ B/op │ B/op vs base │
LeadingZerosOnHash-8 48.00 ± 0% 0.00 ± 0% -100.00% (p=0.000 n=10)

│ before │ after │
│ allocs/op │ allocs/op vs base │
LeadingZerosOnHash-8 1.000 ± 0% 0.000 ± 0% -100.00% (p=0.000 n=10)

authored by

Brad Fitzpatrick and committed by
bnewbold
5e98aed6 acb9236f

+44 -9
+1 -1
mst/mst.go
··· 215 215 if len(nd.Entries) > 0 { 216 216 // NOTE(bnewbold): can compute the layer on the first KeySuffix, because for the first entry that field is a complete key 217 217 firstLeaf := nd.Entries[0] 218 - layer = leadingZerosOnHash(string(firstLeaf.KeySuffix)) 218 + layer = leadingZerosOnHashBytes(firstLeaf.KeySuffix) 219 219 } 220 220 221 221 entries, err := deserializeNodeData(ctx, cst, nd, layer)
+24
mst/mst_test.go
··· 511 511 } 512 512 } 513 513 } 514 + 515 + func TestLeadingZerosOnHashAllocs(t *testing.T) { 516 + var sink int 517 + const in = "some.key.prefix/key.bar123456789012334556" 518 + var inb = []byte(in) 519 + if n := int(testing.AllocsPerRun(1000, func() { 520 + sink = leadingZerosOnHash(in) 521 + })); n != 0 { 522 + t.Errorf("allocs (string) = %d; want 0", n) 523 + } 524 + if n := int(testing.AllocsPerRun(1000, func() { 525 + sink = leadingZerosOnHashBytes(inb) 526 + })); n != 0 { 527 + t.Errorf("allocs (bytes) = %d; want 0", n) 528 + } 529 + _ = sink 530 + } 531 + 532 + func BenchmarkLeadingZerosOnHash(b *testing.B) { 533 + b.ReportAllocs() 534 + for i := 0; i < b.N; i++ { 535 + _ = leadingZerosOnHash("some.key.prefix/key.bar123456789012334556") 536 + } 537 + }
+19 -8
mst/mst_util.go
··· 8 8 "fmt" 9 9 "regexp" 10 10 "strings" 11 + "unsafe" 11 12 12 13 "github.com/ipfs/go-cid" 13 14 cbor "github.com/ipfs/go-ipld-cbor" ··· 18 19 // chunks of 2-bits. Eg, a leading 0x00 byte is 4 "zeros". 19 20 // Typescript: leadingZerosOnHash(key, fanout) -> number 20 21 func leadingZerosOnHash(key string) int { 21 - k := []byte(key) 22 - hv := sha256.Sum256(k) 22 + var b []byte 23 + if len(key) > 0 { 24 + b = unsafe.Slice(unsafe.StringData(key), len(key)) 25 + } 26 + return leadingZerosOnHashBytes(b) 27 + } 23 28 24 - total := 0 25 - for i := 0; i < len(hv); i++ { 26 - if hv[i] == 0x00 { 29 + func leadingZerosOnHashBytes(key []byte) (total int) { 30 + hv := sha256.Sum256(key) 31 + for _, b := range hv { 32 + if b&0xC0 != 0 { 33 + // Common case. No leading pair of zero bits. 34 + break 35 + } 36 + if b == 0x00 { 27 37 total += 4 28 38 continue 29 - } else if hv[i]&0xFC == 0x00 { 39 + } 40 + if b&0xFC == 0x00 { 30 41 total += 3 31 - } else if hv[i]&0xF0 == 0x00 { 42 + } else if b&0xF0 == 0x00 { 32 43 total += 2 33 - } else if hv[i]&0xC0 == 0x00 { 44 + } else { 34 45 total += 1 35 46 } 36 47 break