Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark existing ways to check for IDENTITY CIDs #135

Merged
merged 1 commit into from
Nov 8, 2021

Conversation

masih
Copy link
Member

@masih masih commented Sep 27, 2021

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

Copy link
Contributor

@mvdan mvdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop the CidV1 part of the benchmark name; Cid is redundant since this module is go-cid, and v1 is redundant as we don't really care if the CID is v1 or not - it just so happens that all identity CIDs are currently v1. If they also supported a future CIDv2, then the benchmark would simply do both in each iteration.

You could also make the two methods sub-benchmarks, to simplify sharing code and make the output nicer. For example:

func BenchmarkIdentityCheck(b *testing.B) {
    // cid setup, ReportAllocs, etc
    b.Run("Prefix", func(b *testing.B) {
        b.RunParallel(func(pb *testing.PB) {
            // check via Prefix
        })
    })
    b.Run("MultihashDecode", func(b *testing.B) {
        b.RunParallel(func(pb *testing.PB) {
            // check via MultihashDecode
        })
    })
}

@masih masih force-pushed the masih/bench-identity-check branch from eb5e77d to ada55fd Compare September 27, 2021 09:54
Copy link
Contributor

@mvdan mvdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM! Probably wait for a review from a steward though.

benchmark_test.go Outdated Show resolved Hide resolved
benchmark_test.go Outdated Show resolved Hide resolved
benchmark_test.go Show resolved Hide resolved
@masih masih force-pushed the masih/bench-identity-check branch from ada55fd to c2a6e2b Compare September 27, 2021 10:07
Copy link
Member

@warpfork warpfork left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no reason to complain about more benchmarks :)

If you still have the context in your head: I wouldn't mind a short paragraph of comment attached to the function about what you were looking for / why it mattered / and what the rough findings were at the time you committed. (It'll get stale, sure -- but that doesn't mean it won't still be useful context clues to some future archaeologist^W reviewer.)

(... I guess that would be roughly the same content you've already included in the PR message. Still worth repeating in the diff, IMO. :) I find it a lot easier to rediscover links in a comment in the relevant code than... links that are in PRs that are maybe (depending on which merge button you click in github) mentioned by number in the commit log that you might find after you go blame-splunking.)

@masih
Copy link
Member Author

masih commented Nov 8, 2021

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)

@masih masih force-pushed the masih/bench-identity-check branch 2 times, most recently from 2a5f393 to 44b768d Compare November 8, 2021 13:15
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.

Relates to #133
@masih masih force-pushed the masih/bench-identity-check branch from 44b768d to 70c228c Compare November 8, 2021 13:16
@masih masih merged commit 28f4a5e into master Nov 8, 2021
@masih masih deleted the masih/bench-identity-check branch November 8, 2021 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants