Upgrade to Pro — share decks privately, control downloads, hide ads and more …

天下一HTTPRouter武闘会.pdf

bmf_san
December 14, 2022

 天下一HTTPRouter武闘会.pdf

bmf_san

December 14, 2022
Tweet

More Decks by bmf_san

Other Decks in Programming

Transcript

  1. ఱԼҰHTTPRouter෢ಆձ @bmf_san
 2022.12.14 Makuake LT

  2. Who am Iʁ • ࣗ࡞HTTP Routerܳਓ

  3. ʮԶΑΓڧౕ͍ʹձ͍ʹߦ͘ʯ

  4. Զ https://github.com/bmf-san/goblin

  5. Goblin • ԶʹΑΔԶͷͨΊͷԶͷHTTP Router • MIT LicenseͳͷͰԶҎ֎΋࢖͑Δͧ • net/httpޓ׵ •

    HTTP Routerͱͯ͠ͷجຊతͳػೳΛ͍࣋ͬͯΔ͸ͣ • Trie໦ϕʔε • awesome-goʹܝࡌ
  6. Trie໦ͱ͸ • จࣈྻͷू߹Λѻ͏໦ߏ଄ͷҰछ • ŧŐşŽ • https://github.com/bmf-san/road-to-algorithm- master/blob/master/data_structures/tree/trie/ trie.go

  7. ΦϨΦϨτϥΠ໦

  8. ΋͏ͪΐͬͱৄ͍͠࿩͸ • ŧŐşŽʢڵຯ࣋ͬͨਓ͸ݟͯΈ͍ͯͩ͘͞:Dʣ • https://speakerdeck.com/bmf_san/ httpdetukuruhttprutazi-zuo-ru-men

  9. ͰɺԿͷ࿩ʁ • ԶΑΓڧ͍΍ͭʹձ͍͍ͨ • ഊ๺Λ஌Γ͍ͨʢϑϥάʣ

  10. HPCMJOͱଞͷ)5513PVUFSͷ ύϑΥʔϚϯεΛൺֱ͍ͨ͠ʂʂ

  11. ϕϯνϚʔΧʔ࡞ͬͨΝ • https://github.com/bmf-san/go-router-benchmark • ݩʑ༏लͳϕϯνϚʔΧʔ͕͕͋ͬͨɾɾ • https://github.com/julienschmidt/go-http-routing- benchmark • ϝϯςࢭ·ͬͪΌͬͨ

    • Goblin௥ՃͷPRग़͚ͨ͠ͲClose͞ΕͪΌͬͨ..
  12. લఏ • HTTP RouterʹΑͬͯ౰વػೳ͕ࠩ͋Δ • ͢΂ͯΛΧόʔ͢Δςετ͸೉͍͠ • ׬શͳύϑΥʔϚϯεൺֱͰ͸ͳ͍ • ֤HTTP

    Routerͷσʔλߏ଄ͱΞϧΰϦζϜͷಘҙɾෆಘҙ ͕͋ΔͷͰɺςετέʔεʹΑͬͯಘखෆಘखͷ͕ࠩग़Δ
  13. ςετέʔε • ”୳ࡧ”ʢϧʔςΟϯάʣ͚ͩςετ • ”ૠೖ”ʢϧʔςΟϯάͷఆٛʣ͸ର৅֎ • Rootʢ/ʣͷϧʔςΟϯάΛ1ύλʔϯ • ੩తͳϧʔςΟϯάΛ3ύλʔϯ •

    Ex. /foo, /foo/bar • ಈతͳϧʔςΟϯάΛ3ύλʔϯ • Ex. /foo/:bar, /foo/:bar/:baz • cf. https://github.com/bmf-san/go-router-benchmark/blob/ main/benchmark.go
  14. ର৅ͷHTTP Router • HTTP RouterιϜϦΤͷޗഐ͕બΜͩHTTP Router 14બ • ։ൃ͕ࢭ·͍ͬͯΔ΋ͷ΋͋Δ͕ɺߴੑೳɾߴػೳΛᨳ͍ͬͯΔ΋ͷ͸ϐοΫΞοϓ •

    ༗໊ॴ͸཈͍͑ͯΔ͸ͣɾɾ • bmf-san/goblin • julienschmidt/httprouter • go-chi/chi • gin-gonic/gin • uptrace/bunrouter • dimfeld/httptreemux • beego/mux • gorilla/mux • nissy/bon • naoina/denco • labstack/echo • gocraft/web • vardius/gorouter • go-ozzo/ozzo-routing
  15. ϕϯνϚʔΧʔ࣮ߦ݁Ռ go test -bench=. -benchmem goos: darwin goarch: amd64 pkg:

    github.com/go-router-benchmark cpu: VirtualApple @ 2.50GHz BenchmarkStaticRoutesRootGoblin-8 37810608 31.62 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes1Goblin-8 17207332 68.55 ns/op 16 B/op 1 allocs/op BenchmarkStaticRoutes5Goblin-8 5786544 208.3 ns/op 80 B/op 1 allocs/op BenchmarkStaticRoutes10Goblin-8 2992412 400.3 ns/op 160 B/op 1 allocs/op BenchmarkPathParamColonRoutes1Goblin-8 1907245 627.5 ns/op 409 B/op 6 allocs/op BenchmarkPathParamColonRoutes5Goblin-8 525704 2269 ns/op 965 B/op 13 allocs/op BenchmarkPathParamColonRoutes10Goblin-8 275090 4399 ns/op 1608 B/op 19 allocs/op BenchmarkStaticRoutesRootHTTPRouter-8 100000000 10.46 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes1HTTPRouter-8 100000000 10.33 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes5HTTPRouter-8 100000000 10.44 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes10HTTPRouter-8 100000000 10.36 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes1HTTPRouter-8 27414811 43.20 ns/op 32 B/op 1 allocs/op BenchmarkPathParamColonRoutes5HTTPRouter-8 10295874 114.4 ns/op 160 B/op 1 allocs/op BenchmarkPathParamColonRoutes10HTTPRouter-8 6092434 197.3 ns/op 320 B/op 1 allocs/op BenchmarkStaticRoutesRootChi-8 5429904 220.0 ns/op 304 B/op 2 allocs/op BenchmarkStaticRoutes1Chi-8 5290057 220.6 ns/op 304 B/op 2 allocs/op BenchmarkStaticRoutes5Chi-8 5434046 219.3 ns/op 304 B/op 2 allocs/op BenchmarkStaticRoutes10Chi-8 5505698 212.7 ns/op 304 B/op 2 allocs/op BenchmarkPathParamBracketRoutes1Chi-8 4445071 268.9 ns/op 304 B/op 2 allocs/op BenchmarkPathParamBracketRoutes5Chi-8 2730680 437.0 ns/op 304 B/op 2 allocs/op BenchmarkPathParamBracketRoutes10Chi-8 1888065 636.3 ns/op 304 B/op 2 allocs/op BenchmarkStaticRoutesRootGin-8 32862794 36.33 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes1Gin-8 33070339 36.07 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes5Gin-8 32586816 36.17 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes10Gin-8 32143107 37.20 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes1Gin-8 27500701 42.62 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes5Gin-8 15258900 78.41 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes10Gin-8 9522596 127.7 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutesRootBunRouter-8 64215045 18.42 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes1BunRouter-8 56828720 21.02 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes5BunRouter-8 56755030 20.99 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes10BunRouter-8 56908671 20.84 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes1BunRouter-8 35162946 33.80 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes5BunRouter-8 8425648 140.7 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes10BunRouter-8 4257427 283.7 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutesRootHTTPTreeMux-8 6690556 179.2 ns/op 328 B/op 3 allocs/op BenchmarkStaticRoutes1HTTPTreeMux-8 6562962 183.0 ns/op 328 B/op 3 allocs/op BenchmarkStaticRoutes5HTTPTreeMux-8 5362312 224.5 ns/op 328 B/op 3 allocs/op BenchmarkStaticRoutes10HTTPTreeMux-8 4219711 284.1 ns/op 328 B/op 3 allocs/op BenchmarkPathParamColonRoutes1HTTPTreeMux-8 3174328 377.0 ns/op 680 B/op 6 allocs/op BenchmarkPathParamColonRoutes5HTTPTreeMux-8 1545889 757.7 ns/op 904 B/op 9 allocs/op BenchmarkPathParamColonRoutes10HTTPTreeMux-8 807580 1474 ns/op 1742 B/op 11 allocs/op BenchmarkStaticRoutesRootBeegoMux-8 22258030 52.52 ns/op 32 B/op 1 allocs/op BenchmarkStaticRoutes1BeegoMux-8 15893838 75.32 ns/op 32 B/op 1 allocs/op BenchmarkStaticRoutes5BeegoMux-8 1000000 1071 ns/op 32 B/op 1 allocs/op BenchmarkStaticRoutes10BeegoMux-8 592300 2029 ns/op 32 B/op 1 allocs/op BenchmarkPathParamColonRoutes1BeegoMux-8 3219261 373.9 ns/op 672 B/op 5 allocs/op BenchmarkPathParamColonRoutes5BeegoMux-8 824432 1420 ns/op 672 B/op 5 allocs/op BenchmarkPathParamColonRoutes10BeegoMux-8 354176 3350 ns/op 1254 B/op 6 allocs/op BenchmarkStaticRoutesRootGorillaMux-8 2076849 574.2 ns/op 720 B/op 7 allocs/op BenchmarkStaticRoutes1GorillaMux-8 2066920 579.6 ns/op 720 B/op 7 allocs/op BenchmarkStaticRoutes5GorillaMux-8 1967010 610.1 ns/op 720 B/op 7 allocs/op BenchmarkStaticRoutes10GorillaMux-8 1824106 658.2 ns/op 720 B/op 7 allocs/op BenchmarkPathParamBracketRoutes1GorillaMux-8 1339666 895.5 ns/op 1024 B/op 8 allocs/op BenchmarkPathParamBracketRoutes5GorillaMux-8 526460 2262 ns/op 1088 B/op 8 allocs/op BenchmarkPathParamBracketRoutes10GorillaMux-8 238946 5008 ns/op 1751 B/op 9 allocs/op BenchmarkStaticRoutesRootBon-8 88309966 13.51 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes1Bon-8 87832992 13.51 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes5Bon-8 86620446 13.48 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes10Bon-8 87585646 14.03 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes1Bon-8 6392168 186.3 ns/op 304 B/op 2 allocs/op BenchmarkPathParamColonRoutes5Bon-8 4628920 259.3 ns/op 304 B/op 2 allocs/op BenchmarkPathParamColonRoutes10Bon-8 3381514 355.9 ns/op 304 B/op 2 allocs/op BenchmarkStaticRoutesRootDenco-8 79634784 14.46 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes1Denco-8 79188319 14.38 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes5Denco-8 79437754 14.36 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes10Denco-8 74332171 15.32 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes1Denco-8 19618006 60.16 ns/op 32 B/op 1 allocs/op BenchmarkPathParamColonRoutes5Denco-8 8545141 140.5 ns/op 160 B/op 1 allocs/op BenchmarkPathParamColonRoutes10Denco-8 5004351 239.7 ns/op 320 B/op 1 allocs/op BenchmarkStaticRoutesRootEcho-8 33267644 35.72 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes1Echo-8 30976101 38.24 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes5Echo-8 21207642 56.36 ns/op 0 B/op 0 allocs/op BenchmarkStaticRoutes10Echo-8 12649693 94.39 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes1Echo-8 26137147 45.25 ns/op 0 B/op 0 allocs/op BenchmarkPathParamColonRoutes5Echo-8 11194768 106.2 ns/op 0 B/op 0 allocs/op
  16. $47ʹՃ޻ͯ͠εϓγͰ੔ཧͨ͠

  17. static-root • ςετέʔεɿ/ • goblin͸தؒ૚ͱ͍ͬͨײ͡ • ্Ґ૚ʢTop 4ʣͱͷࠩ͸݁ߏ͋Δ

  18. static-1 • ςετέʔεɿ/foo • goblin͸͜ͷஈ֊Ͱmemallocs͕ग़ͯ͠·͏

  19. static-5 • ςετέʔεɿ/foo/bar/baz/qux/quux • goblin͸memallocs͸1ճͰ͸ࡁΜͰ͍Δ͕ɺbeego/muxʹൈ͔ΕΔ • ্Ґ૚͕ڧ͍ɻύϑΥʔϚϯεྼԽ͕΄ͱΜͲͳ͍ • static-1 ~

    5Λશ෦·ͱΊͨέʔεͰͷςετ΋͢Ε͹Α͔ͬͨ.. pathparam΋ ಉ༷
  20. static-10 • ςετέʔεɿ/foo/bar/baz/qux/quux/corge/grault/garply/waldo/fred • goblin͸͞ΒʹॱҐΛམͱ͢ • ্Ґ૚͸ϝϞϦׂΓ౰͕ͯগͳ͍ • beego/mux΋ϝϞϦׂΓ౰ͯ͸গͳΊ͕ͩɺ࣮ߦ଎౓஗Ί

  21. pathparam-1 • ςετέʔεɿ/foo/:bar • ্Ґ૚͸ੑೳ͕፰߅͍ͯ͠Δ • goblin͸ॳઓ͔Βݫ͍͠ઓ͍ • staticͷͱ͖͸HTTP Routerʹ܉഑্͕͕͕ͬͨɺpathparam͸Ginͷ΄͏͔

    • ginͷ಺෦͸HTTP Routerϕʔεͷ࣮૷ʹͳ͍ͬͯΔ
  22. pathparam-5 • ςετέʔεɿ/foo/:bar/:baz/:qux/:quux/:corge • goblin͸ϫʔετ1ʹ • pathparam-3͘Β͍΋ݟ͓ͯ͘΂͖͔ͩͬͨ΋

  23. pathparam-10 • ςετέʔεɿ/foo/:bar/:baz/:qux/:quux/:corge/:grault/:garply/:waldo/:fred/:plugh • ginڧ͗͢ • goblin͞Μ…

  24. ݁Ռൃද • static෦໳ • HTTP Router, Bon, Denco͕Top3 • goblin͸্Ґ૚ͱͷԿ͔௒͑ΒΕͳ͍นΛײͨ͡

    • pathpara෦໳ • gin, ozzo-routing, echo͕Top3 • goblin͸࠷ۙΞʔΧΠϒ͞Εͨ͋Β͍͠gorilla/muxͱ͸ྑ͍উෛ • ্Ґ૚ͷσʔλߏ଄͸ߴ౓ʹ࠷దԽ͞Εͨ໦ • echoɺginɺhttprouterɺbonɺchi ͸Radixɺdenco͸Double array • ՐΛݟΔΑΓ໌Β͔͕ͩɺΦϨΦϨτϥΠͰ͸উͯͳ͍ • খखઌͷվળͰ͸௥͍͚ͭͳ͍ɻߴ౓ͳ໦͕ඞཁ • ߴ౓ͳ໦ͷ࠾༻ˠϚΠΫϩͳνϡʔχϯά • ֤HTTP Routerͷ࣮૷ͪΌΜͱݟͯͳ͍ͷͰݚڀ͍ͨ͠ͱ͜Ζ
  25. ఱԼҰHTTPRouter෢ಆձʹࢀՃ͠Α͏ • ࣗ࡞HTTP RouterΛ࡞ͬͯࢀઓ͍ͨ͠ਓ • ŧŐşŽʢ؆қతͳTrie໦ϕʔεͷHTTP Routerʣ • https://github.com/bmf-san/introduction-to-golang-http- router-made-with-net-http

    • 1Ոʹ1୆HTTP Router͕ͭ͘ΕΔ͸ͣ
  26. ʮԶΑΓڧౕ͍͔͍͠ͳ͔ͬͨʯ ʮഊ๺Λݏͱ͍͏΄Ͳ஌ͬͨʯ ʙʢ׬ʣʙ