Slide 1

Slide 1 text

ISUCONʹRustͰ௅ઓͨ͠࿩ RustͷLTձ Shinjuku.rs #12 @ΦϯϥΠϯ https://forcia.connpass.com/event/187287/ 2020/10/20 ͯ͢ʹΌΜ (id:stefafafan)

Slide 2

Slide 2 text

໨࣍ • ࣗݾ঺հ • ISUCONͱ͸ʁ • ࣄલ४උ΍ௐ΂ͨRustͷCrateͷ঺հ • ISUCON༧બ • ֶͼɾײ૝ͳͲ 2

Slide 3

Slide 3 text

ࣗݾ঺հ • ͯ͢ʹΌΜ (id:stefafafan) • גࣜձࣾ͸ͯͳͷࣾһ • WebΞϓϦέʔγϣϯΤϯδχΞ • Rustॳ৺ऀ • ISUCON10͸Trust RustνʔϜͱͯ͠ࢀՃ

Slide 4

Slide 4 text

ISUCONͱ͸ʁ

Slide 5

Slide 5 text

ISUCONͱ͸ • LINEגࣜձࣾओ࠵ʮIikanjini Speed Up ContestʯͷུশͰ͋Γɺ༩͑ ΒΕͨWebΞϓϦέʔγϣϯΛߴ଎Խ͠ڝ͍߹͏ίϯςετ • 1ਓʙ3ਓͷνʔϜͰࢀՃ͢Δ͜ͱ͕Ͱ͖ͯɺΞϓϦέʔγϣϯίʔυ ͷΈͳΒͣྫ͑͹nginx΍MySQLͷνϡʔχϯάͳͲ΋ߦ͏͜ͱʹͳ Δ • ༻ҙ͞ΕͨϕϯνϚʔΫΛ࣮ߦ͢Δ͜ͱʹΑͬͯʮ఺਺ʯ͕֬ೝͰ͖ Δ

Slide 6

Slide 6 text

ISUCONͱ͸ • ࣾձਓ΍ֶੜ΋ࢀՃՄೳ • ܾউ্ҐʹೖΔͱ৆ۚ΋͋Δ ※ʮISUCONʯ͸ɺLINEגࣜձࣾͷ঎ඪ·ͨ͸ొ࿥঎ඪͰ͢ɻ http://isucon.net/

Slide 7

Slide 7 text

༧બग़৔·Ͱͷ४උ

Slide 8

Slide 8 text

༧બग़৔·Ͱͷ४උ • νʔϜ࡞Γͱ໾ׂ෼୲ܾΊ • nginx΍MySQLͷϩάղੳɾνϡʔχϯά • Rustͷษڧ • WebΞϓϦέʔγϣϯͱRust

Slide 9

Slide 9 text

༧બग़৔·Ͱͷ४උ • νʔϜ࡞Γͱ໾ׂ෼୲ܾΊˡࠓճ͸࿩͞ͳ͍ • nginx΍MySQLͷϩάղੳɾνϡʔχϯάˡࠓճ͸࿩͞ͳ͍ • RustͷษڧˡTour of Rust΍Γ·ͨ͠ (https://tourofrust.com/) • WebΞϓϦέʔγϣϯͱRust

Slide 10

Slide 10 text

WebΞϓϦέʔγϣϯͱRust

Slide 11

Slide 11 text

ISUCONͷࢀߟ࣮૷ʹ͍ͭͯ • ISUCONͰ͸Կछྨ͔ͷݴޠͰͷ࣮૷͕ఏڙ͞Ε্ͨͰڝٕ͕ߦΘΕ Δ • ౰ॳզʑ͸Rustͷࢀߟ࣮૷͕ͳ͍έʔε΋ߟྀ͠ɺ1͔ΒRustͰΞϓ ϦέʔγϣϯΛ࣮૷͢Δ͜ͱ΋ݕ౼ʹೖΕ͍ͯͨ • RustͰWebΞϓϦέʔγϣϯΛ࡞ΔͷʹඞཁͳϥΠϒϥϦͳͲΛ ஌͓ͬͯ͘ඞཁ͕͋Δ

Slide 12

Slide 12 text

WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠ • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠

Slide 13

Slide 13 text

WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠ • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠

Slide 14

Slide 14 text

actix-web use actix_web::{web, App, HttpResponse, HttpServer}; // ϢʔβҰཡΛදࣔ͢Δ async fn show_users() -> HttpResponse { HttpResponse::Ok().body(“Show users…”) } // ΤϯυϙΠϯτ͝ͱʹͲͷؔ਺ΛݺͿ͔ͳͲ͜͜Ͱهड़͢Δ #[actix_rt::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new().service( web::scope(“/users”) .route(“/show”, web::get().to(show_users)), ) }) .bind(“127.0.0.1:8088”)? .run() .await } RustͷWeb framework https://github.com/actix/ actix-web

Slide 15

Slide 15 text

actix-web READMEʹػೳҰཡ΍ Example͕͍͔ͭ͘ࡌͬͯ ͍Δ

Slide 16

Slide 16 text

WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠ • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠

Slide 17

Slide 17 text

Tera

My Programming Languages

    {% for language in programming_languages %} {% if language.name == “rust” %}
  • Rust Rules!!!!
  • {% else %}
  • {{ language.name }}
  • {% endif %} {% endfor %}
… Template engine inspired by Jinja2 https://github.com/Keats/ tera

Slide 18

Slide 18 text

WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠ • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠

Slide 19

Slide 19 text

reqwest let client = reqwest::Client::new(); // form()ͷଞʹbody()ͱ͔json()ͱ͔΋࢖͑Δ let res = client.post(“https://…”) .form(&params) .send() .await?; RustͷHTTPΫϥΠΞϯτ https://github.com/ seanmonstar/reqwest

Slide 20

Slide 20 text

WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠ • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠

Slide 21

Slide 21 text

serde use actix_web::{HttpResponse, Responder}; use serve::{Serialize, Deserialize}; #[derive(Serialize, Deserialize)] struct User { id: i32, username: String, name: String } async fn user_detail_json() -> imp Responder { let user = User { id: 12345, username: “stefafafan”.to_string(), name: “ͯ͢ʹΌΜ”.to_string(), } // ͜Ε͸actix_webͷؔ਺͚ͩͲJSONͱͯ͠ฦͤͯΔ HttpResponse::Ok().json(user) } Rust޲͚ʹσʔλΛSerialize/ Deserialize͢ΔϑϨʔϜϫʔΫ https://github.com/serde-rs/ serde

Slide 22

Slide 22 text

WebΞϓϦέʔγϣϯΛRustͰهड़͢ΔͷʹԿ ͕ඞཁͳͷ͔ • WebϑϨʔϜϫʔΫ͕΄͍͠ • ςϯϓϨʔτΤϯδϯ͕΄͍͠ • APIୟ͍ͨΓ͢ΔͳΒHTTPΫϥΠΞϯτ͕΄͍͠ • JSONฦ͢ͳΒSerialize/DeserializeͰ͖ΔΑ͏ʹ͍ͨ͠ • MySQL΍PostgreSQLΫϥΠΞϯτ͕΄͍͠

Slide 23

Slide 23 text

SQLx use sqlx::{MySqlPool}; async fn main() -> std::io::Result<()> { let pool = MySqlPool::connect(&DATABASE_URL).await; let row = sqlx::query!(“SELECT …”) .fetch_all(&pool) .await .unwrap(); … } Rust޲͚SQLϥΠϒϥϦ https://github.com/ launchbadge/sqlx

Slide 24

Slide 24 text

ISUCON༧બ

Slide 25

Slide 25 text

ISUCON10 ༧બ໰୊ͷղઆͱߨධ http://isucon.net/archives/55025156.html

Slide 26

Slide 26 text

ISUCON10 ༧બ֓ཁ • Πεʹ߹͏෺݅Λݕࡧ͢ΔαΠτ(ISUUMO) ҎԼͷΑ͏ͳը໘͕͋Δ • Πεͷݕࡧ • ෺݅ͷݕࡧ • Πεɾ෺݅ͷৄࡉը໘ • ϚοϓΛϚ΢εͰͳͧͬͯൣғ಺ʹؚ·ΕΔ෺݅Λදࣔ

Slide 27

Slide 27 text

ISUCON10 ༧બ֓ཁ • MySQLͱnginx͕࢖ΘΕ͍ͯΔ • Rust࣮૷΋͋Δ • αʔό͕3୆࢖͑Δ

Slide 28

Slide 28 text

ISUCON༧બTrust RustνʔϜͰ΍ͬͨ͜ͱ • αʔόʹ͋ΔRustͷ࣮૷ΛGit؅ཧԼʹஔ͘ • alp΍pt-query-digestΛ࢖ͬͯɺ஗͍ΤϯυϙΠϯτ΍஗͍ΫΤϦΛಛఆ • MySQLͷINDEXΛ଍͢ͳͲ • طଘͷRustίʔυΛோΊ͖ͯ͋Β͔ʹඇޮ཰ͳ͜ͱ΍ͬͯͦ͏ͳ෦෼Έ ͚ͭͯվળ͠Α͏ͱͨ͠ • 3୆αʔό͕͋ΔͷͰΞϓϦαʔό1୆ + DBαʔό2୆ߏ੒ʹ

Slide 29

Slide 29 text

ISUCON༧બTrust RustνʔϜͰ΍ͬͨ͜ͱ • αʔόʹ͋ΔRustͷ࣮૷ΛGit؅ཧԼʹஔ͘ • alp΍pt-query-digestΛ࢖ͬͯɺ஗͍ΤϯυϙΠϯτ΍஗͍ΫΤϦΛಛఆ • MySQLͷINDEXΛ଍͢ͳͲ • طଘͷRustίʔυΛோΊ͖ͯ͋Β͔ʹඇޮ཰ͳ͜ͱ΍ͬͯͦ͏ͳ෦෼Έ ͚ͭͯվળ͠Α͏ͱͨ͠ • 3୆αʔό͕͋ΔͷͰΞϓϦαʔό1୆ + DBαʔό2୆ߏ੒ʹ

Slide 30

Slide 30 text

ISUCON༧બ վળྫᶃ ɾΠεͷॎԣ෯͕෺݅ͷυΞʹೖΔ ͔Ͳ͏͔ͷΫΤϦΛ૊Έཱ͍ͯͯΔ Օॴͷվળ https://github.com/dekokun/ ISUCON-2020-qualify/pull/12

Slide 31

Slide 31 text

ISUCON༧બ վળྫᶄ ɾchairςʔϒϧͱestateςʔϒϧͰ ผʑͷMySQLPoolΛࢀর͢ΔΑ͏ ʹͯ͠ɺDBαʔόΛΘ͚ΒΕΔঢ় ଶʹ https://github.com/dekokun/ ISUCON-2020-qualify/pull/15

Slide 32

Slide 32 text

༧બͷ݁Ռ • զʑ͸༧બഊୀͨ͠ • ISUCON10 ΦϯϥΠϯ༧બͷར༻ݴޠൺ཰ http://isucon.net/ archives/55008738.html • Goݴޠ͕1൪ਓؾͰ6ׂۙ͘ͷνʔϜ͕࢖͍ͬͯͨ • Rust͸8૊Ͱ͏ͪ1૊͕ຊઓग़৔ (͍͢͝)

Slide 33

Slide 33 text

ֶͼɾײ૝ • Rust͸ίϯύΠϧ࣌ʹஸೡʹ৭ʑͱౖͬͯ͘ΕΔͷͰ҆৺ײ͕͋Δ • ૉૣ͘վળ͍ͯ͘͠ඞཁͷ͋ΔISUCONʹؔͯ͠͸ίϯύΠϧ࣌ؒ΍ίʔ υॻ࣌͘ͷखܰ͞ͱ͍͏໘Ͱ͸Go͸࢖͍΍͍͢ͷͩΖ͏ͳͱ͸ࢥͬͨ • ࠓճͷ͏ͪͷνʔϜͷઓ͍ํ͸ΞϓϦͷίʔυΛΰϦΰϦॻ͘ͱ͍ ͏ײ͡Ͱ΋ͳ͔ͬͨͱ͍͏ͷ΋͋Γ·͢ • RustΛ৮Δػձʹ͸ͳͬͨ͠ɺ༧બ͸མͪͨ΋ͷͷ΍ͬͯΑ͔ͬͨ