Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Ruby Racing: Challenging Ruby Methods
Search
Danielle Adams
November 25, 2016
Technology
130
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby Racing: Challenging Ruby Methods
Danielle Adams
November 25, 2016
Other Decks in Technology
See All in Technology
Claude Code本は、どのように執筆チームで書かれたか
oikon48
1
380
Optunaの最新リリースv5.0について
pfn
PRO
0
120
AI × アーキテクト 第1・2章|生成AIの課題とアーキテクトの素養
daidaidai
0
170
QA4AI 2026-Oct 生成AI時代の品質保証
tokuty
1
860
会社説明資料
persolserverworks
0
9.8k
エンタメ企業のM&Aを支えるデータ基盤 ― 統合から店舗のリアルタイム分析まで / Data Engineering Summit 2026
genda
0
170
Rust で関数型ドメインモデリング
takonda
0
140
AI時代のPRレビューにどう向き合うか?
kairim0
1
240
自作バイナリを動かす「入口」8選
puhitaku
2
1.3k
Persisting Massive Data with Rails ~Designing for 60 billion rows, and fighting the bottlenecks~ Railsで挑む大量データ永続化 〜600億件を永続化するための設計とボトルネックとの戦い〜
occhi
0
730
TinyGoでオブザーバビリティ?OpenTelemetryはTinyGoで使えるのか / Is OpenTelemetry available in TinyGo environment?
ymotongpoo
2
140
LambdaWebFunctionsを試してみた / exploring-lambda-web-functions
koukihosaka
1
330
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
304
22k
New Earth Scene 8
popppiees
4
2.7k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
990
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
430
Mind Mapping
helmedeiros
1
400
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
480
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
エンジニアに許された特別な時間の終わり
watany
109
250k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
59k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
330
Test your architecture with Archunit
thirion
2
2.4k
Transcript
Ruby Racing: Challenging Ruby Methods Danielle Adams / @adamzdanielle
None
None
Ruby Racing: Challenging Ruby Methods
outline • code execution at runtime • speeding up code
• dive into source code
outline • code execution at runtime • speeding up code
• dive into source code
static VALUE rb_ary_pop_m(int argc, VALUE *argv, VALUE ary) { VALUE
result; if (argc == 0) { return rb_ary_pop(ary); } rb_ary_modify(ary); result = ary_take_first_or_last(argc, argv, ary, ARY_TAKE_LAST); ARY_INCREASE_LEN(ary, -RARRAY_LEN(result)); return str; }
#pop
How do Ruby methods work?
code
# hello_ruby_day.rb def say_hello(name) puts "Hello, #{name}!" end say_hello("Ruby Day")
tokens
AST abstract syntax trees
bytecode
bytecode + YARV
(This is only true for CRuby.)
outline • code execution at runtime • speeding up code
• dive into source code
story time
What is Ruby Racing?
$ gem install ruby-racer
None
#flatten_race.rb require 'ruby_racer' require_relative 'flatten'
#flatten_race.rb require 'ruby_racer' require_relative 'flatten' input = [ 1, [
2, 3 ] ] proc = Proc.new { |input| flatten(input) }
#flatten_race.rb require 'ruby_racer' require_relative 'flatten' input = [ 1, [
2, 3 ] ] proc = Proc.new { |input| flatten(input) } RubyRacer.go(:flatten, input, &proc)
demo
What makes code faster?
primitive operations
primitive operations are fast
# max with each enumerable def max(array) (...) array.each do
|el| max = el if el > max (...) end
# max with while loop def max(array) (...) while i
< arr.length if array[i] > max max = array[i] (...) end
# benchmark iterations = 1000000 input = [ 1, 2,
3 ] operation = el + el user total real with each: 0.2500 0.2500 (0.251002) with while: 0.2200 0.2200 (0.226902)
array mutation
array mutation is costly
# array appending vs assignment [] << 1 [nil][0] =
1 # => [1]
# benchmark iterations = 1000000 user total real with append:
0.1000 0.1000 (0.096824) with assign: 0.0900 0.0900 (0.095280)
complex algorithms
complex algorithms are slow
outline • code execution at runtime • speeding up code
• dive into source code
static VALUE rb_ary_flatten(int argc, VALUE *argv, VALUE ary) { int
mod = 0, level = -1; VALUE result, lv; (...) result = flatten(ary, level, &mod); OBJ_INFECT(result, ary); return result; } # ruby/array.c:4631
static VALUE rb_ary_flatten(int argc, VALUE *argv, VALUE ary) { int
mod = 0, level = -1; VALUE result, lv; (...) result = flatten(ary, level, &mod); OBJ_INFECT(result, ary); return result; } # ruby/array.c:4641
static VALUE flatten(VALUE ary, int level, int *modified) { long
i = 0; VALUE stack, result, tmp, elt; (...) stack = ary_new(0, ARY_DEFAULT_SIZE); result = ary_new(0, RARRAY_LEN(ary)); # ruby/array.c:4508
static VALUE flatten(VALUE ary, int level, int *modified) { long
i = 0; VALUE stack, result, tmp, elt; (...) stack = ary_new(0, ARY_DEFAULT_SIZE); result = ary_new(0, RARRAY_LEN(ary)); # ruby/array.c:4516
static VALUE flatten(VALUE ary, int level, int *modified) { long
i = 0; VALUE stack, result, tmp, elt; (...) stack = ary_new(0, ARY_DEFAULT_SIZE); result = ary_new(0, RARRAY_LEN(ary)); # ruby/array.c:4517
static VALUE flatten(VALUE ary, int level, int *modified) { (...)
while (i < RARRAY_LEN(ary)) { if (level >= 0 && RARRAY_LEN(stack) / 2 >= level) { rb_ary_push(result, elt); continue; (...) else { rb_ary_push(stack, ary); # ruby/array.c:4508
static VALUE flatten(VALUE ary, int level, int *modified) { (...)
while (i < RARRAY_LEN(ary)) { if (level >= 0 && RARRAY_LEN(stack) / 2 >= level) { rb_ary_push(result, elt); continue; (...) else { rb_ary_push(stack, ary); # ruby/array.c:4544
static VALUE flatten(VALUE ary, int level, int *modified) { (...)
while (i < RARRAY_LEN(ary)) { if (level >= 0 && RARRAY_LEN(stack) / 2 >= level) { rb_ary_push(result, elt); continue; (...) else { rb_ary_push(stack, ary); # ruby/array.c:4525
static VALUE flatten(VALUE ary, int level, int *modified) { (...)
while (i < RARRAY_LEN(ary)) { if (level >= 0 && RARRAY_LEN(stack) / 2 >= level) { rb_ary_push(result, elt); continue; (...) else { rb_ary_push(stack, ary); # ruby/array.c:4526
static VALUE rb_ary_uniq(VALUE ary) { VALUE hash, uniq; (...) if
(RARRAY_LEN(ary) <= 1) return rb_ary_dup(ary); (...) else { hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } # ruby/array.c:4382
static VALUE rb_ary_uniq(VALUE ary) { VALUE hash, uniq; (...) if
(RARRAY_LEN(ary) <= 1) return rb_ary_dup(ary); (...) else { hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } # ruby/array.c:4387
static VALUE rb_ary_uniq(VALUE ary) { VALUE hash, uniq; (...) if
(RARRAY_LEN(ary) <= 1) return rb_ary_dup(ary); (...) else { hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } # ruby/array.c:4388
static VALUE rb_ary_uniq(VALUE ary) { VALUE hash, uniq; (...) if
(RARRAY_LEN(ary) <= 1) return rb_ary_dup(ary); (...) else { hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } # ruby/array.c:4394
static VALUE rb_ary_uniq(VALUE ary) { VALUE hash, uniq; (...) if
(RARRAY_LEN(ary) <= 1) return rb_ary_dup(ary); (...) else { hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } # ruby/array.c:4395
VALUE rb_ary_sort(VALUE ary) { ary = rb_ary_dup(ary); rb_ary_sort_bang(ary); return ary;
} # ruby/array.c:2527
VALUE rb_ary_sort(VALUE ary) { ary = rb_ary_dup(ary); rb_ary_sort_bang(ary); return ary;
} # ruby/array.c:2530
VALUE rb_ary_sort(VALUE ary) { ary = rb_ary_dup(ary); rb_ary_sort_bang(ary); return ary;
} # ruby/array.c:2531
There are a few factors that will affect performance if
we are willing to sacrifice other methodologies.
How do we define code quality?
What is Ruby best at?
None
I didn't work hard to make Ruby perfect for everyone,
because you feel differently from me. No language is perfect for everyone. [...] The perfect language for Guido van Rossum is probably Python. - Yukihiro "Matz" Matsumoto
(Guido van Rossum wrote Python.)
resources
PyPy pypy.org
If you want your code to run faster, you should
probably just use PyPy. - Guido van Rossum
Ruby Under the Microscope Pat Shaughnessy
Why?
Ruby is designed to make programmers happy. - Matz
@adamzdanielle code: github.com/danielleadams/ruby-racer thank you