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
130
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
370
Lessons in Mentorship
drbrain
1
240
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
140
Open Source Maintenance — RailsClub Moscow
drbrain
1
170
drbdump
drbrain
2
540
Other Decks in Programming
See All in Programming
ソフトウェア設計の結合バランス #phperkaigi
kajitack
0
140
t *testing.T は どこからやってくるの?
otakakot
1
690
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
340
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
380
mruby on C#: From VM Implementation to Game Scripting (RubyKaigi 2026)
hadashia
2
570
의존성 주입과 모듈화
fornewid
0
150
Angular Signal Forms
debug_mode
0
110
Claude Code × Gemini × Ebitengine ゲーム制作素人WebエンジニアがGoでゲームを作った話
webzawa
0
140
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
5k
Cache-moi si tu peux : patterns et pièges du cache en production - Devoxx France 2026 - Conférence
slecache
0
270
Liberating Ruby's Parser from Lexer Hacks
ydah
2
1.6k
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
4
3k
Featured
See All Featured
AI: The stuff that nobody shows you
jnunemaker
PRO
6
580
Optimizing for Happiness
mojombo
378
71k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.2k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
800
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
130
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
170
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
530
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
280
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
510
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)