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
620
Introduction to Rake
drbrain
0
300
Lessons in Mentorship
drbrain
1
200
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
100
Open Source Maintenance — RailsClub Moscow
drbrain
1
150
drbdump
drbrain
2
470
Other Decks in Programming
See All in Programming
ワイがおすすめする新潟の食 / 20250530phpconf-niigata-eve
kasacchiful
0
260
MLOps Japan 勉強会 #52 - 特徴量を言語を越えて一貫して管理する, 『特徴量ドリブン』な MLOps の実現への試み
taniiicom
2
570
Reactive Thinking with Signals, Resource API, and httpResource @Devm.io Angular 20 Launch Party
manfredsteyer
PRO
0
130
當開發遇上包裝:AI 如何讓產品從想法變成商品
clonn
0
2.6k
衛星の軌道をWeb地図上に表示する
sankichi92
0
250
マテリアルって何者?RealityKitで扱うマテリアル入門
nao_randd
0
140
技術懸念に立ち向かい 法改正を穏便に乗り切った話
pop_cashew
0
830
少数精鋭エンジニアがフルスタック力を磨く理由 -そしてAI時代へ-
rebase_engineering
0
130
Use Perl as Better Shell Script
karupanerura
0
660
Rethinking Data Access: The New httpResource in Angular
manfredsteyer
PRO
0
220
AIエージェントによるテストフレームワーク Arbigent
takahirom
0
280
RubyKaigiで得られる10の価値 〜Ruby話を聞くことだけが RubyKaigiじゃない〜
tomohiko9090
0
100
Featured
See All Featured
Docker and Python
trallard
44
3.4k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
106
19k
Typedesign – Prime Four
hannesfritz
41
2.6k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.6k
Speed Design
sergeychernyshev
30
970
Rails Girls Zürich Keynote
gr2m
94
13k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
42
2.3k
Mobile First: as difficult as doing things right
swwweet
223
9.6k
Art, The Web, and Tiny UX
lynnandtonic
298
21k
Music & Morning Musume
bryan
47
6.6k
KATA
mclloyd
29
14k
The Invisible Side of Design
smashingmag
299
50k
Transcript
Lazy Enumera,on Eric Hodel — drbrain@segment7.net
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)