Commit f7cb4c91 authored by Steven Allen's avatar Steven Allen

feat: optimize cid.Prefix

We call this frequently and having to allocate here is really unfortunate.
parent 85cd3087
...@@ -518,12 +518,29 @@ func (c Cid) Loggable() map[string]interface{} { ...@@ -518,12 +518,29 @@ func (c Cid) Loggable() map[string]interface{} {
// Prefix builds and returns a Prefix out of a Cid. // Prefix builds and returns a Prefix out of a Cid.
func (c Cid) Prefix() Prefix { func (c Cid) Prefix() Prefix {
dec, _ := mh.Decode(c.Hash()) // assuming we got a valid multiaddr, this will not error if c.Version() == 0 {
return Prefix{
MhType: mh.SHA2_256,
MhLength: 32,
Version: 0,
Codec: DagProtobuf,
}
}
offset := 0
version, n, _ := uvarint(c.str[offset:])
offset += n
codec, n, _ := uvarint(c.str[offset:])
offset += n
mhtype, n, _ := uvarint(c.str[offset:])
offset += n
mhlen, _, _ := uvarint(c.str[offset:])
return Prefix{ return Prefix{
MhType: dec.Code, MhType: mhtype,
MhLength: dec.Length, MhLength: int(mhlen),
Version: c.Version(), Version: version,
Codec: c.Type(), Codec: codec,
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment