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

What about backing cid.Set by a sorted slice rather than a map? #45

Open
aboodman opened this issue May 16, 2018 · 3 comments
Open

What about backing cid.Set by a sorted slice rather than a map? #45

aboodman opened this issue May 16, 2018 · 3 comments

Comments

@aboodman
Copy link

It's pretty common when using cid.Set to want them to remain sorted. For example, if you're going to compare two sets, you need them to be sorted. If you're going to compute a hash representing the set, you need to do it over a sorted set, otherwise two hashes for equal sets won't be equal.

We could introduce a new cid.SortedSet for this use case, but I think it is worth considering making cid.Set always sorted and backing it by a sorted slice rather than a map.

I can't recall the exact details but I think insertion and lookup in a sorted slice in Go is negligibly different performance than into a map. More importantly, with a slice, you have the option of using a stack allocated array as the backing buffer, eliminating a heap allocation, reducing gc pressure.

@Stebalien
Copy link
Member

I'm fine with that but I'd benchmark it. At the moment, it'll probably be faster as you'll be able to compare CIDs without allocating (while we currently end up allocating a string for the key). We've discussed using a string to back the CID (I believe there's an open PR) but this would probably require a bit of refactoring.

@dignifiedquire
Copy link
Member

string pr is in #47

@anorth
Copy link
Member

anorth commented Nov 8, 2019

As reference, go-filecoin implements an immutable sorted CID set in a slice as TipSetKey.

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

No branches or pull requests

4 participants