Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

WELCOME TO KANSAS

Slide 3

Slide 3 text

WIZARD OF orz

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

KANSAS MISSOURI

Slide 7

Slide 7 text

KANSAS MISSOURI KANSAS CITY

Slide 8

Slide 8 text

KANSAS MISSOURI KANSAS CITY

Slide 9

Slide 9 text

@chancancode GODFREY CHAN

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

! " # $ %

Slide 14

Slide 14 text

RAILS 5

Slide 15

Slide 15 text

PHP

Slide 16

Slide 16 text

JavaScript

Slide 17

Slide 17 text

gem install javascript

Slide 18

Slide 18 text

require "javascript" puts "This is totally Ruby" javascript { console.log("ZOMG JavaScript"); } puts "This is Ruby again"

Slide 19

Slide 19 text

require "javascript" puts "This is totally Ruby" javascript { console.log("ZOMG JavaScript"); } puts "This is Ruby again"

Slide 20

Slide 20 text

require "javascript" puts "This is totally Ruby" javascript { console.log("ZOMG JavaScript"); } puts "This is Ruby again"

Slide 21

Slide 21 text

require "javascript" javascript { var plusOne = function(x) { console.log(x + 1); }; plusOne(3); # => 4 }

Slide 22

Slide 22 text

Everyone's favorite JavaScript feature?

Slide 23

Slide 23 text

Callbacks

Slide 24

Slide 24 text

Everyone's favorite Rails feature?

Slide 25

Slide 25 text

Callbacks

Slide 26

Slide 26 text

require "javascript" class Post < ActiveRecord::Base before_create &javascript { function() { this.slug = this.title.parameterize(); } } end

Slide 27

Slide 27 text

require "javascript" class Post < ActiveRecord::Base before_create &javascript { function() { this.slug = this.title.parameterize(); } } end

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Subscribe Today! http://bit.ly/rails-weekly

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

SIGN UP TODAY! https://skylight.io

Slide 33

Slide 33 text

SIGN UP TODAY! https://skylight.io/r/r1Z8ogwwN05Z

Slide 34

Slide 34 text

RUBY

Slide 35

Slide 35 text

C

Slide 36

Slide 36 text

Best of both worlds* NATIVE EXTENSIONS

Slide 37

Slide 37 text

gem install json

Slide 38

Slide 38 text

gem install json JSON::Pure Pure Ruby

Slide 39

Slide 39 text

gem install json JSON::Ext Native Extension JSON::Pure Pure Ruby

Slide 40

Slide 40 text

Best of both worlds* NATIVE EXTENSIONS

Slide 41

Slide 41 text

Best of both worlds* NATIVE EXTENSIONS

Slide 42

Slide 42 text

Best of both worlds* NATIVE EXTENSIONS for the end-user

Slide 43

Slide 43 text

"Here’s a general value for Rails development: We will jump through hoops on the implementation to make the user-facing API nicer." David Heinemeier Hansson

Slide 44

Slide 44 text

"Here’s a general value for Rails development: We will jump through hoops on the implementation to make the user-facing API nicer." David Heinemeier Hansson

Slide 45

Slide 45 text

"Here’s a general value for Rails development: We will jump through hoops on the implementation to make the user-facing API nicer." David Heinemeier Hansson developer experience

Slide 46

Slide 46 text

@SamSaffron

Slide 47

Slide 47 text

String#blank?

Slide 48

Slide 48 text

class String def blank? /\A[[:space:]]*\z/ === self end end

Slide 49

Slide 49 text

! SamSaffron/fast_blank

Slide 50

Slide 50 text

UP TO 20X FASTER*

Slide 51

Slide 51 text

& ME

Slide 52

Slide 52 text

& ME knows just enough C to be dangerous

Slide 53

Slide 53 text

& ME knows just enough C to be dangerous ptr

Slide 54

Slide 54 text

& ME knows just enough C to be dangerous *ptr

Slide 55

Slide 55 text

& ME knows just enough C to be dangerous **********ptr

Slide 56

Slide 56 text

& ME knows just enough C to be dangerous &&&&&&&&&&&&&&&&&&&&ptr

Slide 57

Slide 57 text

' SEGV

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

FEATHERWEIGHT AGENT

Slide 60

Slide 60 text

RUST

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

HACK WITHOUT FEAR

Slide 63

Slide 63 text

This is Rust. Rust has a compiler. Rust's compiler finds errors at compile- time. Rust's compiler guarantees the compiled program cannot crash at runtime. Be like Rust.

Slide 64

Slide 64 text

without garbage collection MEMORY SAFETY

Slide 65

Slide 65 text

without data races CONCURRENCY

Slide 66

Slide 66 text

ZERO-COST ABSTRACTIONS

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

BACK TO FAST_BLANK

Slide 69

Slide 69 text

FAST_BLANK IN C

Slide 70

Slide 70 text

static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: /* found */ break; default: return Qfalse; } s += n; } return Qtrue; }

