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
Lazy Enumeration
Search
Eric Hodel
April 27, 2016
Programming
0
110
Lazy Enumeration
An introduction to lazy enumeration in ruby
Eric Hodel
April 27, 2016
Tweet
Share
More Decks by Eric Hodel
See All by Eric Hodel
Building maintainable command-line tools with MRuby
drbrain
0
550
Introduction to Rake
drbrain
0
260
Lessons in Mentorship
drbrain
1
160
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
83
Open Source Maintenance — RailsClub Moscow
drbrain
1
140
drbdump
drbrain
2
430
Other Decks in Programming
See All in Programming
Our Websites Need a Lifestyle Change, Not a Diet
ryantownsend
0
110
今インフラ技術をイチから学び直すなら
yuhta28
1
120
Ruby Parser progress report 2024
yui_knk
2
200
Some more adventure of Happy Eyeballs
coe401_
2
160
エンジニア1年目で複雑なコードの改善に取り組んだ話
mtnmr
1
260
Regular Expressions, REXML, Automata Learning
makenowjust
0
190
私のEbitengineの第一歩
qt_luigi
0
440
事業フェーズの変化に対応する 開発生産性向上のゼロイチ
masaygggg
0
120
GraphQL あるいは React における自律的なデータ取得について
quramy
9
2.6k
The Future of Frontend i18n : Intl.MessageFormat
sajikix
1
2.5k
RAGの回答精度評価用のQAデータセットを生成AIに作らせた話
kurahara
0
230
Playwrightから始めるVisual Regression Testingのススメ by とっと
totto2727
2
1.9k
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
228
18k
The Straight Up "How To Draw Better" Workshop
denniskardys
230
130k
Music & Morning Musume
bryan
46
6k
Scaling GitHub
holman
458
140k
Infographics Made Easy
chrislema
239
18k
Thoughts on Productivity
jonyablonski
66
4.2k
Building an army of robots
kneath
302
42k
Creatively Recalculating Your Daily Design Routine
revolveconf
215
12k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
103
48k
Embracing the Ebb and Flow
colly
83
4.4k
A Tale of Four Properties
chriscoyier
155
22k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
326
21k
Transcript
Lazy Enumera,on Eric Hodel —
[email protected]
Loop values = [1, 2, 3, 4] doubles = []
index = 0 while index < values.length do doubles << values[index] * 2 index += 1 end
Enumera,ng Where am I? values[index] Am I done? index <
values.length What’s next? index += 1
Enumerator API Where am I? next #=> nil or exception
if done Am I done? nil, StopIteration What’s next? handled for you
External Enumerator result = db_conn.exec ‘SELECT * FROM orders’ while
order = result.next do # … end
Internal Enumerator result = db_conn.exec ‘SELECT * FROM orders’ result.each_row
do |order| # … end
External vs Internal You write loop Impera,ve C, Ruby Loop
built-in Func,onal Scheme, Ruby
Eager Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value
= orders.map { |order| # 10,000 values order[‘total’] }.reduce { |sum, order_total| sum + order_total }
Eager Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value
= orders.map { |order| # 100,000,000 values order[‘total’] }.reduce { |sum, order_total| sum + order_total }
100,000,000 Objects >> a = Array.new 100_000_000 >> ObjectSpace.memsize_of a
=> 800000040 800MB 400ms
Lazy Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value
= orders.lazy.map { |order| order[‘total’] }.reduce { |sum, order_total| sum + order_total } 10MB similar ,me
Eager Processing 100M 100M .map .map 100M .map 100M
Lazy Processing 1 1 .map .map 1 .map 1 2
2 2 2 3 3 3 3 … … … … 100M 100M 100M 100M
How does lazy work? Fibers!
Fiber? •Story line for a program •One Fiber runs at
a ,me •Scheduled by author •“Corou,ne”
Hierarchy Process ↳Thread ↳Fiber OS scheduled Manually scheduled
Scheduling Fibers resume(input) #=> output Run a specific Fiber Fiber.yield(value)
Return output to #resume
Ac,ve Fiber 1 1 .map .map 1 .map 1 2
2 2 2 3 3 3 3 … … … … 100M 100M 100M 100M Fiber Fiber Fiber Fiber
Example of Fiber ⃠
Lazy Enumera,on •Reduces memory used •Great for huge data sets
•Processes one at a ,me •Uses Fiber (corou,ne)