How are various routers are "faster" than each other?

blov · · 471 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>As far as I understood all of the golang routers (mux, chi, gin etc.) are based on the native golang http package but they claim to be faster than each other.</p> <p>How is this possible given that they are all based on the same package?</p> <hr/>**评论:**<br/><br/>shovelpost: <pre><p>Please do not obsess over routers. Their difference in speed, if any, is negligible compared to the network and disk IO of a standard web app. Just pick one and move on.</p></pre>HugoWeb: <pre><p>Its about how the router stores the patterns on startup and, when running, does the pattern matching needed to dispatch an incoming request.</p> <p>&#34;The router is optimized for high performance and a small memory footprint. It scales well even with very long paths and a large number of routes. A compressing dynamic trie <em>(radix tree)</em> structure is used for efficient matching.&#34; (<a href="https://github.com/julienschmidt/httprouter" rel="nofollow">https://github.com/julienschmidt/httprouter</a>)</p> <p>&#34;chi&#39;s router is based on a kind of <em>Patricia Radix trie</em>. The router is fully compatible with net/http.&#34; (<a href="https://github.com/go-chi/chi/" rel="nofollow">https://github.com/go-chi/chi/</a></p></pre>TheMerovius: <pre><p>Spoiler alert: They aren&#39;t. All of them are, for all intents and purposes, infinitely fast.</p> <p>(routing cost, while not being zero, ends up being completely negligible, no matter what scale you are running at and basically no matter how naively you are doing it. Don&#39;t worry about it)</p></pre>dtfinch: <pre><p>I was getting 20k requests/second to a small vps with just the builtin ServeMux, while updating a database with each request. That was enough for the few urls that I had to handle. I&#39;ve never come close to needing something faster.</p> <p>The problem might come if I had lots and lots of url patterns, or other special needs. If you&#39;ve got 1000 patterns in ServeMux, that&#39;s has the potential to be 1000x slower, if it has to check requests against all of them. You&#39;d want something more specialized that can handle them in o(1) or o(log n) time instead of o(n).</p></pre>shark1337: <pre><p>Most of the times you don&#39;t even need a router.. Just use a switch case on the req.Path and you&#39;re all set up.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

471 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传