éãã¯æ£ç¾©ãã©ãããããããã§ãã
Rust ã§ä¸çªéã HTTP ãã¼ãµã¼ã¯ C ã§æ¸ããã picohttpparser
ã® Rust ãã¤ã³ãã£ã³ã°ãpicohttpparser-sys ã§ããã¬ããã
$ RUSTFLAGS="-C target-feature=+sse4.2" cargo +nightly bench Finished bench [optimized] target(s) in 0.28s warning: the following packages contain code that will be rejected by a future version of Rust: traitobject v0.1.0 note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 78` Running unittests src/main.rs (target/release/deps/rust_http_parser_bench-63366b14855362d6) running 16 tests test tests::bench_dumb_http_parser ... bench: 2,085 ns/iter (+/- 537) = 337 MB/s test tests::bench_http_box ... bench: 592 ns/iter (+/- 43) = 1187 MB/s test tests::bench_http_bytes ... bench: 2,347 ns/iter (+/- 341) = 299 MB/s test tests::bench_http_muncher ... bench: 795 ns/iter (+/- 141) = 884 MB/s test tests::bench_http_parser ... bench: 3,791 ns/iter (+/- 594) = 185 MB/s test tests::bench_http_pull_parser ... bench: 6,990 ns/iter (+/- 658) = 100 MB/s test tests::bench_http_tiny ... bench: 12,675 ns/iter (+/- 1,219) = 55 MB/s test tests::bench_httparse ... bench: 276 ns/iter (+/- 11) = 2547 MB/s test tests::bench_milstian_http ... bench: 23,279 ns/iter (+/- 2,616) = 30 MB/s test tests::bench_picohttpparser_sys ... bench: 135 ns/iter (+/- 24) = 5207 MB/s test tests::bench_rhymuweb ... bench: 6,868 ns/iter (+/- 631) = 102 MB/s test tests::bench_rocket_http_hyper ... bench: 4,119 ns/iter (+/- 420) = 170 MB/s test tests::bench_saf_httparser ... bench: 5,870 ns/iter (+/- 528) = 119 MB/s test tests::bench_stream_httparse ... bench: 1,781 ns/iter (+/- 202) = 394 MB/s test tests::bench_thhp ... bench: 177 ns/iter (+/- 14) = 3971 MB/s test tests::bench_uhttp_request ... bench: 261 ns/iter (+/- 24) = 2693 MB/s test result: ok. 0 passed; 0 failed; 0 ignored; 16 measured; 0 filtered out; finished in 19.03s
Pure Rust 㪠HTTP ãã¼ãµã¼ã§ä¸çªéãã®ã¯ thhp
ã§ããã
ç°å¢
- Macbook Pro (13-inch, 2018, Four Thunderbolt 3 Ports)
- Processor: 2.7 GHz Quad-Core Intel Core i7
- Memory: 16 GB 2133 MHz LPDDR3
$ cargo -V cargo 1.68.0-nightly (8c460b223 2023-01-04)
èæ¯
ä»äºã§ Rust ã使ãããã«ãªãä½ã Rust ã使ã£ã¦èªåã§ä½ãããã¨æã£ãã®ãçºç«¯ã§ãã
1ããä½ãã®ã¯å¤§å¤ãªã®ã§ä½ãæåãªãããã¯ãã Rust ã«ç§»æ¤ãããããªã¨æã£ã¦ãã¦ããã¨ãã¨èå³ã®ãã£ã HTTP ãµã¼ãããã¼ã¿ãã¼ã¹ãè²ã 調ã¹ã¦ãã¾ããã
ããã§ãh2o ã®ä½è ã® Kazuho Oku ããã®ã¹ã©ã¤ããè¦ã¤ãã¦æéãåãã¾ãã2014 å¹´ã®çºè¡¨è³æã§ããä»ã§ãæåã§ããè³æã§ããç¹ã« 21 ãã¼ã¸ã® "characteristics of a fast program" ã¯ã¨ã¦ãè¯ãã£ãã§ãã
- characteristics of a fast program
- executes less instructions
- speed is a result of simplicity, not complexity
- causes less pipeline hazards
- minimum number of conditional branches / indirect calls
- use branch-predictor-friendly logic
www.slideshare.net
ãã®ã¹ã©ã¤ãã§ã¯æéã® HTTP ãã¼ãµã¼ã¨ã㦠picohttpparser
ãç´¹ä»ããã¦ãã¾ãããããã§ãpicohttpparser
ã Rust ã«ç§»æ¤ãããã¨ã«ãã¾ããã
ãããã§ã« Rust 㧠picohttpparser
ããéã HTTP ãã¼ãµãå®è£
ããã¦ãããæ°è¦æ§ãèããã®ã§ Rust ã® HTTP ãã¼ãµã®ãã³ããã¼ã¯ãåããã¨ã«ãã¾ããã
調æ»
Rust ã® HTTP ãã¼ãµã¯ crates.io 㧠"http parser" ã¨èª¿ã¹ã¦åºã¦ãããã®ãçã£ã±ããããã³ããã¼ã¯ã«çªã£è¾¼ã¿ã¾ãã
ããã¤ãã®ã¯ã¬ã¼ãã¯ä»¥ä¸ã®çç±ã§ãã³ããã¼ã¯ãåãã¾ããã§ããã
- https://crates.io/crates/safe_http_parser
- ãªãããã¦ã³ãã¼ãã§ããªãã
- https://crates.io/crates/simple_http_parser
- ã¤ã³ã¿ã¼ãã§ã¼ã¹ã
std::net::TCPStream
ããåãä»ããªãããæ¯è¼ã§ãããå ¨ç¶ simple ãããªãã
- ã¤ã³ã¿ã¼ãã§ã¼ã¹ã
- https://crates.io/crates/http_request_parser
- simple_http_parser ã¨åãçç±
- https://crates.io/crates/li-async-h1
- ãã¼ãµã¯ httparse ã使ã£ã¦ãã
- https://crates.io/crates/async-h1
- li-async-h1 ã¨åãçç±
- https://crates.io/crates/http_headers
- 空ã®
it_works()
ãããªãã©ã¤ãã©ãªã»ã»ã»ãååã®äºç´ã ãããã®ããªã»ã»ã»ã
- 空ã®
- https://crates.io/crates/pico
- æå¾ ãã¦ããã crate ãå£ãã¦ãã¦ã³ã³ãã¤ã«ã§ããªãã£ãã
ãã®çµæãæåã«è²¼ãä»ãããã°ã§ãã
ãªãéãã®ã
Pure Rust ãªãã®ã§ç¹ã«æ©ãã£ãã¯ã¬ã¼ã㯠thhp
ãhttparse
ãuhttp_request
ã® 3 ã¤ã§ããããããã®ã©ãã picohttpparser
ã¨åãããã«ã¹ãã¼ãã¬ã¹ã«ãã¼ã¹ãã¦ãã¾ãã
uhttp_request
ã¯æ¹è¡ã§ split ããå¾ã«ããã«ç©ºç½ã§ split ãããªã©ããç¡é§ãããããã«æãã¾ãããã¾ããåæåãæ£ããå¤ã«ãªã£ã¦ãããã® validation ããã¦ãã¾ããããã®ãããã¾ãåèã«ã¯ãªããªããããããªãã§ãã
httparse
ã¯ãã¦ã³ãã¼ãæ°ã®å¤ãã¯ã¬ã¼ããªã®ã§ Rust ã®ä¸ã®ããã¡ã¯ãã®ã©ã¤ãã©ãªã ã¨æããã¾ãã
thhp
㯠Totemo-Hayai (ã¨ã¦ãéã) HTTP Parser ã®ç¥ã ããã§ããä½è
ã®æ¹ã® Twitter ãè¦ãéããpicohttpparser
ãæèãã¦æ¸ããã¦ããããã§ãã
æè¿æ¸ãã¦ã http parser ã SSE 使ããªã picohttpparser ç¨åº¦ã«ã¯æ©ããªã£ãã
— ããã ããã (@kei10in) 2018å¹´4æ4æ¥
httparse
㨠thhp
ã®ã³ã¼ããèªãã¨ããããéããããã¾ãããã ãã©ããææãªå·®ãçã¿åºãã¦ããã®ãã¯ããã«èª¿æ»ãå¿
è¦ããã§ãã
- method ã®è©ä¾¡
httparse
: å é 4 ãã¤ãã match æ§æã§æ¯è¼thhp
: 1 æåãã¤TCHAR_MAP
ã§æ¤è¨¼picohttpparser
: SIMD ã¨token_char_map
ã§æ¤è¨¼
- path ã®è©ä¾¡
- version ã®è©ä¾¡
httparse
: å é 8 ãã¤ãã match æ§æã§æ¯è¼thhp
: å é 8 ãã¤ãã 1 æåãã¤è©ä¾¡picohttpparser
: å é 8 ãã¤ãã 1 æåãã¤è©ä¾¡
- field name ã®è©ä¾¡
httparse
:parse_headers_iter_uninit
ã§ãã£ã¦ããããªããé·ããHEADER_NAME_MAP
ã§ï¼æåãã¤è©ä¾¡ãã¦ããthhp
: 1 æåãã¤TCHAR_MAP
ã§æ¤è¨¼picohttpparser
: SIMD ã¨token_char_map
ã§æ¤è¨¼
- filed value ã®è©ä¾¡
çµè«
thhp
ããã§ã«ããã®ã§ picohttpparser
ã® Rust ã¸ã®ç§»æ¤ã¯è«¦ãã¾ãããããthhp
㯠filed value 以å¤ã§ã SIMD ã使ãããã«ããã°ãã£ã¨éããªãããã