Slide 71

Slide 71 text

static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } static VALUE rb_str_blank(VALUE str) { }

Slide 72

Slide 72 text

static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } static VALUE rb_str_blank(VALUE str) { // …boilerplate… }

Slide 73

Slide 73 text

static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } static VALUE rb_str_blank(VALUE str) { // …boilerplate… if (!s || RSTRING_LEN(str) == 0) return Qtrue; }

Slide 74

Slide 74 text

static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } static VALUE rb_str_blank(VALUE str) { // …boilerplate… if (!s || RSTRING_LEN(str) == 0) return Qtrue; while(s < RSTRING_END(str)) { s++; } }

Slide 75

Slide 75 text

static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } static VALUE rb_str_blank(VALUE str) { // …boilerplate… if (!s || RSTRING_LEN(str) == 0) return Qtrue; while(s < RSTRING_END(str)) { // …boilerplate… s++; } }

Slide 76

Slide 76 text

static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } static VALUE rb_str_blank(VALUE str) { // …boilerplate… if (!s || RSTRING_LEN(str) == 0) return Qtrue; while(s < RSTRING_END(str)) { // …boilerplate… switch (s) { } s++; } }

Slide 77

Slide 77 text

static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } static VALUE rb_str_blank(VALUE str) { // …boilerplate… if (!s || RSTRING_LEN(str) == 0) return Qtrue; while(s < RSTRING_END(str)) { // …boilerplate… switch (cc) { case 9: case 0xa: … case 0x205f: case 0x3000: // whitespace break; } s++; } }

Slide 78

Slide 78 text

static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } static VALUE rb_str_blank(VALUE str) { // …boilerplate… if (!s || RSTRING_LEN(str) == 0) return Qtrue; while(s < RSTRING_END(str)) { // …boilerplate… switch (cc) { case 9: case 0xa: … case 0x205f: case 0x3000: // whitespace break; default: return Qfalse; } s++; } }

Slide 79

Slide 79 text

static VALUE rb_str_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: /* found */ break; default: return Qfalse; } s += n; } return Qtrue; } static VALUE rb_str_blank(VALUE str) { // …boilerplate… if (!s || RSTRING_LEN(str) == 0) return Qtrue; while(s < RSTRING_END(str)) { // …boilerplate… switch (cc) { case 9: case 0xa: … case 0x205f: case 0x3000: // whitespace break; default: return Qfalse; } s++; } return Qtrue; }

Slide 80

Slide 80 text

FAST_BLANK IN RUST

Slide 81

Slide 81 text

pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) }

Slide 82

Slide 82 text

pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) }

Slide 83

Slide 83 text

pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) }

Slide 84

Slide 84 text

pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) }

Slide 85

Slide 85 text

pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) }

Slide 86

Slide 86 text

pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) }

Slide 87

Slide 87 text

pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) }

Slide 88

Slide 88 text

pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) }

Slide 89

Slide 89 text

=========== Test String Length: 6 =========== Rust 11.043M (± 3.5%) i/s - 54.744M C 10.583M (± 8.5%) i/s - 52.464M Ruby 964.469k (±27.6%) i/s - 4.390M

Slide 90

Slide 90 text

=========== Test String Length: 6 =========== Rust 11.043M (± 3.5%) i/s - 54.744M C 10.583M (± 8.5%) i/s - 52.464M Ruby 964.469k (±27.6%) i/s - 4.390M

Slide 91

Slide 91 text

=========== Test String Length: 6 =========== Rust 11.043M (± 3.5%) i/s - 54.744M C 10.583M (± 8.5%) i/s - 52.464M Ruby 964.469k (±27.6%) i/s - 4.390M

