5 ms·
Does it use a trie? If not, it probably should. Here's a wonderful talk on using that datastructure (with go) for the gov uk url router: https://gdstechnology.
by SEJeff 10y ago
Does it use a trie? If not, it probably should. Here's a wonderful talk on using that datastructure (with go) for the gov uk url router:
https://gdstechnology.blog.gov.uk/2013/12/05/building-a-new-router-for-gov-uk/ https://gdstechnology.blog.gov.uk/2013/12/05/building-a-new-...
The Vulcan proxy from Mailgun does the same thing:
http://vulcand.github.io/proxy.html#route http://vulcand.github.io/proxy.html#route
It looks like it doesn't, so existing open source golang url routers will perform much better, contrary to this post. The one in vuland is interesting in that it supports both regex AND tries, so you get the best of both.
Additionally, httprouter (golang) uses a trie and is fast as hell:
https://github.com/julienschmidt/httprouter https://github.com/julienschmidt/httprouter
Edit: Adding another one (thanks buro9!) that is used in production by CloudFlare:
https://github.com/pressly/chi https://github.com/pressly/chi
- dvdhnt 10y agoThese links are pretty cool. Are there any tutorials or walkthroughs you would recommend? Like, Go has always interested me but I have no idea where to start to get an experience like say ExpressJS or even where to find templating or rendering. Any suggestions are appreciated!
- cloverich 10y agoWhen I was learning Go, Negroni was where I left off and felt like what you may be looking for as a next step: https://github.com/urfave/negroni https://github.com/urfave/negroni (I"m sure someone more experienced can provide more context or other / better links -- thought this would be a better answer than none until then)
- navd 10y agoWith go there is really less emphasis on taking advantage of a web framework like expressjs (look at go alternatives like gin) and building server functionality from the std library (since everything you need is there). I wrote a small helper library to help with template rendering if you're interested in looking at the source to see how you can take advantage of the std library for templating: https://github.com/dannav/migo https://github.com/dannav/migo
- thewhitetulip 10y agohttps://github.com/thewhitetulip/web-dev-golang-anti-textbook/ https://github.com/thewhitetulip/web-dev-golang-anti-textboo... I have written a very basic introduction to writing webapps in Go without using a framework, as others have rightly pointed out, we really do not need to use a framework in Go for writing webapps, and Go is a great language. If you don't like mine, https://github.com/astaxie/build-web-application-with-golang/tree/master/en https://github.com/astaxie/build-web-application-with-golang... is a nice start for new comers.
- creshal 10y agohttprouter is really awesome. I switched to it when Go's built-in router would cause panics during msan stress testing (back in Go 1.6), and really performs well.
- alex_dev 10y agoLooks like it uses regexp... There isn't any benchmark code as one would expect when making a claim that it's "fast".
- laumars 10y agoRegexp definitely isn't something you'd want to be using if you're primary goal is speed. When running tight loops in string parsing I've found using string splitting and then cycling through the range of indices in a slice was several times faster than Regexp matching. Obviously performance difference will vary depending on the expression and application but that was enough to convince me to think twice about future usage of Regex - as to whether the problem needed Regex or if I was just using them lazily. The latter being a practice I'd slipped into after years of Perl hacking.
- jules 10y agoThat depends entirely on the regex implementation. If the implementation uses a DFA to match multiple regexes simultaneously then the performance will be as good as a trie because a DFA is more or less a trie.
- vanderZwan 10y ago> That depends entirely on the regex implementation True, and anyone who knows that Russ Cox is a core member of the Go team will have a hard time suppressing a smirk when reading this :) https://swtch.com/~rsc/regexp/ https://swtch.com/~rsc/regexp/
- laumars 10y agoTrue. I was talking specifically about the same Regexp package as the one used in the topic project though. I assumed that would have been obvious given the context however I apologise for not stating that in my comment and shall amend it appropriately. [edit: i can't add an amendment to my previous post now]
- mypalmike 10y ago
- alistproducer2 10y agoInterestingly enough, I used a trie (without knowing that's what it was called) when building the router for my Frontend framework. https://github.com/ShamariFeaster/Templar/blob/master/core/Route.js https://github.com/ShamariFeaster/Templar/blob/master/core/R...
- zbobet2012 10y agoTrie's are my favorite data structure. The implementation in the Linux kernel is a lock free RCU structure (http://lxr.free-electrons.com/source/lib/radix-tree.c http://lxr.free-electrons.com/source/lib/radix-tree.c). It is used for the page cache among other things.
- sorokod 10y agoWhy is a trie obviously better then a hash map?
- teraflop 10y agoA 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.
- jeremiep 10y agoThey 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.
- mstade 10y agoAlso, 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.
- SEJeff 10y agoFixed 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.