Slide 1

Slide 1 text

   fiberについて kuro

Slide 2

Slide 2 text

自己紹介 kuro ・22卒エンジニア ・Goが好き、仕事でも使ってる。 ・SO Techonologies株式会社 という会社で働いています。 ミミッキュも好きです。

Slide 3

Slide 3 text

Goのフレームワークは何がいいのか GitHubスター数(2022年7月3日時点) ・ gin 60.7k stars ・ beego 28.4k stars ・ mux 16.9k stars ・ echo 22.8k stars ・ fasthttp 18k stars ・ fiber 20.8k stars

Slide 4

Slide 4 text

「Go フレームワーク 比較」 で調べると・・・ https://github.com/smallnest/go-web-framework-benchmark ベンチマークを調べたものが、、、

Slide 5

Slide 5 text

速いものに憧れる・・・!! やはり、速ければ速いほど良さそう。。ロマンもある。。。

Slide 6

Slide 6 text

実際に手元でも試してみる。。。 fiber

Slide 7

Slide 7 text

実際に手元でも試してみる。。。 fiber  1,経過: 27938ns 2,経過: 25525ns 3,経過: 29585ns 4,経過: 25614ns 5,経過: 24513ns 6,経過: 30206ns 7,経過: 24597ns 8,経過: 25819ns 9,経過: 26585ns 10,経過: 45435ns 平均28581.7ns

Slide 8

Slide 8 text

実際に手元でも試してみる。。。 gin

Slide 9

Slide 9 text

実際に手元でも試してみる。。。 gin 1,経過: 86115ns 2,経過: 56648ns 3,経過: 65380ns 4,経過: 118521ns 5,経過: 62619ns 6,経過: 68169ns 7,経過: 57073ns 8,経過: 92922ns 9,経過: 79548ns 10,経過: 57686ns 平均74468.1ns

Slide 10

Slide 10 text

なぜそんなに速いのか? fasthttpがfiberの多くのメソッドで使われている。。。

Slide 11

Slide 11 text

fasthttpも相当速い

Slide 12

Slide 12 text

なぜfasthttpは速いのか? https://github.com/valyala/fasthttp ● Why creating yet another http package instead of optimizing net/http? Because net/http API limits many optimization opportunities. For example: ○ net/http Request object lifetime isn't limited by request handler execution time. So the server must create a new request object per each request instead of reusing existing objects like fasthttp does. ○ net/http headers are stored in a map[string][]string . So the server must parse all the headers, convert them from []byte to string and put them into the map before calling user-provided request handler. This all requires unnecessary memory allocations avoided by fasthttp. ○ net/http client API requires creating a new response object per each request. なぜ、net/httpを最適化する代わりに、さらに別のhttpパッケージを作成するのですか? net/http APIは、多くの最適化の機会を制限しているからです。 net/http ヘッダは map[string][]string に格納されます。ですから、サーバはすべてのヘッダを解析し、 []byte から文字列に変換 し、ユーザが提供するリクエストハンドラを呼ぶ前にそれらを mapに入れなければなりません。これには、 fasthttpが回避してい る、不要なアロケーションが必要になります。

Slide 13

Slide 13 text

実際に見てみる。 fasthttp バイト列のままヘッダー情報を 保持している。

Slide 14

Slide 14 text

まとめ ・fiberはとにかく速い。 ・その速さの理由の一つは、fasthttpを使っているから。 ・fasthttpの速さの理由の一つは、net/httpと異なり、ヘッダーのデータを不要なパースを 避けるために、バイト列のまま保持しているから。 ・fiber使っていきたい!!