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

Why do you use a sstree instead of a key/value pair? #50

Closed
ruralcoder opened this issue Feb 2, 2022 · 3 comments
Closed

Why do you use a sstree instead of a key/value pair? #50

ruralcoder opened this issue Feb 2, 2022 · 3 comments

Comments

@ruralcoder
Copy link

Hi Cenkati,

Been thinking of a project around torrent and since I love golang, i came across your project. Thank you!

I only had a small question on the use of trees. Why not use a key/value pair with O(1) access and free dedup. Just curious (not a bug)

tree stree.Stree

I would love to talk to you about torrent. I have tons of questions. Do you use discord or any other platform??

@cenkalti
Copy link
Owner

cenkalti commented Feb 2, 2022

IPv4 address range is 32 bits and the memory cost of storing each address separately in a map would be too high.
For the same reason blocklists are usually distributed in CIDR format. Segment tree is a data structure to allow fast (O(logn)) lookups within stored ranges.

Segment tree: https://en.wikipedia.org/wiki/Segment_tree

Original implementation: https://github.com/toberndo/go-stree

Example list source: https://www.iblocklist.com/lists?fileformat=cidr&archiveformat=gz

Doc:

// URL to the blocklist file in CIDR format.

I don't use Discord but you can email me for other questions.

@cenkalti cenkalti closed this as completed Feb 2, 2022
@ruralcoder
Copy link
Author

ruralcoder commented Feb 3, 2022

Cool, thanks. I will email you.

One cool thing about golang is that keys don't need to be strings.

So your key could easily be a 32bit int, though you lose 1 byte in the bool. But then each of your tree nodes use 2x 8byte for the left, right pointers.

map[int32]bool{ ipA: true, ipB: true }

Now perhaps your blocklist is not used much hence not an issue.

Was playing with rain on the command like to see how it works....really cool. Works like a charm.

@cenkalti
Copy link
Owner

cenkalti commented Feb 5, 2022

The memory space is one part of the problem. The other one is searching. How do you check if an address is blocked by one of the ranges in the blocklist with a map type?

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

2 participants