store Multiaddrs as strings and don't use interfaces#62
store Multiaddrs as strings and don't use interfaces#62
Conversation
|
The tests are failing because this change breaks GX. We'd have to fix that concurrently with this PR. @whyrusleeping thoughts? This will be a real pain ( An alternative is to define multiaddrs to be |
This allows us to send multiaddrs on the wire without copying (through the `Bytes()` method). It also avoids one allocation/indirection per multiaddr. This is an alternative (or stop-gap) to #62 as #62 is invasive. Unlike #62, this PR doesn't break `Multiaddr == nil` or `maddr1.Equals(maddr2)`. However, it *doesn't* allow multiaddrs to be used as map keys :(.
|
@Stebalien why not keep the interface, and just do: type multiaddr stringThat way we get all the same benefits, plus the ability to have nil multiaddrs (making the upgrade path easier) |
1. We can now efficiently use them as map keys. 2. We can now efficiently sort them (without copying). 3. We can (in theory) put them in protobufs without copying (yay). This commit isn't as efficient as it could be. We could improve it slightly by using unsafe casts to temporarily treat strings as bytes. However I'm not sure this will improve performance much.
9e9b220 to
241f192
Compare
08f1a80 to
a1c5e98
Compare
|
Note: I'm pretty sure that'll still require an additional allocation/indirection but it should be fairly cheep. |
|
I think Go is smart enough and allocates the type header in the same call as the string. |
It's the string header I'm worried about. Strings are usually:
And interfaces are usually:
So, a string wrapped in an interface should be:
From a bit of quick testing, it looks like this is correct. |
correct as in "it adds allocation"? |
|
Correct as in it adds a level of indirection. Go may do something sneaky with the allocation but I kind of doubt it. Go could do something to avoid an extra object to GC (e.g., have some concept of ownership) but the complexity probably isn't worth it. |
….com/multiformats/go-multiaddr-0.2.0 Bump github.com/multiformats/go-multiaddr from 0.1.0 to 0.2.0
This commit isn't as efficient as it could be. We could improve it slightly by using unsafe casts to temporarily treat strings as bytes. However I'm not sure this will improve performance much and, in general, this change will lead to fewer allocations overall.