$30 off During Our Annual Pro Sale. View Details »

fiberについて

kuro
July 17, 2022

 fiberについて

kuro

July 17, 2022
Tweet

Other Decks in Programming

Transcript

  1.    fiberについて
    kuro

    View Slide

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

    View Slide

  3. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  11. fasthttpも相当速い

    View Slide

  12. なぜ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が回避してい
    る、不要なアロケーションが必要になります。

    View Slide

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

    View Slide

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

    View Slide