News for hackers. Stuff that matters.
Re:Mux – A lightweight, fast HTTP request router for Go by sorokod on Monday December 05, 2016 @01:51PM (#13108105)
Why is a trie obviously better then a hash map?
Re:Mux – A lightweight, fast HTTP request router for Go by jeremiep on Monday December 05, 2016 @01:55PM (#13108160)
They allow you to match the URL in a series of steps. Where each step can be static (ie "/foo") or dynamic (ie /images/).

You can then easily add features to this structure, like route middlewares at every level in the tree; just apply them while doing the routing.

This also allows you to match route variants easily, ie "foo" and "foo/" are the same, so are "/" and "/index.html". In the same way you can extract query parameters for middleware/handler use.

Re:Mux – A lightweight, fast HTTP request router for Go by mstade on Monday December 05, 2016 @03:37PM (#13109247)
Also, tries means it's trivial to do detect routing ambiguities, so your routing table doesn't necessarily have to be built up front, or in any particular order. This is particularly powerful when you're developing an application with a team distributed both over space and time.
Re:Mux – A lightweight, fast HTTP request router for Go by zbobet2012 on Monday December 05, 2016 @02:54PM (#13108781)
A trie is O(k), where k is the length of the key on inserts and deletes. Generally, any hashing algorithm is also O(k) with the additional overhead of the lookup in the table. This also ignores that most hashes have only amortized O(1) inserts. A trie usually also exhibits better memory locality and caching behavior. Also, a trie (like most tree data structures) can be implemented in a lock-free format fairly easily.
Re:Mux – A lightweight, fast HTTP request router for Go by teraflop on Monday December 05, 2016 @01:54PM (#13108142)
A hash map doesn't solve the same problem. Given a collection of routes, you need to find the longest one which is a prefix of the given URL.
Re:Mux – A lightweight, fast HTTP request router for Go by buckhx on Monday December 05, 2016 @05:59PM (#13110654)
A trie can efficiently match all routes that start with a prefix. With a hashmap you wouldn't be off any better than a list efficiency wise.
Re:Mux – A lightweight, fast HTTP request router for Go by SEJeff on Monday December 05, 2016 @02:03PM (#13108235)
Fixed lookup time. A trie (aka radix tree) 3 levels deep with 1 million entries has the same lookup time as a 3 level deep trie with 5 entries.
Re:Mux – A lightweight, fast HTTP request router for Go by papaf on Monday December 05, 2016 @02:07PM (#13108266)
Isn't the same thing true for a 3 level deep hashmap of hashmaps?
Re:Mux – A lightweight, fast HTTP request router for Go by SEJeff on Monday December 05, 2016 @04:18PM (#13109706)
See a much better answer than mine: https://news.ycombinator.com/item?id=13108781