Slide 92

Slide 92 text

pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) }

Slide 93

Slide 93 text

pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().chars().all(|c| c.is_whitespace()) } *

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

RUST C // Rust Code extern crate libc; use std::{ptr, slice}; use std::marker::PhantomData; #[repr(C)] #[allow(raw_pointer_derive)] #[derive(Copy, Clone)] pub struct Buf<'a> { ptr: *mut u8, len: usize, marker: PhantomData<&'a ()>, } impl<'a> Buf<'a> { pub fn as_slice(self) -> &'a str { unsafe { let s = slice::from_raw_parts(self.ptr as *const u8, self.len); std::str::from_utf8_unchecked(s) } } } #[no_mangle] pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().unwrap().chars().all(|c| c.is_whitespace()) } // C Code #include #include typedef struct { void* data; size_t len; } fast_blank_buf_t; static inline fast_blank_buf_t STR2BUF(VALUE str) { return (fast_blank_buf_t) { .data = RSTRING_PTR(str), .len = RSTRING_LEN(str), }; } int fast_blank(fast_blank_buf_t); static VALUE rb_string_blank_p(VALUE str) { return fast_blank(STR2BUF(str)) ? Qtrue : Qfalse; } void Init_fast_blank() { rb_define_method(rb_cString, "blank?", rb_string_blank_p, 0); } #include #include #include #include #define STR_ENC_GET(str) rb_enc_from_index(ENCODING_GET(str)) static VALUE rb_string_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: break; default: return Qfalse; } s += n; } return Qtrue; } void Init_fast_blank() { rb_define_method(rb_cString, "blank?", rb_string_blank, 0); }

Slide 96

Slide 96 text

RUST C // Rust Code extern crate libc; use std::{ptr, slice}; use std::marker::PhantomData; #[repr(C)] #[allow(raw_pointer_derive)] #[derive(Copy, Clone)] pub struct Buf<'a> { ptr: *mut u8, len: usize, marker: PhantomData<&'a ()>, } impl<'a> Buf<'a> { pub fn as_slice(self) -> &'a str { unsafe { let s = slice::from_raw_parts(self.ptr as *const u8, self.len); std::str::from_utf8_unchecked(s) } } } #[no_mangle] pub extern "C" fn fast_blank(buf: Buf) -> bool { buf.as_slice().unwrap().chars().all(|c| c.is_whitespace()) } // C Code #include #include typedef struct { void* data; size_t len; } fast_blank_buf_t; static inline fast_blank_buf_t STR2BUF(VALUE str) { return (fast_blank_buf_t) { .data = RSTRING_PTR(str), .len = RSTRING_LEN(str), }; } int fast_blank(fast_blank_buf_t); static VALUE rb_string_blank_p(VALUE str) { return fast_blank(STR2BUF(str)) ? Qtrue : Qfalse; } void Init_fast_blank() { rb_define_method(rb_cString, "blank?", rb_string_blank_p, 0); } #include #include #include #include #define STR_ENC_GET(str) rb_enc_from_index(ENCODING_GET(str)) static VALUE rb_string_blank(VALUE str) { rb_encoding *enc; char *s, *e; enc = STR_ENC_GET(str); s = RSTRING_PTR(str); if (!s || RSTRING_LEN(str) == 0) return Qtrue; e = RSTRING_END(str); while (s < e) { int n; unsigned int cc = rb_enc_codepoint_len(s, e, &n, enc); switch (cc) { case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0x20: case 0x85: case 0xa0: case 0x1680: case 0x2000: case 0x2001: case 0x2002: case 0x2003: case 0x2004: case 0x2005: case 0x2006: case 0x2007: case 0x2008: case 0x2009: case 0x200a: case 0x2028: case 0x2029: case 0x202f: case 0x205f: case 0x3000: break; default: return Qfalse; } s += n; } return Qtrue; } void Init_fast_blank() { rb_define_method(rb_cString, "blank?", rb_string_blank, 0); }

Slide 97

Slide 97 text

HELIX

Slide 98

Slide 98 text

#[macro_use] extern crate libcruby; declare_types! { reopen class RubyString { def is_blank(self) -> bool { self.chars().all(|c| c.is_whitespace()) } } }

Slide 99

Slide 99 text

