ipfs / go-cid

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

Benchmark existing ways to check for `IDENTITY` CIDs #135

Closed masih closed 2 years ago

masih commented 3 years ago

Benchmark existing ways to check for IDENTITY CIDs

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

  1. Cid.Prefix().MhType
  2. Decode of Cid.Hash()

This benchmark illustrates that using Cid.Prefix is more efficient than multihash.Decode. Users wishing to perform such a check should use Cid.Prefix.

Consider that Cid.Prefix is already efficient enough and introducing a dedicated API for performing this check will likely result in small gains.

BenchmarkIdentityCheck
BenchmarkIdentityCheck/Prefix
BenchmarkIdentityCheck/Prefix-8             351656472            3.280 ns/op
BenchmarkIdentityCheck/MultihashDecode
BenchmarkIdentityCheck/MultihashDecode-8             5715622           239.1 ns/op

Relates to #133

masih commented 2 years ago

Will do 🍻

Context captured here

TLDR; there are multiple ways of inferring if a CID is IDENTITY and one of them is more efficient. The one that's more efficient is efficient enough to avoid the need to implement new APIs to do this (see closed PR for more details)