Node学園 25限 Introduction to Rust

Node学園 25限 Introduction to Rust

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

9a3693c0c3d12f2fbae51792dd6a1344?s=128

Akito0107

April 24, 2017
Tweet

Transcript

  1. 2.

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

    Githubେମ͜ΕͰ͢ • ϦΫϧʔτςΫϊϩδʔζͱ͍͏ձࣾͰ
 ιϑτ΢ΣΞΤϯδχΞΛ΍͍ͬͯ·͢ɻ • ීஈ͸js(΋)ॻ͍ͯ·͢
  2. 4.
  3. 7.

    Rustͷ༻్ • System Programming • Network Programming • Kernel (OS

    / Device Driver / rust.koͰggr ) • Web Assembly (LLVMͷԸܙ) • etc…
  4. 9.

    RustͷԿ͕৽͍͠ͷ͔ • ϝϞϦ؅ཧ • खಈ / Smart Ptr / GCͳͲͷطଘͷ࢓૊Έͱ͸


    ҰઢΛը͢Δ • ҆શʹϝϞϦͷ؅ཧΛߦ͏
 => ॴ༗ݖͱ͍͏֓೦
  5. 13.

    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 }
  6. 14.

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

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

    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͔Β໌ࣔతʹ։์
  9. 17.

    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͞ΕΔ
  10. 19.

    खಈͰͷϝϞϦ؅ཧ 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. 20.

    खಈͰͷϝϞϦ؅ཧ 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 } ͜͜Ͱ։์
  12. 22.
  13. 25.
  14. 27.

    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
  15. 28.

    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 }
  16. 29.

    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 }←͜͜Ͱղ์
  17. 30.

    ϝϞϦϞσϧʢؔ਺ݺͼग़͠ʣ 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
  18. 31.

    ϝϞϦϞσϧʢؔ਺ݺͼग़͠ʣ 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
  19. 32.

    ϝϞϦϞσϧʢؔ਺ݺͼग़͠ʣ 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
  20. 33.

    ϝϞϦϞσϧʢؔ਺ݺͼग़͠ʣ 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
  21. 34.

    ϝϞϦϞσϧʢؔ਺ݺͼग़͠ʣ 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
  22. 35.

    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 }
  23. 36.

    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 }
  24. 37.

    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
  25. 38.

    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
  26. 39.

    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
  27. 40.

    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
  28. 41.

    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
  29. 42.

    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ྖҬΛղ์
  30. 43.

    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
  31. 44.

    ͜ͷέʔεΛߟ͑ͯΈΑ͏ 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 }
  32. 45.

    ͜ͷέʔεΛߟ͑ͯΈΑ͏ 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
  33. 46.

    ͜ͷέʔεΛߟ͑ͯΈΑ͏ 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
  34. 47.

    ͜ͷέʔεΛߟ͑ͯΈΑ͏ 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ࢀরઌΛղ์
  35. 48.

    ͜ͷέʔεΛߟ͑ͯΈΑ͏ 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 ← ͜͜
  36. 49.

    ͜ͷέʔεΛߟ͑ͯΈΑ͏ 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ࢀরઌ͕ղ์ࡁʁ
  37. 50.

    ͜Ε͸ίϯύΠϧΤϥʔʹͳΔ 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 }
  38. 53.

    ͜ͷ࣌ʹ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
  39. 54.

    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
  40. 55.

    ίϯύΠϥ͕ݕ஌ʂ 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 ↑ҧ൓
  41. 56.

    ΋͏̍͜ͷέʔε 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 }
  42. 57.

    ͜Ε΋ίϯύΠϧΤϥʔ 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 } ←͜͜Ͱॴ༗ݖͷҠৡ͕ൃੜ
  43. 59.

    ࢀরͱआ༻ • 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 }
  44. 60.

    ϛϡʔλϒϧࢀর • 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 }
  45. 61.

    आ༻ͱण໋ • आ༻ઌͷण໋ > आ༻ݩͷण໋
 ͸ίϯύΠϧΤϥʔ 1 fn main() {

    2 let a; 3 { 4 let b = 100; 5 a = &b; 6 } 7 println!("{}", a); 8 }
  46. 70.

    Proof of Concepts • tokio-minihttp + ImageMagick Ͱ
 ؆қը૾ม׵αʔόΛߏங •

    (tokio … RustͰඇಉظIOΛఏڙ͢ΔFW) • ImageMagickΛIO / ThreadͰ࣮ߦ͢Δύλʔϯͱ ผ్֬อ͞ΕͨThread PoolͰ࣮ߦ͢ΔύλʔϯͰ εϧʔϓοτΛܭଌ
  47. 71.
  48. 72.

    ݁Ռ (2core) 0 2.25 4.5 6.75 9 10 50 100

    EV (2core) EV + MT (2core) ಉ࣌઀ଓ਺ req/s
  49. 73.

    ݁Ռ (4core) 0 6 12 18 24 10 50 100

    EV (4core) EV + MT (4core) ಉ࣌઀ଓ਺ req/s
  50. 74.
  51. 75.

    Future Work • httpαʔόࣗମͷ࠶࣮૷ • gzipͳͲͷ࣮૷ • Cache etc… •

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