#[macro_use] extern crate libcruby; declare_types! { reopen class RubyString { def is_blank(self) -> bool { self.chars().all(|c| c.is_whitespace()) } } }

Slide 100

Slide 100 text

#[macro_use] extern crate libcruby; declare_types! { reopen class RubyString { def is_blank(self) -> bool { self.chars().all(|c| c.is_whitespace()) } } }

Slide 101

Slide 101 text

#[macro_use] extern crate libcruby; declare_types! { reopen class RubyString { def is_blank(self) -> bool { self.chars().all(|c| c.is_whitespace()) } } }

Slide 102

Slide 102 text

#[macro_use] extern crate libcruby; declare_types! { reopen class RubyString { def is_blank(self) -> bool { self.chars().all(|c| c.is_whitespace()) } } }

Slide 103

Slide 103 text

TURBO RAILS IN RUST?

Slide 104

Slide 104 text

WHAT ABOUT MY APP?

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

MEAL-MATCHING ALGORITHM

Slide 107

Slide 107 text

meal_tags = [3, 6, 18, 34, 50] preference_tags = [6, 18, 42] meal_tags.fully_contain?(preference_tags)

Slide 108

Slide 108 text

meal_tags = [3, 6, 18, 34, 50] preference_tags = [6, 18, 42] meal_tags.fully_contain?(preference_tags)

Slide 109

Slide 109 text

meal_tags = [3, 6, 18, 34, 50] preference_tags = [6, 18, 42] meal_tags.fully_contain?(preference_tags)

Slide 110

Slide 110 text

meal_tags = [3, 6, 18, 34, 50] preference_tags = [6, 18, 42] meal_tags.fully_contain?(preference_tags)

Slide 111

Slide 111 text

SET CONTAINMENT

Slide 112

Slide 112 text

KNOWN TRICKTM

Slide 113

Slide 113 text

class Array def fully_contain?(other) self & other == other end end

Slide 114

Slide 114 text

class Array def fully_contain?(other) self & other == other end end

Slide 115

Slide 115 text

class Array def fully_contain?(other) self & other == other end end

Slide 116

Slide 116 text

class Array def fully_contain?(other) self & other == other end end

Slide 117

Slide 117 text

SORTED

Slide 118

Slide 118 text

UNIQUE

Slide 119

Slide 119 text

