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
140
0
Share
Lazy Enumeration
An introduction to lazy enumeration in ruby
Eric Hodel
April 27, 2016
More Decks by Eric Hodel
See All by Eric Hodel
Building maintainable command-line tools with MRuby
drbrain
0
680
Introduction to Rake
drbrain
0
390
Lessons in Mentorship
drbrain
1
250
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
140
Open Source Maintenance — RailsClub Moscow
drbrain
1
180
drbdump
drbrain
2
540
Other Decks in Programming
See All in Programming
エラー処理の温故知新 / history of error handling technic
ryotanakaya
7
1.9k
色即是空、空即是色、データサイエンス
kamoneggi
1
140
サークル参加から学ぶ、小さな事業の回し方
yuzneri
0
210
【ディップ|26年新卒研修資料】TDD実装演習
dip_tech
PRO
0
280
SkillsをS3 Filesに置く時のあれこれ
watany
3
1.7k
「なんか〇〇ライブラリで脆弱性あるみたいなんだけど。。。」から始める脆弱性対応 / First Steps in Vulnerability Response
mackey0225
2
130
権限チェックの一貫性を型で守る TypeScript による多層防御
mnch
3
290
Are We Really Coding 10× Faster with AI?
kohzas
0
200
Kubernetesを使わない環境にもCloud Nativeなデプロイを実現する / Enabling Cloud Native deployments without the complexity of Kubernetes
linyows
3
430
要はバランスからの卒業 #yumemi_grow
kajitack
0
180
cloudnative conference 2026 flyle
azihsoyn
1
200
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
450
Featured
See All Featured
Designing for humans not robots
tammielis
254
26k
Claude Code のすすめ
schroneko
67
220k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.5k
Marketing to machines
jonoalderson
1
5.3k
Navigating Weather and Climate Data
rabernat
0
190
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
740
Become a Pro
speakerdeck
PRO
31
5.9k
Typedesign – Prime Four
hannesfritz
42
3k
Mind Mapping
helmedeiros
PRO
1
190
Navigating Team Friction
lara
192
16k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
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)