multiformats/go-multihash

Make Multihash a string instead of a []byte?

Opened this issue · 6 comments

Currently Multihash is defined as:

type Multihash []byte

A multihash may be binary, but it is small and (I hope) immutable. In my view a string is a better fit since it will ensure that it will not change. It will also take less memory to point too (two pointers, instead of three).

This relates to ipfs/go-cid#3.

The reason i vote in favor of using a []byte is because once we move to using a string, anything that needs to copy or write out a multihash needs to make a copy of it first, and those can get expensive.

I suppose that is true. It is really annoying that go does not have a concept of "const" as that can avoid most of these unnecessary copies, for example having to make a copy to pass a string into a function that expects a []byte even though there the function will not modify the data.

And the argument can also be that more things end up needing it as a string that a []byte. @Stebalien what are your thoughts on this manor.

Changing the type of Multihash will be a simple but API breaking change. This perhaps should be done at the same time as ipfs/go-cid#3 (if we decide to do that) to minimize the pain.

I see CIDs used as keys in maps all the time and using a string would help there a lot. Honestly, I'm not sure (we'd have to test it).

@Stebalien I think you are you are confusing CID with mutlihashes, they are now two different things.

Ah. Sorry, too many issues open (I care about this because it's one route to make CID's usable as map keys).