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
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
700
Introduction to Rake
drbrain
0
400
Lessons in Mentorship
drbrain
1
270
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
150
Open Source Maintenance — RailsClub Moscow
drbrain
1
180
drbdump
drbrain
2
560
Other Decks in Programming
See All in Programming
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
380
AWS Transform Customによる Spring Boot 2.xから4.xへのVerUp
satoshi256kbyte
2
100
自動化したのに回らない テスト運用の壁―AI時代の品質責任と生産性
mfunaki
0
480
MySQLとPostgreSQLって何が違うの?
akagami
0
140
Press start. Python's next generation.
willingc
PRO
3
130
FastAPI の並行処理モデルを完全に理解する
hoto17296
8
3k
私のClaude Code活用法 (個人開発編) - PHPerKaigi mini #4(2026/08/24)
panda_program
1
160
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
160
AIに既存システムを理解させる技術 ~レガシーを見捨てないハーネスエンジニアリング入門~
ochtum
0
150
実装をデザインガイドラインに追従させるための取り組み / 260731-dip-mosh-design-system
dachi023
0
7.8k
承認済みなのに差戻しできてしまうバグ、型で潰せます
shinchit
0
110
ソフトウェアラスタライザ
fadis
1
660
Featured
See All Featured
Statistics for Hackers
jakevdp
799
230k
Evolving SEO for Evolving Search Engines
ryanjones
0
260
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
Exploring anti-patterns in Rails
aemeredith
3
470
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.3k
SEO for Brand Visibility & Recognition
aleyda
0
4.7k
A better future with KSS
kneath
240
18k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Agile that works and the tools we love
rasmusluckow
331
22k
Faster Mobile Websites
deanohume
310
32k
Abbi's Birthday
coloredviolet
3
9.5k
A Tale of Four Properties
chriscoyier
163
24k
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)