Making Ruby faster without C

@josefrousek

Writing an OS in Rust by Philipp Oppermann

Rust? WTH?

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.

rust-lang.org

Package management

same as in Ruby - both created by Yehuda Katz

 

but crates instead of gems

Ownership and Borrowing

Automatically free memory without manually calling free, reference counting and GC?

Ownership

fn print_vec(v: Vec<i32>) {
    println!("{:?}", v);
}

fn main() {
    let v = vec![1,2,3];
    print_vec(v);
}


# result

rustc 1.12.1 (d4f39402a 2016-10-19)
[1, 2, 3]
fn print_vec(v: Vec<i32>) {
    println!("{:?}", v);
}

fn main() {
    let v = vec![1,2,3];
    print_vec(v);
    print_vec(v);
}


# result

rustc 1.12.1 (d4f39402a 2016-10-19)
error[E0382]: use of moved value: `v`
 --> <anon>:8:15
  |
7 |     print_vec(v);
  |               - value moved here
8 |     print_vec(v);
  |
  = note: move occurs because `v` has type `std::vec::Vec<i32>`,
which does not implement the `Copy` trait

Borrowing

fn print_vec(v: &Vec<i32>) {
    println!("{:?}", v);
}

fn main() {
    let v = vec![1,2,3];
    print_vec(&v); # <---- & char
    print_vec(&v);
}


# result

rustc 1.12.1 (d4f39402a 2016-10-19)
[1, 2, 3]
[1, 2, 3]

Mutation

fn print_vec(v: &Vec<i32>) {
    println!("{:?}", v);
}

fn main() {
    let v = vec![1,2,3];
    v[0] = 0;
    print_vec(&v);
    print_vec(&v);
}


# result

rustc 1.12.1 (d4f39402a 2016-10-19)
error: cannot borrow immutable local variable `v` as mutable
 --> <anon>:7:5
  |
6 |     let v = vec![1,2,3];
  |         - use `mut v` here to make mutable
7 |     v[0] = 0;
fn print_vec(v: &Vec<i32>) {
    println!("{:?}", v);
}

fn main() {
    let mut v = vec![1,2,3]; # <----- mut keyword
    v[0] = 0;
    print_vec(&v);
    print_vec(&v);
}


# result

rustc 1.12.1 (d4f39402a 2016-10-19)
[0, 2, 3]
[0, 2, 3]

Some more mutation

fn mut_vec(mut v: Vec<i32>) -> Vec<i32> {
    v[0] = 10;
    return v;
}

fn print_vec(v: Vec<i32>) {
    println!("{:?}", v);
}

fn main() {
    let v = vec![1,2,3];
    let another_vec = mut_vec(v);
    print_vec(another_vec);
}


# result

rustc 1.12.1 (d4f39402a 2016-10-19)
[10, 2, 3]

Some numbers! - Part 1

text = """
Hi, thank you for your talk proposal. We've received a lot of high
quality talks and we had very hard time to pick the best one.
"""


Benchmark.bm(20) do |x|
  x.report("blank?") { n.times do; text.blank?; end }
  x.report("Rust.fast_blank") { n.times do; Rust.fast_blank(text); end }
end

Some numbers! - Part 2

# Note the whitespace here
text_spaces = """
                                                                                                                                x
Hi, thank you for your talk proposal. We've received a lot 
of high quality talks and we had very hard time to pick the best one.
"""


Benchmark.bm(20) do |x|
  x.report("blank?") { n.times do; text_spaces.blank?; end }
  x.report("Rust.fast_blank") { n.times do; Rust.fast_blank(text_spaces); end }
end

thank you!

twitter: @josefrousek

http: rousek.name

 

source code, slides and everything!

 

https://github.com/stlk/rust-talk

Made with Slides.com