(

Slide 120

Slide 120 text

WE CAN DO BETTER!

Slide 121

Slide 121 text

class Array def fully_contain?(needle) return true if needle.empty? return false if self.empty? needle_length = needle.length return false if needle_length > self.length needle_position = 0 needle_item = needle[needle_position] self.each do |item| if item == needle_item needle_position += 1 if needle_position >= needle_length return true else needle_item = needle[needle_position] end end end false end end

Slide 122

Slide 122 text

class Array def fully_contain?(needle) return true if needle.empty? return false if self.empty? needle_length = needle.length return false if needle_length > self.length needle_position = 0 needle_item = needle[needle_position] self.each do |item| if item == needle_item needle_position += 1 if needle_position >= needle_length return true else needle_item = needle[needle_position] end end end false end end

Slide 123

Slide 123 text

class Array def fully_contain?(needle) return true if needle.empty? return false if self.empty? needle_length = needle.length return false if needle_length > self.length needle_position = 0 needle_item = needle[needle_position] self.each do |item| if item == needle_item needle_position += 1 if needle_position >= needle_length return true else needle_item = needle[needle_position] end end end false end end

Slide 124

Slide 124 text

class Array def fully_contain?(needle) return true if needle.empty? return false if self.empty? needle_length = needle.length return false if needle_length > self.length needle_position = 0 needle_item = needle[needle_position] self.each do |item| if item == needle_item needle_position += 1 if needle_position >= needle_length return true else needle_item = needle[needle_position] end end end false end end

Slide 125

Slide 125 text

class Array def fully_contain?(needle) return true if needle.empty? return false if self.empty? needle_length = needle.length return false if needle_length > self.length needle_position = 0 needle_item = needle[needle_position] self.each do |item| if item == needle_item needle_position += 1 if needle_position >= needle_length return true else needle_item = needle[needle_position] end end end false end end

Slide 126

Slide 126 text

UP TO 7X FASTER

Slide 127

Slide 127 text

RUST?

Slide 128

Slide 128 text

#[macro_use] extern crate libcruby; declare_types! { reopen class Array { def fully_contain(self, needle: &[usize]) -> bool { if needle.is_empty() { return true } let haystack = self.as_ref(); if haystack.is_empty() { return false } let mut needle = needle.iter(); let mut needle_item = needle.next().unwrap(); for item in haystack { if item == needle_item { match needle.next() { None => return true, Some(next_item) => needle_item = next_item } } } false } } }

Slide 129

Slide 129 text

#[macro_use] extern crate libcruby; declare_types! { reopen class Array { def fully_contain(self, needle: &[usize]) -> bool { if needle.is_empty() { return true } let haystack = self.as_ref(); if haystack.is_empty() { return false } let mut needle = needle.iter(); let mut needle_item = needle.next().unwrap(); for item in haystack { if item == needle_item { match needle.next() { None => return true, Some(next_item) => needle_item = next_item } } } false } } } \

Slide 130

Slide 130 text

#[macro_use] extern crate libcruby; declare_types! { reopen class Array { def fully_contain(self, needle: &[usize]) -> bool { if needle.is_empty() { return true } let haystack = self.as_ref(); if haystack.is_empty() { return false } let mut needle = needle.iter(); let mut needle_item = needle.next().unwrap(); for item in haystack { if item == needle_item { match needle.next() { None => return true, Some(next_item) => needle_item = next_item } } } false } } }

Slide 131

Slide 131 text

#[macro_use] extern crate libcruby; declare_types! { reopen class Array { def fully_contain(self, needle: &[usize]) -> bool { if needle.is_empty() { return true } let haystack = self.as_ref(); if haystack.is_empty() { return false } let mut needle = needle.iter(); let mut needle_item = needle.next().unwrap(); for item in haystack { if item == needle_item { match needle.next() { None => return true, Some(next_item) => needle_item = next_item } } } false } } }

Slide 132

Slide 132 text

#[macro_use] extern crate libcruby; declare_types! { reopen class Array { def fully_contain(self, needle: &[usize]) -> bool { if needle.is_empty() { return true } let haystack = self.as_ref(); if haystack.is_empty() { return false } let mut needle = needle.iter(); let mut needle_item = needle.next().unwrap(); for item in haystack { if item == needle_item { match needle.next() { None => return true, Some(next_item) => needle_item = next_item } } } false } } }

Slide 133

Slide 133 text

#[macro_use] extern crate libcruby; declare_types! { reopen class Array { def fully_contain(self, needle: &[usize]) -> bool { if needle.is_empty() { return true } let haystack = self.as_ref(); if haystack.is_empty() { return false } let mut needle = needle.iter(); let mut needle_item = needle.next().unwrap(); for item in haystack { if item == needle_item { match needle.next() { None => return true, Some(next_item) => needle_item = next_item } } } false } } }

Slide 134

Slide 134 text

#[macro_use] extern crate libcruby; declare_types! { reopen class Array { def fully_contain(self, needle: &[usize]) -> bool { if needle.is_empty() { return true } let haystack = self.as_ref(); if haystack.is_empty() { return false } let mut needle = needle.iter(); let mut needle_item = needle.next().unwrap(); for item in haystack { if item == needle_item { match needle.next() { None => return true, Some(next_item) => needle_item = next_item } } } false } } }

Slide 135

Slide 135 text

#[macro_use] extern crate libcruby; declare_types! { reopen class Array { def fully_contain(self, needle: &[usize]) -> bool { if needle.is_empty() { return true } let haystack = self.as_ref(); if haystack.is_empty() { return false } let mut needle = needle.iter(); let mut needle_item = needle.next().unwrap(); for item in haystack { if item == needle_item { match needle.next() { None => return true, Some(next_item) => needle_item = next_item } } } false } } }

Slide 136

Slide 136 text

UP TO 173X FASTER

Slide 137

Slide 137 text

Ruby (Naive) Ruby (Fast) Rust

Slide 138

Slide 138 text

! rustbridge/helix

Slide 139

Slide 139 text

HACK WITHOUT FEAR

Slide 140

Slide 140 text

RUBY WITHOUT FEAR

Slide 141

Slide 141 text

@chancancode GODFREY CHAN

Slide 142

Slide 142 text

#MAKERUBYGREATAGAIN