LoginSignup
3
1

More than 3 years have passed since last update.

世界一速い Hello World サーバを V 言語で作ってみた

Last updated at Posted at 2020-01-22

はじめに

「世界一速い」の定義なんですが 2020/01/23 時点での話になります。

ベンチマークの結果

  • 秒間 734万リクエストの対象のベンチマーク結果です (pico.v というマイクロフレームワーク)

スクリーンショット 2020-01-23 8.21.34.png

対象の実装

import json
import syou.picoev
import syou.picohttpparser as hp

struct Message {
    message string
}

[inline]
fn json_response() string {
    msg := Message{
        message: 'Hello, World!'
    }
    return json.encode(msg)
}

[inline]
fn hello_response() string {
    return 'Hello, World!'
}

pub fn callback(req hp.Request, res mut hp.Response) {
    if hp.cmpn(req.method, 'GET ', 4) {
        if hp.cmp(req.path, '/t') {
            res.http_ok().header_server().header_date().plain().body(hello_response())
        }
        else if hp.cmp(req.path, '/j') {
            res.http_ok().header_server().header_date().json().body(json_response())
        }
        else {
            res.http_404()
        }
    }
    else {
        res.http_405()
    }
}

pub fn main() {
    picoev.new(8088, &callback).serve()
}

利用ライブラリなど

イベントリープの部分で kazuho さんの picoev を利用しています。 HTTP リクエストのパーサも picohttparser を一部利用しています。

HTTP のレスポンスの部分は ulib ほどはチューニングしていないのでその内 ulib に抜かれるとは思いますが、まぁ、最初の実装としては良い結果かなぁと思いました。

環境

サーバの環境としては、こちらのTechEmpower のリンクに最新情報ありますが、
- 物理サーバで Dell R440 に Intel Xeon Gold 5120 CPU, 32 GB メモリー, SSD. Cisco の 10G スイッチなどが使われています。
- クラウド版は Azure 上の D3v2インスタンスにありますが、そちらは2位でした。リンク

V 言語に関して

V 言語は C にトランスパイルされるので、gcc/clang などのコンパイラ Optimization の恩恵を受けられます。あと、開発スピードもかなり速いので今後に期待しても良いではないかと思っています。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1