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

Ruby and Rust

Ruby and Rust

Presentation in Brazilian Portuguese gave at the RubyConf Brasil 2016.

This presentation offers Rust as a possible solution for reimplementing those small pieces of a Rails app in which Ruby performance is not good enough. This talk includes examples from a demo that shows how to call Rust from within Ruby using FFI (Foreign Function Interface).

Alex Braha Stoll

September 23, 2016
Tweet

More Decks by Alex Braha Stoll

Other Decks in Programming

Transcript

  1. AGENDA Analisar quando Ruby não é a ferramenta mais adequada

    Introduzir Rust como uma opção para esses cenários Passo a passo de como integrar Rust com Rails Algumas ideias para explorarmos / acompanharmos nos próximos anos
  2. Ruby MRI não é a ferramenta mais adequada para situações

    em que é necessário processamento pesado
  3. Por que Rust? Extremamente rápida Robusto modelo de memória (protege

    contra segfaults e condições de corrida) Abstrações de custo zero Documentação excelente e comunidade amigável
  4. Rust é ideal para embarcar em uma aplicação Rails porque:

    Interoperabilidade com C (o MRI é um programa C) Não possui garbage colector
  5. p r i m e s s r c l

    i b . r s t a r g e t d e b u g r e l e a s e C a r g o . l o c k C a r g o . t o m l
  6. [ p a c k a g e ] n

    a m e = " p r i m e s " v e r s i o n = " 0 . 1 . 0 " a u t h o r s = [ " A l e x B r a h a S t o l l < a l e x b r a h a s t o l l @ g m a i l . c o m > " ] [ l i b ] n a m e = " p r i m e s " c r a t e - t y p e = [ " d y l i b " ]
  7. p u b f n d i s c o

    v e r _ p r i m e s ( u p _ t o _ n t h _ p r i m e : u 3 2 ) - > V e c < u 3 2 > { l e t m u t p r i m e s : V e c < u 3 2 > = v e c ! [ ] ; l e t m u t p o t e n t i a l _ p r i m e = 2 ; / / . . . p r i m e s }
  8. w h i l e ( p r i m

    e s . l e n ( ) a s u 3 2 ) < u p _ t o _ n t h _ p r i m e { l e t m u t c o m p o s i t e = f a l s e ; f o r d i v i s o r i n 2 . . ( p o t e n t i a l _ p r i m e + 1 ) { i f p o t e n t i a l _ p r i m e % d i v i s o r = = 0 & & d i v i s o r < p o t e n t i a l _ p r i m e { c o m p o s i t e = t r u e ; b r e a k ; } } i f ! c o m p o s i t e { p r i m e s . p u s h ( p o t e n t i a l _ p r i m e ) ; } p o t e n t i a l _ p r i m e + = 1 ; }
  9. # [ c f g ( t e s t

    ) ] m o d t e s t s { u s e s u p e r : : * ; / / . . . # [ t e s t ] f n c o r r e c t l y _ d i s c o v e r _ p r i m e s _ u p _ t o _ 4 t h ( ) { l e t e x p e c t e d _ p r i m e s = v e c ! [ 2 , 3 , 5 , 7 ] ; a s s e r t _ e q ! ( e x p e c t e d _ p r i m e s , d i s c o v e r _ p r i m e s ( 4 ) ) ; } / / . . . }
  10. # [ n o _ m a n g l

    e ] p u b e x t e r n f n n t h _ p r i m e ( n t h : u 3 2 ) - > u 3 2 { m a t c h d i s c o v e r _ p r i m e s ( n t h ) . l a s t ( ) { S o m e ( & v a l u e ) = > v a l u e , N o n e = > 0 } }
  11. # [ n o _ m a n g l

    e ] p u b e x t e r n f n l i s t _ p r i m e s _ u p _ t o _ n t h ( n t h : u s i z e ) - > * c o n s t c _ c h a r { l e t m u t p r i m e s _ l i s t = " " . t o _ s t r i n g ( ) ; f o r p r i m e i n & d i s c o v e r _ p r i m e s ( n t h ) { p r i m e s _ l i s t . p u s h _ s t r ( & p r i m e . t o _ s t r i n g ( ) ) ; p r i m e s _ l i s t . p u s h _ s t r ( " , " ) ; } p r i m e s _ l i s t . p o p ( ) ; p r i m e s _ l i s t . p o p ( ) ; C S t r i n g : : n e w ( p r i m e s _ l i s t ) . u n w r a p ( ) . i n t o _ r a w ( ) }
  12. g e m ' f f i ' , '

    1 . 9 . 1 4 ' # R u b y F F I ( F o r e i g n F u n c t i o n I n t e r f a c e ) l i b r a r y .
  13. c l a s s P r i m e

    s C o n t r o l l e r < A p p l i c a t i o n C o n t r o l l e r d e f i n d e x i f p a r a m s [ : p r i m e s _ c o u n t ] . p r e s e n t ? i m p l = E N V . f e t c h ( ' P R I M E S _ I M P L ' , ' R u s t y P r i m e ' ) . c o n s t a n t i z e @ p r i m e s = i m p l . s e n d ( : l i s t _ p r i m e s , p a r a m s [ : p r i m e s _ c o u n t ] . t o _ i ) e n d e n d e n d
  14. m o d u l e R u s t

    y P r i m e e x t e n d F F I : : L i b r a r y f f i _ l i b ' l i b / c a r g o / p r i m e s / t a r g e t / r e l e a s e / l i b p r i m e s . d y l i b ' a t t a c h _ f u n c t i o n : n t h _ p r i m e , [ : i n t ] , : i n t a t t a c h _ f u n c t i o n : l i s t _ p r i m e s _ u p _ t o _ n t h , [ : i n t ] , : s t r i n g a t t a c h _ f u n c t i o n : d e a l l o c a t e _ c _ s t r , [ : p o i n t e r ] , : v o i d # . . . e n d
  15. m o d u l e R u s t

    y P r i m e # . . . d e f s e l f . l i s t _ p r i m e s ( n t h ) u n s a f e _ l i s t = l i s t _ p r i m e s _ u p _ t o _ n t h ( n t h ) s a f e _ l i s t = u n s a f e _ l i s t . c l o n e d e a l l o c a t e _ c _ s t r ( F F I : : M e m o r y P o i n t e r . f r o m _ s t r i n g ( u n s a f e _ l i s t ) ) s a f e _ l i s t e n d e n d
  16. Ruby é uma linguagem fantástica, mas não é a melhor

    ferramenta para todo problema Rust pode ajudar a sua equipe a melhorar pontos críticos de sua app Rails Fiquemos atentos em como Rust e Ruby vão interagir nos próximos anos
  17. FONTES / PROJETOS INTERESSANTES Introducing Helix Steve Klabnik - Using

    Rust with Ruby Discussão sobre projetos usando Ruby + Rust Rust - memory safety without garbage collector