ipfs / go-cid

Content ID v1 implemented in go
MIT License
156 stars 47 forks source link

Implement efficient function to check for `IDENTITY` multihash code #134

Closed masih closed 3 years ago

masih commented 3 years ago

Implement a fail-fast function that checks whether the code of a CID is multihash.IDENTITY or not.

Add benchmarks that compare three ways of checking for multihash.IDENTITY code:

  1. Cid.Prefix().MhType
  2. Decode of Cid.Has()
  3. The new Cid.IsIdentity() API
BenchmarkCidV1IdentityCheckUsingPrefix
BenchmarkCidV1IdentityCheckUsingPrefix-8                355396287            3.334 ns/op    332899.32 MB/s         0 B/op          0 allocs/op
BenchmarkCidV1IdentityCheckUsingMultihashDecode
BenchmarkCidV1IdentityCheckUsingMultihashDecode-8        6404062           194.2 ns/op  5715.67 MB/s        1216 B/op          2 allocs/op
BenchmarkCidV1IdentityCheckUsingIsIdentity
BenchmarkCidV1IdentityCheckUsingIsIdentity-8            442939464            2.621 ns/op    423535.07 MB/s         0 B/op          0 allocs/op

Fixes #133

Stebalien commented 3 years ago

We really shouldn't be special-casing "identity" CIDs at this layer. If we really need this for performance, we should have a function to get the multihash codec.

But I'm not convinced it matters. We're saving less than a nanosecond per op, which'll be nothing compared to all the other work we're going to end up doing.

masih commented 3 years ago

Thank you @marten-seemann and @Stebalien for the reviews 🍻 I agree with @Stebalien.

Closing this as it does not add much value.

masih commented 3 years ago

@marten-seemann @Stebalien I have extracted benchmarks from this PR into a new PR as a way to document the efficiency of existing APIs for IDENTITY check. I think that's worth having 🙂

See: #135