Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node学園 25限 Introduction to Rust

Akito0107
April 24, 2017

Node学園 25限 Introduction to Rust

Node学園 25限の発表資料です。 

Akito0107

April 24, 2017
Tweet

More Decks by Akito0107

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ҏ౻ ӯ (@Akito0107) • Twitter / mstdn /

    Githubେମ͜ΕͰ͢ • ϦΫϧʔτςΫϊϩδʔζͱ͍͏ձࣾͰ
 ιϑτ΢ΣΞΤϯδχΞΛ΍͍ͬͯ·͢ɻ • ීஈ͸js(΋)ॻ͍ͯ·͢
  2. Rustͷ༻్ • System Programming • Network Programming • Kernel (OS

    / Device Driver / rust.koͰggr ) • Web Assembly (LLVMͷԸܙ) • etc…
  3. RustͷԿ͕৽͍͠ͷ͔ • ϝϞϦ؅ཧ • खಈ / Smart Ptr / GCͳͲͷطଘͷ࢓૊Έͱ͸


    ҰઢΛը͢Δ • ҆શʹϝϞϦͷ؅ཧΛߦ͏
 => ॴ༗ݖͱ͍͏֓೦
  4. StackͱHeap (Cͷྫ) 1 int foo() { 2 int len =

    100; 3 int* head = (int*) malloc(sizeof(int) * len); 4 /* do some work.... */ 5 free(head); 6 return 0; 7 }
  5. StackͱHeap (Cͷྫ) 1 int foo() { 2 int len =

    100; 3 int* head = (int*) malloc(sizeof(int) * len); 4 /* do some work.... */ 5 free(head); 6 return 0; 7 } <= StackʹlenΛpush
  6. StackͱHeap (Cͷྫ) 1 int foo() { 2 int len =

    100; 3 int* head = (int*) malloc(sizeof(int) * len); 4 /* do some work.... */ 5 free(head); 6 return 0; 7 } ⊖ HeapʹྖҬΛ֬อ
  7. StackͱHeap (Cͷྫ) 1 int foo() { 2 int len =

    100; 3 int* head = (int*) malloc(sizeof(int) * len); 4 /* do some work.... */ 5 free(head); 6 return 0; 7 } <= Heap͔Β໌ࣔతʹ։์
  8. StackͱHeap (Cͷྫ) 1 int foo() { 2 int len =

    100; 3 int* head = (int*) malloc(sizeof(int) * len); 4 /* do some work.... */ 5 free(head); 6 return 0; 7 } <= Block͕ऴྃɺlen͕stack͔Βpop͞ΕΔ
  9. खಈͰͷϝϞϦ؅ཧ 1 int foo() { 2 int len = 100;

    3 int* head = (int*) malloc(sizeof(int) * len); 4 /* do some work.... */ 5 free(head); 6 return 0; 7 } ͜͜Ͱ֬อ
  10. खಈͰͷϝϞϦ؅ཧ 1 int foo() { 2 int len = 100;

    3 int* head = (int*) malloc(sizeof(int) * len); 4 /* do some work.... */ 5 free(head); 6 return 0; 7 } ͜͜Ͱ։์
  11. RustͷϝϞϦϞσϧ • RustͷDefault͸Stack΁ͷAllocate BEES OBNF WBM O ʜ  

     ϝϞϦStack 1 struct Foo { 2 pub n: u32, 3 } 4 5 impl Foo { 6 fn new() -> Foo { 7 Foo { n: 42 } 8 } 9 } 10 11 fn main() { 12 let foo = Foo::new(); 13 let bar = 12; 14 } ←Start
  12. RustͷϝϞϦϞσϧ • RustͷDefault͸Stack΁ͷAllocate BEES OBNF WBM O ʜ  

    CBS   GPP 'PP\O^ ϝϞϦStack 1 struct Foo { 2 pub n: u32, 3 } 4 5 impl Foo { 6 fn new() -> Foo { 7 Foo { n: 42 } 8 } 9 } 10 11 fn main() { 12 let foo = Foo::new(); 13 let bar = 12; 14 }
  13. RustͷϝϞϦϞσϧ • RustͷDefault͸Stack΁ͷAllocate BEES OBNF WBM O ʜ  

     Stack 1 struct Foo { 2 pub n: u32, 3 } 4 5 impl Foo { 6 fn new() -> Foo { 7 Foo { n: 42 } 8 } 9 } 10 11 fn main() { 12 let foo = Foo::new(); 13 let bar = 12; 14 }←͜͜Ͱղ์
  14. ϝϞϦϞσϧʢؔ਺ݺͼग़͠ʣ 1 fn bar() { 2 let a = 10;

    3 let b = 15; 4 println!("{}", a + b); 5 } 6 7 fn main() { 8 let foo = Foo::new(); 9 bar(); 10 } 11 BEES OBNF WBM O ʜ    Stack
  15. ϝϞϦϞσϧʢؔ਺ݺͼग़͠ʣ 1 fn bar() { 2 let a = 10;

    3 let b = 15; 4 println!("{}", a + b); 5 } 6 7 fn main() { 8 let foo = Foo::new(); 9 bar(); 10 } 11 BEES OBNF WBM O ʜ    GPP 'PP\O^ Stack ←call
  16. ϝϞϦϞσϧʢؔ਺ݺͼग़͠ʣ 1 fn bar() { 2 let a = 10;

    3 let b = 15; 4 println!("{}", a + b); 5 } 6 7 fn main() { 8 let foo = Foo::new(); 9 bar(); 10 } 11 BEES OBNF WBM O ʜ  C   B   GPP 'PP\O^ Stack ←call bar()ͷstack frame
  17. ϝϞϦϞσϧʢؔ਺ݺͼग़͠ʣ 1 fn bar() { 2 let a = 10;

    3 let b = 15; 4 println!("{}", a + b); 5 } 6 7 fn main() { 8 let foo = Foo::new(); 9 bar(); 10 } 11 BEES OBNF WBM O ʜ    GPP 'PP\O^ Stack ← end
  18. ϝϞϦϞσϧʢؔ਺ݺͼग़͠ʣ 1 fn bar() { 2 let a = 10;

    3 let b = 15; 4 println!("{}", a + b); 5 } 6 7 fn main() { 8 let foo = Foo::new(); 9 bar(); 10 } 11 BEES OBNF WBM O ʜ    Stack ← end
  19. Heap Allocation • ΋ͪΖΜHeap Allocation΋Ͱ͖Δ • std::boxed::BoxΛ࢖͏ 1 use std::boxed::Box;

    2 fn boxed_foo() -> Box<Foo> { 3 Box::new(Foo::new()) 4 } 5 fn main() { 6 let foo = boxed_foo(); 7 println!("{}", foo.n); 8 }
  20. BoxΛ࢖ͬͨͱ͖ͷϝϞϦ BEES OBNF WBM O ʜ    BEES

    OBNF WBM Stack heap 1 use std::boxed::Box; 2 fn boxed_foo() -> Box<Foo> { 3 Box::new(Foo::new()) 4 } 5 fn main() { 6 let foo = boxed_foo(); 7 println!("{}", foo.n); 8 }
  21. BoxΛ࢖ͬͨͱ͖ͷϝϞϦ BEES OBNF WBM O ʜ    GPP

    Š BEES OBNF WBM Stack heap 1 use std::boxed::Box; 2 fn boxed_foo() -> Box<Foo> { 3 Box::new(Foo::new()) 4 } 5 fn main() { 6 let foo = boxed_foo(); 7 println!("{}", foo.n); 8 } ←call
  22. BoxΛ࢖ͬͨͱ͖ͷϝϞϦ BEES OBNF WBM O ʜ    GPP

    Š BEES OBNF WBM Stack heap 1 use std::boxed::Box; 2 fn boxed_foo() -> Box<Foo> { 3 Box::new(Foo::new()) 4 } 5 fn main() { 6 let foo = boxed_foo(); 7 println!("{}", foo.n); 8 } ←call
  23. BoxΛ࢖ͬͨͱ͖ͷϝϞϦ BEES OBNF WBM O ʜ    GPP

    Š BEES OBNF WBM " 'PP\O^ Stack heap 1 use std::boxed::Box; 2 fn boxed_foo() -> Box<Foo> { 3 Box::new(Foo::new()) 4 } 5 fn main() { 6 let foo = boxed_foo(); 7 println!("{}", foo.n); 8 } ←call
  24. BoxΛ࢖ͬͨͱ͖ͷϝϞϦ BEES OBNF WBM O ʜ    GPP

    " BEES OBNF WBM " 'PP\O^ Stack heap 1 use std::boxed::Box; 2 fn boxed_foo() -> Box<Foo> { 3 Box::new(Foo::new()) 4 } 5 fn main() { 6 let foo = boxed_foo(); 7 println!("{}", foo.n); 8 } ←return
  25. BoxΛ࢖ͬͨͱ͖ͷϝϞϦ BEES OBNF WBM O ʜ    GPP

    " BEES OBNF WBM " 'PP\O^ Stack heap 1 use std::boxed::Box; 2 fn boxed_foo() -> Box<Foo> { 3 Box::new(Foo::new()) 4 } 5 fn main() { 6 let foo = boxed_foo(); 7 println!("{}", foo.n); 8 }←End
  26. BoxΛ࢖ͬͨͱ͖ͷϝϞϦ BEES OBNF WBM O ʜ    GPP

    " BEES OBNF WBM Stack heap 1 use std::boxed::Box; 2 fn boxed_foo() -> Box<Foo> { 3 Box::new(Foo::new()) 4 } 5 fn main() { 6 let foo = boxed_foo(); 7 println!("{}", foo.n); 8 }←End ࢀরઌHeapྖҬΛղ์
  27. BoxΛ࢖ͬͨͱ͖ͷϝϞϦ BEES OBNF WBM O ʜ    BEES

    OBNF WBM Stack heap 1 use std::boxed::Box; 2 fn boxed_foo() -> Box<Foo> { 3 Box::new(Foo::new()) 4 } 5 fn main() { 6 let foo = boxed_foo(); 7 println!("{}", foo.n); 8 }←End
  28. ͜ͷέʔεΛߟ͑ͯΈΑ͏ 1 fn print_n(foo2: Box<Foo>) { 2 println!("{}", foo2.n); 3

    } 4 fn main() { 5 let foo = boxed_foo(); 6 print_n(foo); 7 println!("{}", foo.n); 8 }
  29. ͜ͷέʔεΛߟ͑ͯΈΑ͏ 1 fn print_n(foo2: Box<Foo>) { 2 println!("{}", foo2.n); 3

    } 4 fn main() { 5 let foo = boxed_foo(); 6 print_n(foo); 7 println!("{}", foo.n); 8 } BEES OBNF WBM O ʜ    GPP " BEES OBNF WBM " 'PP\O^ Stack heap
  30. ͜ͷέʔεΛߟ͑ͯΈΑ͏ 1 fn print_n(foo2: Box<Foo>) { 2 println!("{}", foo2.n); 3

    } 4 fn main() { 5 let foo = boxed_foo(); 6 print_n(foo); 7 println!("{}", foo.n); 8 } BEES OBNF WBM O ʜ   GPP "  GPP " BEES OBNF WBM " 'PP\O^ Stack heap ←call
  31. ͜ͷέʔεΛߟ͑ͯΈΑ͏ 1 fn print_n(foo2: Box<Foo>) { 2 println!("{}", foo2.n); 3

    } 4 fn main() { 5 let foo = boxed_foo(); 6 print_n(foo); 7 println!("{}", foo.n); 8 } BEES OBNF WBM O ʜ   GPP "  GPP " BEES OBNF WBM Stack heap ←End foo2ࢀরઌΛղ์
  32. ͜ͷέʔεΛߟ͑ͯΈΑ͏ 1 fn print_n(foo2: Box<Foo>) { 2 println!("{}", foo2.n); 3

    } 4 fn main() { 5 let foo = boxed_foo(); 6 print_n(foo); 7 println!("{}", foo.n); 8 } BEES OBNF WBM O ʜ    GPP " BEES OBNF WBM Stack heap ← ͜͜
  33. ͜ͷέʔεΛߟ͑ͯΈΑ͏ 1 fn print_n(foo2: Box<Foo>) { 2 println!("{}", foo2.n); 3

    } 4 fn main() { 5 let foo = boxed_foo(); 6 print_n(foo); 7 println!("{}", foo.n); 8 } BEES OBNF WBM O ʜ    GPP " BEES OBNF WBM Stack heap ← !? fooࢀরઌ͕ղ์ࡁʁ
  34. ͜Ε͸ίϯύΠϧΤϥʔʹͳΔ 1 fn print_n(foo2: Box<Foo>) { 2 println!("{}", foo2.n); 3

    } 4 fn main() { 5 let foo = boxed_foo(); 6 print_n(foo); 7 println!("{}", foo.n); 8 }
  35. ͜ͷ࣌ʹfoo͔Βॴ༗ݖফࣦ 1 fn print_n(foo2: Box<Foo>) { 2 println!("{}", foo2.n); 3

    } 4 fn main() { 5 let foo = boxed_foo(); 6 print_n(foo); 7 println!("{}", foo.n); 8 } BEES OBNF WBM O ʜ   GPP "  GPP " BEES OBNF WBM " 'PP\O^ Stack heap ←call
  36. 0ͷྖҬ΁ͷΞΫηε͸ېࢭ͞ ΕΔ 1 fn print_n(foo2: Box<Foo>) { 2 println!("{}", foo2.n);

    3 } 4 fn main() { 5 let foo = boxed_foo(); 6 print_n(foo); 7 println!("{}", foo.n); 8 } BEES OBNF WBM O ʜ   GPP "  GPP " BEES OBNF WBM " 'PP\O^ Stack heap ←call
  37. ίϯύΠϥ͕ݕ஌ʂ 1 fn print_n(foo2: Box<Foo>) { 2 println!("{}", foo2.n); 3

    } 4 fn main() { 5 let foo = boxed_foo(); 6 print_n(foo); 7 println!("{}", foo.n); 8 } BEES OBNF WBM O ʜ   GPP "  GPP " BEES OBNF WBM " 'PP\O^ Stack heap ↑ҧ൓
  38. ΋͏̍͜ͷέʔε 1 struct Foo { 2 pub n: u32, 3

    } 4 impl Foo { 5 fn new() -> Foo { 6 Foo { n: 42 } 7 } 8 } 9 fn main() { 10 let foo = Foo::new(); 11 let bar = foo; 12 println!("{}", foo.n); 13 }
  39. ͜Ε΋ίϯύΠϧΤϥʔ 1 struct Foo { 2 pub n: u32, 3

    } 4 impl Foo { 5 fn new() -> Foo { 6 Foo { n: 42 } 7 } 8 } 9 fn main() { 10 let foo = Foo::new(); 11 let bar = foo; 12 println!("{}", foo.n); 13 } ←͜͜Ͱॴ༗ݖͷҠৡ͕ൃੜ
  40. ࢀরͱआ༻ • Read Onlyͷࢀর
 Λି͠෇͚Δ͜ͱ͕
 Ͱ͖Δ 1 fn print_n(foo: &Foo)

    { 2 println!("{}", foo.n); 3 } 4 fn main() { 5 let foo = Foo::new(); 6 let a = &foo; 7 let b = &foo; 8 let c = &foo; 9 print_n(a); 10 print_n(b); 11 print_n(c); 12 }
  41. ϛϡʔλϒϧࢀর • Write΋Ͱ͖Δࢀর
 # 1͔ͭ͠ି෇ΒΕͳ͍ 1 fn print_n(foo: &mut Foo)

    { 2 foo.n = 100; 3 println!("{}", foo.n); 4 } 5 fn main() { 6 let mut foo = Foo::new(); 7 let a = &mut foo; 8 // let b = &mut foo; <= ͜Ε͸ίϯύΠϧΤϥʔ 9 print_n(a); 10 }
  42. आ༻ͱण໋ • आ༻ઌͷण໋ > आ༻ݩͷण໋
 ͸ίϯύΠϧΤϥʔ 1 fn main() {

    2 let a; 3 { 4 let b = 100; 5 a = &b; 6 } 7 println!("{}", a); 8 }
  43. Proof of Concepts • tokio-minihttp + ImageMagick Ͱ
 ؆қը૾ม׵αʔόΛߏங •

    (tokio … RustͰඇಉظIOΛఏڙ͢ΔFW) • ImageMagickΛIO / ThreadͰ࣮ߦ͢Δύλʔϯͱ ผ్֬อ͞ΕͨThread PoolͰ࣮ߦ͢ΔύλʔϯͰ εϧʔϓοτΛܭଌ
  44. ݁Ռ (2core) 0 2.25 4.5 6.75 9 10 50 100

    EV (2core) EV + MT (2core) ಉ࣌઀ଓ਺ req/s
  45. ݁Ռ (4core) 0 6 12 18 24 10 50 100

    EV (4core) EV + MT (4core) ಉ࣌઀ଓ਺ req/s
  46. Future Work • httpαʔόࣗମͷ࠶࣮૷ • gzipͳͲͷ࣮૷ • Cache etc… •

    ͱΓ͋͑ͣ͋ͱ10ഒҎ্͸ૣ͘͠ͳ͚Ε͹
 ࢖͑ͳ͍