Skip to content

Commit 8b9ff39

Browse files
Merge pull request #109 from ipfs/feat/optimize-prefix
feat: optimize cid.Prefix
2 parents 85cd308 + f7cb4c9 commit 8b9ff39

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

cid.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,29 @@ func (c Cid) Loggable() map[string]interface{} {
518518

519519
// Prefix builds and returns a Prefix out of a Cid.
520520
func (c Cid) Prefix() Prefix {
521-
dec, _ := mh.Decode(c.Hash()) // assuming we got a valid multiaddr, this will not error
521+
if c.Version() == 0 {
522+
return Prefix{
523+
MhType: mh.SHA2_256,
524+
MhLength: 32,
525+
Version: 0,
526+
Codec: DagProtobuf,
527+
}
528+
}
529+
530+
offset := 0
531+
version, n, _ := uvarint(c.str[offset:])
532+
offset += n
533+
codec, n, _ := uvarint(c.str[offset:])
534+
offset += n
535+
mhtype, n, _ := uvarint(c.str[offset:])
536+
offset += n
537+
mhlen, _, _ := uvarint(c.str[offset:])
538+
522539
return Prefix{
523-
MhType: dec.Code,
524-
MhLength: dec.Length,
525-
Version: c.Version(),
526-
Codec: c.Type(),
540+
MhType: mhtype,
541+
MhLength: int(mhlen),
542+
Version: version,
543+
Codec: codec,
527544
}
528545
}
529546

0 commit comments

Comments
 (0)