| 1 |
What golang Kinds work best to implement CIDs? |
| 2 |
============================================== |
| 3 |
|
| 4 |
There are many possible ways to implement CIDs. This package explores them. |
| 5 |
|
| 6 |
### criteria |
| 7 |
|
| 8 |
There's a couple different criteria to consider: |
| 9 |
|
| 10 |
- We want the best performance when operating on the type (getters, mostly); |
| 11 |
- We want to minimize the number of memory allocations we need; |
| 12 |
- We want types which can be used as map keys, because this is common. |
| 13 |
|
| 14 |
The priority of these criteria is open to argument, but it's probably |
| 15 |
mapkeys > minalloc > anythingelse. |
| 16 |
(Mapkeys and minalloc are also quite entangled, since if we don't pick a |
| 17 |
representation that can work natively as a map key, we'll end up needing |
| 18 |
a `KeyRepr()` method which gives us something that does work as a map key, |
| 19 |
an that will almost certainly involve a malloc itself.) |
| 20 |
|
| 21 |
### options |
| 22 |
|
| 23 |
There are quite a few different ways to go: |
| 24 |
|
| 25 |
- Option A: CIDs as a struct; multihash as bytes. |
| 26 |
- Option B: CIDs as a string. |
| 27 |
- Option C: CIDs as an interface with multiple implementors. |
| 28 |
- Option D: CIDs as a struct; multihash also as a struct or string. |
| 29 |
- Option E: CIDs as a struct; content as strings plus offsets. |
| 30 |
- Option F: CIDs as a struct wrapping only a string. |
| 31 |
|
| 32 |
The current approach on the master branch is Option A. |
| 33 |
|
| 34 |
Option D is distinctive from Option A because multihash as bytes transitively |
| 35 |
causes the CID struct to be non-comparible and thus not suitable for map keys |
| 36 |
as per https://golang.org/ref/spec#KeyType . (It's also a bit more work to |
| 37 |
pursue Option D because it's just a bigger splash radius of change; but also, |
| 38 |
something we might also want to do soon, because we *do* also have these same |
| 39 |
map-key-usability concerns with multihash alone.) |
| 40 |
|
| 41 |
Option E is distinctive from Option D because Option E would always maintain |
| 42 |
the binary format of the cid internally, and so could yield it again without |
| 43 |
malloc, while still potentially having faster access to components than |
| 44 |
Option B since it wouldn't need to re-parse varints to access later fields. |
| 45 |
|
| 46 |
Option F is actually a varation of Option B; it's distinctive from the other |
| 47 |
struct options because it is proposing *literally* `struct{ x string }` as |
| 48 |
the type, with no additional fields for components nor offsets. |
| 49 |
|
| 50 |
Option C is the avoid-choices choice, but note that interfaces are not free; |
| 51 |
since "minimize mallocs" is one of our major goals, we cannot use interfaces |
| 52 |
whimsically. |
| 53 |
|
| 54 |
Note there is no proposal for migrating to `type Cid []bytes`, because that |
| 55 |
is generally considered to be strictly inferior to `type Cid string`. |
| 56 |
|
| 57 |
|
| 58 |
Discoveries |
| 59 |
----------- |
| 60 |
|
| 61 |
### using interfaces as map keys forgoes a lot of safety checks |
| 62 |
|
| 63 |
Using interfaces as map keys pushes a bunch of type checking to runtime. |
| 64 |
E.g., it's totally valid at compile time to push a type which is non-comparable |
| 65 |
into a map key; it will panic at *runtime* instead of failing at compile-time. |
| 66 |
|
| 67 |
There's also no way to define equality checks between implementors of the |
| 68 |
interface: golang will always use its innate concept of comparison for the |
| 69 |
concrete types. This means its effectively *never safe* to use two different |
| 70 |
concrete implementations of an interface in the same map; you may add elements |
| 71 |
which are semantically "equal" in your mind, and end up very confused later |
| 72 |
when both impls of the same "equal" object have been stored. |
| 73 |
|
| 74 |
### sentinel values are possible in any impl, but some are clearer than others |
| 75 |
|
| 76 |
When using `*Cid`, the nil value is a clear sentinel for 'invalid'; |
| 77 |
when using `type Cid string`, the zero value is a clear sentinel; |
| 78 |
when using `type Cid struct` per Option A or D... the only valid check is |
| 79 |
for a nil multihash field, since version=0 and codec=0 are both valid values. |
| 80 |
When using `type Cid struct{string}` per Option F, zero is a clear sentinel. |
| 81 |
|
| 82 |
### usability as a map key is important |
| 83 |
|
| 84 |
We already covered this in the criteria section, but for clarity: |
| 85 |
|
| 86 |
- Option A: ❌ |
| 87 |
- Option B: ✔ |
| 88 |
- Option C: ~ (caveats, and depends on concrete impl) |
| 89 |
- Option D: ✔ |
| 90 |
- Option E: ✔ |
| 91 |
- Option F: ✔ |
| 92 |
|
| 93 |
### living without offsets requires parsing |
| 94 |
|
| 95 |
Since CID (and multihash!) are defined using varints, they require parsing; |
| 96 |
we can't just jump into the string at a known offset in order to yield e.g. |
| 97 |
the multicodec number. |
| 98 |
|
| 99 |
In order to get to the 'meat' of the CID (the multihash content), we first |
| 100 |
must parse: |
| 101 |
|
| 102 |
- the CID version varint; |
| 103 |
- the multicodec varint; |
| 104 |
- the multihash type enum varint; |
| 105 |
- and the multihash length varint. |
| 106 |
|
| 107 |
Since there are many applications where we want to jump straight to the |
| 108 |
multihash content (for example, when doing CAS sharding -- see the |
| 109 |
[disclaimer](https://github.com/multiformats/multihash#disclaimers) about |
| 110 |
bias in leading bytes), this overhead may be interesting. |
| 111 |
|
| 112 |
How much this overhead is significant is hard to say from microbenchmarking; |
| 113 |
it depends largely on usage patterns. If these traversals are a significant |
| 114 |
timesink, it would be an argument for Option D/E. |
| 115 |
If these traversals are *not* a significant timesink, we might be wiser |
| 116 |
to keep to Option B/F, because keeping a struct full of offsets will add several |
| 117 |
words of memory usage per CID, and we keep a *lot* of CIDs. |
| 118 |
|
| 119 |
### interfaces cause boxing which is a significant performance cost |
| 120 |
|
| 121 |
See `BenchmarkCidMap_CidStr` and friends. |
| 122 |
|
| 123 |
Long story short: using interfaces *anywhere* will cause the compiler to |
| 124 |
implicitly generate boxing and unboxing code (e.g. `runtime.convT2E`); |
| 125 |
this is both another function call, and more concerningly, results in |
| 126 |
large numbers of unbatchable memory allocations. |
| 127 |
|
| 128 |
Numbers without context are dangerous, but if you need one: 33%. |
| 129 |
It's a big deal. |
| 130 |
|
| 131 |
This means attempts to "use interfaces, but switch to concrete impls when |
| 132 |
performance is important" are a red herring: it doesn't work that way. |
| 133 |
|
| 134 |
This is not a general inditement against using interfaces -- but |
| 135 |
if a situation is at the scale where it's become important to mind whether |
| 136 |
or not pointers are a performance impact, then that situation also |
| 137 |
is one where you have to think twice before using interfaces. |
| 138 |
|
| 139 |
### struct wrappers can be used in place of typedefs with zero overhead |
| 140 |
|
| 141 |
See `TestSizeOf`. |
| 142 |
|
| 143 |
Using the `unsafe.Sizeof` feature to inspect what the Go runtime thinks, |
| 144 |
we can see that `type Foo string` and `type Foo struct{x string}` consume |
| 145 |
precisely the same amount of memory. |
| 146 |
|
| 147 |
This is interesting because it means we can choose between either |
| 148 |
type definition with no significant overhead anywhere we use it: |
| 149 |
thus, we can choose freely between Option B and Option F based on which |
| 150 |
we feel is more pleasant to work with. |
| 151 |
|
| 152 |
Option F (a struct wrapper) means we can prevent casting into our Cid type. |
| 153 |
Option B (typedef string) can be declared a `const`. |
| 154 |
Are there any other concerns that would separate the two choices? |
| 155 |
|
| 156 |
### one way or another: let's get rid of that star |
| 157 |
|
| 158 |
We should switch completely to handling `Cid` and remove `*Cid` completely. |
| 159 |
Regardless of whether we do this by migrating to interface, or string |
| 160 |
implementations, or simply structs with no pointers... once we get there, |
| 161 |
refactoring to any of the *others* can become a no-op from the perspective |
| 162 |
of any downstream code that uses CIDs. |
| 163 |
|
| 164 |
(This means all access via functions, never references to fields -- even if |
| 165 |
we were to use a struct implementation. *Pretend* there's a interface, |
| 166 |
in other words.) |
| 167 |
|
| 168 |
There are probably `gofix` incantations which can help us with this migration. |