Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Tokyo Ruby Kaigi
Search
Aaron Patterson
May 28, 2016
Technology
5
3.4k
Tokyo Ruby Kaigi
My talk from Tokyo Ruby Kaigi
Aaron Patterson
May 28, 2016
Tweet
Share
More Decks by Aaron Patterson
See All by Aaron Patterson
Speeding up Instance Variables in Ruby 3.3
tenderlove
2
340
[Feature #20425] Speeding up delegate methods
tenderlove
3
210
RailsConf 2023
tenderlove
29
970
Don't @ Me! Faster Instance Variables with Object Shapes
tenderlove
1
410
RailsConf 2022 Keynote
tenderlove
2
500
Some Assembly Required
tenderlove
1
520
HexDevs 2021
tenderlove
1
390
Compacting GC for MRI
tenderlove
60
4.5k
But At What Cost?
tenderlove
9
14k
Other Decks in Technology
See All in Technology
知っててうれしい SQL について
greendrop
0
120
Kotlin Multiplatformのポテンシャル
recruitengineers
PRO
1
140
完全自律型AIエージェントとAgentic Workflow〜ワークフロー構築という現実解
pharma_x_tech
0
300
アジャイルチームが変化し続けるための組織文化とマネジメント・アプローチ / Agile management that enables ever-changing teams
kakehashi
3
3.2k
WantedlyでのKotlin Multiplatformの導入と課題 / Kotlin Multiplatform Implementation and Challenges at Wantedly
kubode
0
240
三菱電機で社内コミュニティを立ち上げた話
kurebayashi
1
340
20240513 - 框裡框外_文學院學生如何在AI世代安身立命 @ 淡江大學
dpys
0
650
Alignment and Autonomy in Cybozu - 300人の開発組織でアラインメントと自律性を両立させるアジャイルな組織運営 / RSGT2025
ama_ch
1
2.2k
深層学習と3Dキャプチャ・3Dモデル生成(土木学会応用力学委員会 応用数理・AIセミナー)
pfn
PRO
0
450
シフトライトなテスト活動を適切に行うことで、無理な開発をせず、過剰にテストせず、顧客をビックリさせないプロダクトを作り上げているお話 #RSGT2025 / Shift Right
nihonbuson
3
2k
Evolving Architecture
rainerhahnekamp
3
250
いま現場PMのあなたが、 経営と向き合うPMになるために 必要なこと、腹をくくること
hiro93n
9
6.5k
Featured
See All Featured
KATA
mclloyd
29
14k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Unsuck your backbone
ammeep
669
57k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Site-Speed That Sticks
csswizardry
2
240
A Philosophy of Restraint
colly
203
16k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Transcript
None
͜Μʹͪʂ
ຊޠೖྗࢼݧ
Aaron Patterson @tenderlove
GitHub
ko1: ϓϩάϥϛϯάΛ͍ͯͯ͠ʮ͓͓ɺ ੌ͍ίʔυ͕ॻ͚ͨʯʮ͜Εੌ͍ͷ Λ࡞ͬͯ͠·ͬͨʯͷΑ͏ͳܦݧͳͲ
Me: ʮੌ͍ίʔυʯ ͍͍ͬͯͬͨԿͩΖ͏
None
;ͨͭͷҙຯ ڪΖ͍͠ / ා͍ ૉΒ͍͠ / ྑ͍
ೋͭͷτϐοΫ ڪΖ͍͠ / ා͍: PHP ૉΒ͍͠ / ྑ͍: Computer Vision
ൿີ
ੌ͍ίʔυ ૉΒ͍͠ / ྑ͍
ίϯϐϡʔλ Ϗδϣϯ
ΨϥΫλ͕ ͍ͬͺ͍
Magic The Gathering (MtG)ͱ
Collectible Card Game
ࢲͷ (Χʔυͷத) • Too many cards • Don’t know what
they do • Are they worth any money? • Plus many more!
ίϯϐϡʔλʔʹ߹͏ Έ͍ͨ Seems like a job for computers!
ࢲͷղܾ
ਓೳ Χϝϥ ೣ! Ruby!
High Level Process
ࣸਅΛࡱΔ ΧʔυΛൈ͚Δ ΧʔυΛ֬ೝ͢Δ σʔλΛอଘ͢Δ
Χʔυͷೝࣝ
श ͬͨ ਪଌ ϓϩϯϓτ ڭ͑Δ ίʔύε
श ͬͨ ਪଌ ϓϩϯϓτ ڭ͑Δ ίʔύ ε
ਓೳ
σʔλɹϞσϧ
Χʔυͷใ
ࣸਅ ໊લ ਓؾ ηοτ
image belongs_to(:card) card has_one(:image)
None
image belongs_to(:card) card has_many(:images)
None
image habtm(:cards) card habmt(:image)
શσʔλϞσϧ Cards Images Hashes habtm has_many
ίʔύεͷੜ
Executor Pool class ThreadExecutor def initialize size @queue = Queue.new
@size = size @pool = size.times.map { |i| Thread.new { while job = @queue.pop; job.run end } } end def execute job = Proc.new promise = Promise.new job @queue << promise promise end def shutdown @size.times { @queue << nil } @pool.each(&:join) end end
Queue up work promise = @executor.execute do @http_pool.with_connection do |conn|
uri = URI 'http://gatherer.wizards.com/Pages/Default.aspx' response = conn.request uri # save the response end end # some time later promise.value
Me Wizards
1.6GB
http://mtgjson.com/
ॻ͘લʹ άʔάϧͨ͠ํ͕͍͍
Χʔυͷೝࣝ
֮తϋογϡ libphash (gem install phasion)
11998341215223291070 12079686925145871551 Reference Scanned
ϋϛϯάڑ require 'phashion' left = Phashion.image_hash_for "a.jpg" right = Phashion.image_hash_for
"b.jpg" p Phashion.hamming_distance left, right # => 8
5622372658725034411 12079686925145871551 Reference Scanned
ϋϛϯάڑ require 'phashion' left = Phashion.image_hash_for "c.jpg" right = Phashion.image_hash_for
"b.jpg" p Phashion.hamming_distance left, right # => 28
ϋογϡΛՃ͢Δͱ ͕ࣝ૿͑Δ
ϋʔυΣΞ
None
None
None
ιϑτΣΞ
OpenCV https://github.com/ruby-opencv/ruby-opencv
ࡱͬͨࣸਅ
None
ཉ͍͠Πϝʔδ
None
Χʔυͷൈ͖ํ
؆୯ͳ8ஈ֊͚ͩ!* * ؆୯͡Όͳ͍ɺଟ
None
લॲཧ
άϨʔεέʔϧ gray = OpenCV.BGR2GRAY img
None
Τοδݕग़ canny_img = gray.canny @thresh, @thresh http://en.wikipedia.org/wiki/Canny_edge_detector
None
ྠֲΛݟ͚ͭΔ contours = [] contour_node = processed.find_contours( :mode => OpenCV::CV_RETR_TREE,
:method => OpenCV::CV_CHAIN_APPROX_SIMPLE) while contour_node unless contour_node.hole? contours << contour_node end contour_node = contour_node.h_next end
ྠֲ ݀
None
Ұ൪େ͖ͳྠֲ max = contours.max_by { |c| c.contour_area }
None
ଟ֯ܗΛݟ͚ͭΔ approx = max.approx_poly(:method => :dp, :recursive => true, :accuracy
=> 0.02 * peri) x = approx.convex_hull2.to_a clockwise_points = clockwise x.reverse
None
ಁࢹมͷߦྻ to = [ OpenCV::CvPoint2D32f.new(0, 0), OpenCV::CvPoint2D32f.new(width, 0), OpenCV::CvPoint2D32f.new(width, height),
OpenCV::CvPoint2D32f.new(0, height), ] from = polygon_points transform = OpenCV::CvMat.get_perspective_transform(from, to)
ΠϝʔδΛม͢Δ new_img = img.warp_perspective transform
None
ڥք࠲ඪΛઃఆ͢Δ new_img.set_roi OpenCV::CvRect.new(0, 0, width, height)
None
It’s Just That Easy!
ϢʔβʔΠϯλʔϑΣΠε ΧϝϥͰ ࡱͬͨ Πϝʔδ ਪଌ #1 ਪଌ #2 ਪଌ #3
None
Կ͕͋Δʁ Card.joins(:images) .where('images.type' => 'UserImage') .group('cards.name') .count
Ͳͷ͙Β͍ྑ͍ʁ Card.joins(:images) # best .where('images.type' => 'UserImage') .order('rating desc').first Card.joins(:images)
# worst .where('images.type' => 'UserImage') .order('rating asc').first
࠷ߴ:
࠷:
ϓϩδΣΫτͷ
যͷ࣌ؒ
None
ࣦഊͷ࣌
None
ֆࣅͯΔ
None
अຐೣ
None
ར ઃఆ؆୯ ඞཁϋʔυΣΞ͋·Γͳ͍ ͍҆ ΩϟϦϒϨʔτ͠ͳ͍
ܽ ෳࡶͳιϑτΣΞ ࿑ಇ ֬ೝͷ͞
ࠓޙͷ׆ಈ
ࣗಈԽ
ੌ͍ίʔυ ڪΖ͍͠ / ා͍
PHP + Ruby
Phuby (ϑʔϏΟ) https://github.com/tenderlove/phuby એ
Examples
PHPͷ ΦϒδΣΫτΛ͏
ࣈ require 'phuby' Phuby::Runtime.php do |runtime| runtime.eval "$v = strlen('PHP
INSIDE OF RUBY');" puts runtime['v'] # => 18 end
จࣈྻ require 'phuby' Phuby::Runtime.php do |runtime| runtime.eval "$name = 'Aaron';"
puts runtime['name'] # => Aaron end
ྻ require 'phuby' Phuby::Runtime.php do |runtime| runtime.eval "$cars = array('Toyota',
'Honda', 'BMW');" p runtime['cars'][0] # => Toyota p runtime['cars'][1] # => Honda p runtime['cars'][2] # => BMW end
࿈ྻ require 'phuby' Phuby::Runtime.php do |runtime| runtime.eval "$e = array('foo'
=> 'bar', 'bar' => 'baz');" p runtime['e']['foo'] # => 'bar' p runtime['e']['bar'] # => 'baz' end
Rubyͷ ΦϒδΣΫτΛ͏
Objects require 'phuby' class RubyKaigi def where puts "౦ژ" end
end Phuby::Runtime.php do |runtime| runtime['kaigi'] = RubyKaigi.new runtime.eval("$kaigi->where();") # => ౦ژ end
ͳͥʁ
͍͍͔Β
scientists were so preoccupied with whether or not they could
that they didn't stop to think if they should.
ϓϩάϥϜͷମܕ
Runtime Proxy Object Map Ruby Object Proxy PHP Object Proxy
Wrapper Access PHP Objects Ruby C PHP Object Map Ruby
Proxy PHP Runtime Object
Wrapper Access Ruby Objects Ruby C PHP Object Map Ruby
Proxy PHP Runtime Object
ϓϩδΣΫτͷ
PHPΛ ίϯύΠϧ͢Δ
GC
άϩʔόϧͷม
require 'phuby' require 'openssl' p OpenSSL::Cipher.ciphers # => ["AES-128-CBC",
"AES-128-CBC-HMAC-SHA1", ... ] Phuby::Runtime.php do |runtime| runtime.eval("$hi = 'Hello World';") end p OpenSSL::Cipher.ciphers # => []
Solution: fork require 'phuby' require 'openssl' p OpenSSL::Cipher.ciphers # =>
["AES-128-CBC", "AES-128-CBC-HMAC-SHA1", ... ] Process.waitpid fork { Phuby::Runtime.php do |runtime| runtime.eval("$hi = 'Hello World';") end } p OpenSSL::Cipher.ciphers # => ["AES-128-CBC", "AES-128-CBC-HMAC-SHA1", ... ]
Rails Support
PHP templates
None
Πϯελϯεม
None
ActiveRecord͑·͢
None
How much would you pay for this?
200ԁ?
250ԁ?
https://github.com/ tenderlove/phuby FREE!
ൿີͷτϐοΫ
ko1 said this is a tech conference, so you don’t
have to tell any jokes
So I won’t tell any jokes.
ੌ͍ίʔυ How does it work?
Homeopathic Optimizations
Homeopathy ͱʁ
رऍɻ
͗͢Δʂ
None
None
Modern advocates of homeopathy have proposed a concept of "water
memory", according to which water "remembers" the substances mixed in
ਫ هԱग़དྷ·͢
ۭന هԱ͢Δࣄ͕Ͱ͖Δʁ
YES!
σϞͷ࣌ؒ!
None
Ͳ͏ͬͯ ಈ͍ͯΔʁ
VM هԱ͢Δ
ͦΕۭന͡Όͳ ͯ͘ɺهԱͩɻ
ଞͷಛ
None
ར
ߴԽ
"No code is faster than no code"
رऍ͞ΕΕ͞ΕΔ΄Ͳ ϓϩάϥϜΑΓ͘ͳ Δ
ҡ࣋
ίʔυͳ͍͔Β
THANK YOU!!!
࣭ʁ